亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

OneHotEncoder categorical_features 已棄用,如何轉換特定列

OneHotEncoder categorical_features 已棄用,如何轉換特定列

桃花長相依 2021-10-19 15:36:18
我需要將獨立字段從字符串轉換為算術符號。我正在使用 OneHotEncoder 進行轉換。我的數據集有許多獨立的列,其中一些是:Country     |    Age       --------------------------Germany     |    23Spain       |    25Germany     |    24Italy       |    30 我必須對 Country 列進行編碼0     |    1     |     2     |       3--------------------------------------1     |    0     |     0     |      230     |    1     |     0     |      251     |    0     |     0     |      24 0     |    0     |     1     |      30我成功地通過使用 OneHotEncoder 作為#Encoding the categorical datafrom sklearn.preprocessing import LabelEncoderlabelencoder_X = LabelEncoder()X[:,0] = labelencoder_X.fit_transform(X[:,0])#we are dummy encoding as the machine learning algorithms will be#confused with the values like Spain > Germany > Francefrom sklearn.preprocessing import OneHotEncoderonehotencoder = OneHotEncoder(categorical_features=[0])X = onehotencoder.fit_transform(X).toarray()現在我收到了要使用的折舊消息categories='auto'。如果我這樣做,將對所有獨立列(如國家、年齡、工資等)進行轉換。如何僅在數據集第 0 列上實現轉換?
查看完整描述

3 回答

?
月關寶盒

TA貢獻1772條經驗 獲得超5個贊

實際上有2個警告:


FutureWarning:整數數據的處理將在 0.22 版本中更改。目前,類別是根據范圍 [0, max(values)] 確定的,而將來它們將根據唯一值確定。如果您想要未來的行為并消除此警告,您可以指定“categories='auto'”。如果您在此 OneHotEncoder 之前使用 LabelEncoder 將類別轉換為整數,那么您現在可以直接使用 OneHotEncoder。


第二個:


'categorical_features' 關鍵字在 0.20 版中已棄用,并將在 0.22 版中刪除。您可以改用 ColumnTransformer。

“改用 ColumnTransformer?!?,DeprecationWarning)


將來,您不應直接在 OneHotEncoder 中定義列,除非您想使用“categories='auto'”。第一條消息還告訴您直接使用 OneHotEncoder,而不是先使用 LabelEncoder。最后,第二條消息告訴您使用 ColumnTransformer,它就像用于列轉換的管道。


這是您案例的等效代碼:


from sklearn.compose import ColumnTransformer 

ct = ColumnTransformer([("Name_Of_Your_Step", OneHotEncoder(),[0])], remainder="passthrough")) # The last arg ([0]) is the list of columns you want to transform in this step

ct.fit_transform(X)    

另請參閱:ColumnTransformer 文檔


對于上面的例子;


編碼分類數據(基本上將文本更改為數字數據,即國家/地區名稱)


from sklearn.preprocessing import LabelEncoder, OneHotEncoder

from sklearn.compose import ColumnTransformer

#Encode Country Column

labelencoder_X = LabelEncoder()

X[:,0] = labelencoder_X.fit_transform(X[:,0])

ct = ColumnTransformer([("Country", OneHotEncoder(), [0])], remainder = 'passthrough')

X = ct.fit_transform(X)


查看完整回答
反對 回復 2021-10-19
  • 3 回答
  • 0 關注
  • 491 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號