我得到这种类型的错误:TypeError:'int'和'str'的实例之间不支持'<'

时间:2019-09-13 04:44:30

标签: python python-3.x type-conversion sklearn-pandas

from sklearn.preprocessing import LabelEncoder
var_mod = ['Gender','Married','Dependents','Education','Self_Employed','Property_Area']
le = LabelEncoder()
for i in var_mod:
    data[i] = le.fit_transform(data[i])

我尝试通过编码类别将所有类别变量转换为数字。但是,出现以下错误。

  

TypeError:“ int”和“ str”的实例之间不支持“ <”

1 个答案:

答案 0 :(得分:2)

通过以下示例:https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.LabelEncoder.html

并检查此内容:

from sklearn import preprocessing

le = preprocessing.LabelEncoder()

var_mod = ['Gender','Married','Dependents','Education','Self_Employed','Property_Area']

le.fit(var_mod)

print(le.transform(['Self_Employed','Dependents']))

print(le.transform(['Gender','Married','Dependents','Education','Self_Employed','Property_Area']))