8.Using Categorical Data with One Hot Encoding
本教程是機器學習系列的一部分。 在此步驟中,您將了解“分類”變量是什么,以及處理此類數(shù)據(jù)的最常用方法。
Introduction
分類數(shù)據(jù)是僅采用有限數(shù)量值的數(shù)據(jù)。
例如,如果人們回答一項關于他們擁有哪種品牌汽車的調(diào)查,結(jié)果將是明確的(因為答案將是本田,豐田,福特,無等等)。 答案屬于一組固定的類別。
如果您嘗試將這些變量插入Python中的大多數(shù)機器學習模型而不首先“編碼”它們,則會出現(xiàn)錯誤。 在這里,我們將展示最流行的分類變量編碼方法。
One-Hot Encoding : The Standard Approach for Categorical Data
One-Hot Encoding是最普遍的方法,除非你的分類變量具有大量的值,否則它的效果非常好(例如,對于變量超過15個不同值的變量,你通常不會這樣做。在數(shù)值較少的情況下它是一個糟糕的選擇,盡管情況有所不同。)
One-Hot Encoding創(chuàng)建新的(二進制)列,指示原始數(shù)據(jù)中每個可能值的存在。 讓我們通過一個例子來解決。
原始數(shù)據(jù)中的值為紅色,黃色和綠色。 我們?yōu)槊總€可能的值創(chuàng)建一個單獨的列。 只要原始值為紅色,我們在紅色列中放置1。
Example
我們在代碼中看到這個。 我們將跳過基本數(shù)據(jù)設置代碼,因此您可以從擁有train_predictors,test_predictors 的DataFrames位置開始。 該數(shù)據(jù)包含住房特征。 您將使用它們來預測房屋價格,房屋價格存儲在稱為目標的系列中。
【1】
# Read the data import pandas as pd train_data = pd.read_csv('../input/train.csv') test_data = pd.read_csv('../input/test.csv')# Drop houses where the target is missing train_data.dropna(axis=0, subset=['SalePrice'], inplace=True)target = train_data.SalePrice# Since missing values isn't the focus of this tutorial, we use the simplest # possible approach, which drops these columns. # For more detail (and a better approach) to missing values, see # https://www.kaggle.com/dansbecker/handling-missing-values cols_with_missing = [col for col in train_data.columns if train_data[col].isnull().any()] candidate_train_predictors = train_data.drop(['Id', 'SalePrice'] + cols_with_missing, axis=1) candidate_test_predictors = test_data.drop(['Id'] + cols_with_missing, axis=1)# "cardinality" means the number of unique values in a column. # We use it as our only way to select categorical columns here. This is convenient, though # a little arbitrary. low_cardinality_cols = [cname for cname in candidate_train_predictors.columns if candidate_train_predictors[cname].nunique() < 10 andcandidate_train_predictors[cname].dtype == "object"] numeric_cols = [cname for cname in candidate_train_predictors.columns if candidate_train_predictors[cname].dtype in ['int64', 'float64']] my_cols = low_cardinality_cols + numeric_cols train_predictors = candidate_train_predictors[my_cols] test_predictors = candidate_test_predictors[my_cols]Pandas為每個列或系列分配數(shù)據(jù)類型(稱為dtype)。 讓我們從預測數(shù)據(jù)中看到隨機的dtypes樣本:
【2】
train_predictors.dtypes.sample(10) Heating object CentralAir object Foundation object Condition1 object YrSold int64 PavedDrive object RoofMatl object PoolArea int64 EnclosedPorch int64 KitchenAbvGr int64 dtype: object對象表示一列有文本(理論上可能有其他東西,但這對我們的目的來說并不重要)。 對這些“對象”列進行one-hot encode是最常見的,因為它們不能直接插入大多數(shù)模型中。 Pandas提供了一個名為get_dummies的便捷功能,可以獲得one-hot encodings。 這樣叫:
[3]
one_hot_encoded_training_predictors = pd.get_dummies(train_predictors)或者,您可以刪除分類。 為了了解這些方法的比較,我們可以計算兩組可選預測構(gòu)建的模型的平均絕對誤差:
One-hot encoding通常有所幫助,但它會根據(jù)具體情況而有所不同。 在這種情況下,使用one-hot encoded變量似乎沒有任何的好處。
?
[4]
from sklearn.model_selection import cross_val_score from sklearn.ensemble import RandomForestRegressordef get_mae(X, y):# multiple by -1 to make positive MAE score instead of neg value returned as sklearn conventionreturn -1 * cross_val_score(RandomForestRegressor(50), X, y, scoring = 'neg_mean_absolute_error').mean()predictors_without_categoricals = train_predictors.select_dtypes(exclude=['object'])mae_without_categoricals = get_mae(predictors_without_categoricals, target)mae_one_hot_encoded = get_mae(one_hot_encoded_training_predictors, target)print('Mean Absolute Error when Dropping Categoricals: ' + str(int(mae_without_categoricals))) print('Mean Abslute Error with One-Hot Encoding: ' + str(int(mae_one_hot_encoded))) Mean Absolute Error when Dropping Categoricals: 18350 Mean Abslute Error with One-Hot Encoding: 18023Applying to Multiple Files
到目前為止,您已經(jīng)對您的訓練數(shù)據(jù)進行了one-hot encoded。 當你有多個文件(例如測試數(shù)據(jù)集,或者你想要預測的其他數(shù)據(jù))時怎么辦? Scikit-learn對列的排序很敏感,因此如果訓練數(shù)據(jù)集和測試數(shù)據(jù)集未對齊,則結(jié)果將是無意義的。 如果分類在訓練數(shù)據(jù)中與測試數(shù)據(jù)具有不同數(shù)量的值,則可能發(fā)生這種情況。
使用align命令確保測試數(shù)據(jù)的編碼方式與訓練數(shù)據(jù)相同:
【5】
one_hot_encoded_training_predictors = pd.get_dummies(train_predictors) one_hot_encoded_test_predictors = pd.get_dummies(test_predictors) final_train, final_test = one_hot_encoded_training_predictors.align(one_hot_encoded_test_predictors,join='left', axis=1)align命令確保列在兩個數(shù)據(jù)集中以相同的順序顯示(它使用列名來標識每個數(shù)據(jù)集中的哪些列對齊。)參數(shù)join ='left'指定我們將執(zhí)行等效的SQL左連接。 這意味著,如果有一列顯示在一個數(shù)據(jù)集而不是另一個數(shù)據(jù)集中,我們將保留我們的訓練數(shù)據(jù)中的列。 參數(shù)join ='inner'將執(zhí)行SQL數(shù)據(jù)庫調(diào)用內(nèi)連接的操作,僅保留兩個數(shù)據(jù)集中顯示的列。 這也是一個明智的選擇。
Conclusion
世界充滿了分類數(shù)據(jù)。 如果您知道如何使用這些數(shù)據(jù),那么您將成為一名更有效的數(shù)據(jù)科學家。 當您開始使用cateogircal數(shù)據(jù)進行更復雜的工作時,這些資源將非常有用。
Your Turn
使用one-hot encoding允許課程項目中的分類。 然后在X數(shù)據(jù)中添加一些分類列。 如果選擇正確的變量,您的模型將會有相當大的改進。 完成后,單擊此處返回學習機器學習,您可以繼續(xù)改進模型。
總結(jié)
以上是生活随笔為你收集整理的8.Using Categorical Data with One Hot Encoding的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pavprsrv.exe - pavpr
- 下一篇: 声称盗取AMD 450G数据得黑客要不来