我正在尝试在 Iris 数据集中的目标列('Species')上使用一个热编码器。
但我收到以下错误:
ValueError:预期的二维数组,而是得到一维数组:
使用 array.reshape(-1, 1) 如果您的数据具有单个特征或使用 array.reshape(1, -1) 如果它包含单个样本来重塑您的数据。
Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species
0 1 5.1 3.5 1.4 0.2 Iris-setosa
1 2 4.9 3.0 1.4 0.2 Iris-setosa
2 3 4.7 3.2 1.3 0.2 Iris-setosa
3 4 4.6 3.1 1.5 0.2 Iris-setosa
4 5 5.0 3.6 1.4 0.2 Iris-setosa
Run Code Online (Sandbox Code Playgroud)
我确实在谷歌上搜索了这个问题,我发现大多数 scikit 学习估计器需要一个二维数组而不是一维数组。
同时,我也发现我们可以尝试通过dataframe及其索引来对单列进行编码,但是没有用
onehotencoder = OneHotEncoder(categorical_features=[df.columns.tolist().index('pattern_id')
X = dataset.iloc[:,1:5].values
y = dataset.iloc[:, 5].values
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder= LabelEncoder()
y = labelencoder.fit_transform(y)
onehotencoder = OneHotEncoder(categorical_features=[0]) …
Run Code Online (Sandbox Code Playgroud) python-3.x pandas scikit-learn data-science one-hot-encoding