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”的实例之间不支持“ <”
答案 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']))