遗憾的是OneHotEncoder无法直接对字符串型的类别变量编码,也就是说OneHotEncoder().fit_transform(testdata[[‘pet’]])这句话会报错(不信你试试)。已经有很多人在 stackoverflow 和 sklearn 的 github issue 上讨论过这个问题,但目前为止的 sklearn 版本仍没有增加OneHotEnc
代码中间加print(orde.categories_) 3.OneHotEncoder # OneHotEncoder:Encode categorical features as a one-hot numeric array(aka 'one-of-K' or 'dummy') #a one-hot encoding of y labels should use a LabelBinarizer instead #Reshape your data either using array.reshape(-1, 1) if your data ...
y = label_encoder.fit_transform(y) Solution 3: Apply one-hot encoding If your target variable represents multiple categories, one-hot encoding can be used to transform it into binary features. This encoding creates binary columns for each category, where a value of 1 indicates membership in a...