我有一个名为“vehicles”的数据框,有 8 列。 7 是数字,但名为“Car_name”的列是数据框中的索引 1,并且是分类的。我需要对其进行编码
我尝试了这段代码,但不起作用
ohe = OneHotEncoding(categorical_features = [1])
vehicles_enc = ohe.fit_transform(vehicles).toarray()
TypeError: OneHotEncoder.__init__() got an unexpected keyword argument 'categorical_features'
然而,这在我使用的 YouTube 视频中效果很好。
您似乎正在使用较新版本的 scikit-learn,这很可能与您正在观看的视频不同。
categorical_features
参数对于 OneHotEncoder
无效。
您可以尝试 ColumnTransformer 或直接指定要编码的列..像这样:
from sklearn.preprocessing import OneHotEncoder
from sklearn.compose import ColumnTransformer
ohe = OneHotEncoder()
column_transformer = ColumnTransformer(
transformers=[
('ohe', ohe, [1]) # Index of 'Car_name' column
],
remainder='passthrough' # Keep the other columns as they are
)
vehicles_enc = column_transformer.fit_transform(vehicles).toarray()
作为额外的建议,请务必检查教程中使用的库的版本,并确保检查您正在使用的库的官方文档: