gpt4 book ai didi

python-3.x - 类型错误 : __init__() got an unexpected keyword argument 'categorical_features'

转载 作者:行者123 更新时间:2023-12-03 15:20:02 24 4
gpt4 key购买 nike

Spyder( python 3.7)

我在这里面临以下错误。我已经从 anaconda 提示符更新了所有库。但是找不到问题的解决办法。

from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X_1 = LabelEncoder()
X[:, 1] = labelencoder_X_1.fit_transform(X[:, 1])
labelencoder_X_2 = LabelEncoder()

X[:, 2] = labelencoder_X_2.fit_transform(X[:, 2])
onehotencoder = OneHotEncoder(categorical_features = [1])
X = onehotencoder.fit_transform(X).toarray()
Traceback (most recent call last):

File "<ipython-input-4-05deb1f02719>", line 2, in <module>
onehotencoder = OneHotEncoder(categorical_features = [1])

TypeError: __init__() got an unexpected keyword argument 'categorical_features'

最佳答案

因此,根据您的代码,您必须:

from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.compose import ColumnTransformer

# Country column
ct = ColumnTransformer([("Country", OneHotEncoder(), [1])], remainder = 'passthrough')
X = ct.fit_transform(X)

# Male/Female
labelencoder_X = LabelEncoder()
X[:, 2] = labelencoder_X.fit_transform(X[:, 2])


注意第一个 LabelEncoder 是如何被移除的,您不再需要在列上同时应用标签编码和一个热编码器。

(我有点假设您的示例来自 ML Udemy 类(class),第一列是国家/地区列表,而第二列是男性/女性二元选择)

关于python-3.x - 类型错误 : __init__() got an unexpected keyword argument 'categorical_features' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59476165/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com