Python 3.x 应用'时引发的类型错误;标签编码';s-fit#u变换';函数可用于创建对象类型的列
我正在尝试使用Python 3.x 应用'时引发的类型错误;标签编码';s-fit#u变换';函数可用于创建对象类型的列,python-3.x,machine-learning,scikit-learn,data-science,Python 3.x,Machine Learning,Scikit Learn,Data Science,我正在尝试使用scikit LabelEncoder函数将列编码为数字格式 下面是我的数据帧的头和数据类型 cc_apps.head() 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 b 30.83 0.000 u g w v 1.25 t t 1 f g 00202 0 + 1 a 58.67 4.460 u g
scikit LabelEncoder
函数将列编码为数字格式
下面是我的数据帧的头和数据类型
cc_apps.head()
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 b 30.83 0.000 u g w v 1.25 t t 1 f g 00202 0 +
1 a 58.67 4.460 u g q h 3.04 t t 6 f g 00043 560 +
2 a 24.50 0.500 u g q h 1.50 t f 0 f g 00280 824 +
3 b 27.83 1.540 u g w v 3.75 t t 5 t g 00100 3 +
4 b 20.17 5.625 u g w v 1.71 t f 0 f s 00120 0 +
cc_apps.dtypes
0 object
1 object
2 float64
3 object
4 object
5 object
6 object
7 float64
8 object
9 object
10 int64
11 object
12 object
13 object
14 int64
15 object
dtype: object
下面是我将“object”类型列转换为数值列所做的操作
from sklearn.preprocessing import LabelEncoder
le=LabelEncoder()
for col in cc_apps.columns:
if cc_apps[col].dtype=='object':
cc_apps[col]=le.fit_transform(cc_apps[col])
TypeError: '<' not supported between instances of 'int' and 'str'
During handling of the above exception, another exception occurred:
TypeError: argument must be a string or number
来自sklearn.preprocessing导入标签编码器
le=标签编码()
对于cc_apps.cols中的列:
如果cc_apps[col].dtype=='object':
cc_应用程序[col]=le.fit_变换(cc_应用程序[col])
TypeError:“在对satckoverflow本身进行了一些研究之后,我发现对象cc_apps[col]
是一种混合类型,对于不同的列有不同的数据类型。因此,对于le.fit_transform()
工作的cc_应用程序[col]
应强制为字符串类型
这意味着,代码的编码位如下所示。这对我很有用
from sklearn.preprocessing import LabelEncoder
le=LabelEncoder()
for col in cc_apps.columns:
if cc_apps[col].dtype=='object':
cc_apps[col]=le.fit_transform(cc_apps[col].astype(str))