我正在运行Python 3,当我尝试运行此代码时:
from sklearn.preprocessing import LabelEncoder
cv=train.dtypes.loc[train.dtypes=='object'].index
print (cv)
le=LabelEncoder()
for i in cv:
train[i]=le.fit_transform(train[i])
test[i]=le.fit_transform(test[i])
但是,我收到此错误:
le=LabelEncoder()
for i in cv:
train[i]=le.fit_transform(train[i])
test[i]=le.fit_transform(test[i])
Traceback (most recent call last):
File "<ipython-input-5-8739984f61b2>", line 3, in <module>
train[i]=le.fit_transform(train[i])
File "C:\Users\myname\Anaconda3\lib\site-packages\sklearn\preprocessing\label.py", line 127, in fit_transform
self.classes_, y = np.unique(y, return_inverse=True)
File "C:\Users\myname\Anaconda3\lib\site-packages\numpy\lib\arraysetops.py", line 195, in unique
perm = ar.argsort(kind='mergesort' if return_index else 'quicksort')
TypeError: unorderable types: str() > float()
奇怪的是,如果我在数据中的指定列上调用编码器,则输出成功。例如:
le.fit_transform(test['Race'])
结果:
le.fit_transform(test['Race'])
Out[7]: array([2, 4, 4, ..., 4, 1, 4], dtype=int64)
我试过了: 浮子(le.fit_transform(火车[I])) STR(le.fit_transform(火车[I]))
两者都没有奏效。
有人可以提供帮助吗?