【发布时间】:2020-06-16 09:36:35
【问题描述】:
我有以下程序:
cat_feats = ['x', 'y', 'z', 'a', 'b',
'c', 'd', 'e']
onehot_encoder = OneHotEncoder(categories='auto')
# convert each categorical feature from integer
# to one-hot
for feature in cat_feats:
data[feature] = data[feature].array.reshape(len(data[feature]), 1)
data[feature] = onehot_encoder.fit_transform(data[feature])
我对此有疑问。我明白了:
'PandasArray' object has no attribute 'reshape'
使用编码器之前data.head()的输出是这样的:
0 2 1 4 6 3 2 1 37
2 1 7 2 10 0 4 1 37
3 2 15 2 6 0 2 1 37
5 2 0 4 7 1 4 1 37
7 4 14 2 9 0 4 1 37
此输出是 DataFrame 类型,仅包含我试图转换为 one-hot 的整数。我尝试过 .array、.values、.array.reshape(-1, 1),但这些都不起作用。我发现尝试 .values 似乎在 for 循环的第一行中起作用,但是我从一次性转换中得到了垃圾。
请帮忙。
【问题讨论】:
标签: python pandas scikit-learn one-hot-encoding