One Hot Encoding
One Hot Encoding
one method converting categorical variables to convenient variables (e.g. 0-1) using dummy variables
Pandas
Get dummy columns
dummies = pd.get_dummies(df.town)
merged = pd.concat([df, dummies], axis='columns')
Drop one of the variables
防止变量出现完全共线性情况使参数无法估计
final = merged.drop(['town', 'west windsor'], axis='columns')
Sklearn
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
dfle = df
dfle.town = le.fit_transform(dfle.town)
X = dfle[['town', 'area']].values
y = dfle.price
from sklearn.preprocessing import OneHotEncoder
ohe = OneHotEncoder(categorical_features=[0])
"""
报错如下:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In [28], line 2
1 from sklearn.preprocessing import OneHotEncoder
----> 2 ohe = OneHotEncoder(categorical_features=[0])
TypeError: __init__() got an unexpected keyword argument 'categorical_features'
原因:新版sklearn删去了"categorical_features"参数
解决:from sklearn.compose import ColumnTransformer
"""
from sklearn.preprocessing import OneHotEncoder
from sklearn.compose import ColumnTransformer
ohe = ColumnTransformer([('encoder', OneHotEncoder(), [0])], remainder='passthrough')
X = ohe.fit_transform(X)
X = X[:,1:] # Take all the rows and drop 0th column