NameError:未定义名称“LabelEncoder”

时间:2018-03-16 10:14:29

标签: python machine-learning scikit-learn spyder

我正在使用Py2.7使用Spyder IDE处理数据集。 作为标记数据的一部分,我遇到了这个错误

X = edm.drop('Class', axis=1)
y = edm['Class']

# Encoding our categorical columns in X
labelEncoder = LabelEncoder()
cat_columns = X.dtypes.pipe(lambda x: x[x == 'object']).index
for col in cat_columns:
X[col] = labelEncoder.fit_transform(X[col])

# Train Test Split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, 
random_state=52)

错误:

File "<ipython-input-18-29fe5821f586>", line 5, in <module>
labelEncoder = LabelEncoder()

NameError: name 'LabelEncoder' is not defined

1 个答案:

答案 0 :(得分:3)

您需要先导入LabelEncoder()

from sklearn.preprocessing import LabelEncoder