我有一个数据集,它有以下分类数据colname。当使用sklearn执行一次热编码时,我得到一个错误。
def ohe_encode(train, test, index):
Onehot = OneHotEncoder(categorical_features='all', handle_unknown='error')
x_train_1 = train
x_test_1 = test
colname = df.columns[index]
Onehot.fit(train[colname].astype(str))
x_trans = Onehot.transform(train[columnns].astype(str))
new_features = Onehot.transform(test[colname].astype(str))
return (x_transform, new_features)
屏幕显示出现错误,
ValueError: could not convert string to float: 'yes'
无法得到错误的原因。
提前感谢,,
摘自SKOneHotEncoder文档(重点矿山):
使用one-of-K方案对分类整数特征进行编码。
此转换器的输入应为整数矩阵,表示分类(离散)特征所采用的值。
然而,您输入原始的分类值,例如。字符串,如“是”和“否”。因此,您会得到ValueError。
您需要首先对数据进行因式分解,这意味着您需要将字符串转换为分类数字(整数)。然后你可以做一个热编码。