Sklearn LabelEncoder 在排序时抛出 TypeError
Sklearn LabelEncoder throws TypeError in sort
我正在使用 Kaggle 的 Titanic 数据集学习机器学习。我正在使用 sklearn 的 LabelEncoder 将文本数据转换为数字标签。以下代码适用于 "Sex" 但不适用于 "Embarked".
encoder = preprocessing.LabelEncoder()
features["Sex"] = encoder.fit_transform(features["Sex"])
features["Embarked"] = encoder.fit_transform(features["Embarked"])
这是我得到的错误
Traceback (most recent call last):
File "../src/script.py", line 20, in <module>
features["Embarked"] = encoder.fit_transform(features["Embarked"])
File "/opt/conda/lib/python3.6/site-packages/sklearn/preprocessing/label.py", line 131, in fit_transform
self.classes_, y = np.unique(y, return_inverse=True)
File "/opt/conda/lib/python3.6/site-packages/numpy/lib/arraysetops.py", line 211, in unique
perm = ar.argsort(kind='mergesort' if return_index else 'quicksort')
TypeError: '>' not supported between instances of 'str' and 'float'
试试这个函数,你需要传递一个 Pandas Dataframe。它将查看您的列的类型并进行编码。所以你甚至不需要自己检查类型。
def encoder(data):
'''Map the categorical variables to numbers to work with scikit learn'''
for col in data.columns:
if data.dtypes[col] == "object":
le = preprocessing.LabelEncoder()
le.fit(data[col])
data[col] = le.transform(data[col])
return data
我自己解决了。问题在于特定特征具有 NaN 值。用数值替换它仍然会抛出错误,因为它具有不同的数据类型。所以我用字符值替换了它
features["Embarked"] = encoder.fit_transform(features["Embarked"].fillna('0'))
我正在使用 Kaggle 的 Titanic 数据集学习机器学习。我正在使用 sklearn 的 LabelEncoder 将文本数据转换为数字标签。以下代码适用于 "Sex" 但不适用于 "Embarked".
encoder = preprocessing.LabelEncoder()
features["Sex"] = encoder.fit_transform(features["Sex"])
features["Embarked"] = encoder.fit_transform(features["Embarked"])
这是我得到的错误
Traceback (most recent call last):
File "../src/script.py", line 20, in <module>
features["Embarked"] = encoder.fit_transform(features["Embarked"])
File "/opt/conda/lib/python3.6/site-packages/sklearn/preprocessing/label.py", line 131, in fit_transform
self.classes_, y = np.unique(y, return_inverse=True)
File "/opt/conda/lib/python3.6/site-packages/numpy/lib/arraysetops.py", line 211, in unique
perm = ar.argsort(kind='mergesort' if return_index else 'quicksort')
TypeError: '>' not supported between instances of 'str' and 'float'
试试这个函数,你需要传递一个 Pandas Dataframe。它将查看您的列的类型并进行编码。所以你甚至不需要自己检查类型。
def encoder(data):
'''Map the categorical variables to numbers to work with scikit learn'''
for col in data.columns:
if data.dtypes[col] == "object":
le = preprocessing.LabelEncoder()
le.fit(data[col])
data[col] = le.transform(data[col])
return data
我自己解决了。问题在于特定特征具有 NaN 值。用数值替换它仍然会抛出错误,因为它具有不同的数据类型。所以我用字符值替换了它
features["Embarked"] = encoder.fit_transform(features["Embarked"].fillna('0'))