machine-learning - Sklearn LabelEncoder 在排序时抛出 TypeError

标签 machine-learning scikit-learn sklearn-pandas

我正在使用 Kaggle 的泰坦尼克号数据集学习机器学习。我正在使用 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'

Description of dataset

最佳答案

我自己解决了。问题在于该特定特征具有 NaN 值。用数值替换它仍然会抛出错误,因为它具有不同的数据类型。所以我用字符值替换了它

 features["Embarked"] = encoder.fit_transform(features["Embarked"].fillna('0'))

关于machine-learning - Sklearn LabelEncoder 在排序时抛出 TypeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43956705/

相关文章:

python - 如何使用gensim使用deepset的词嵌入预训练模型?

Python scikit-learn SVM 分类器 "ValueError: Found array with dim 3. Expected <= 2"

Pyspark 用户定义的列聚合计算

python - 如何将 StandardScaler() 转换转换回数据框?

python - scikit IterativeImputer 中每列的 max_value 和 min_value

image-processing - ROC曲线交叉验证

python - 如何从Python中的URL中提取特征?

machine-learning - 这个符号是什么意思?

python - Scikit-learn(Python) StratifiedKFold 的不同度量结果(f1 分数)

python - 二元分类器 Keras 回调的敏感性和特异性?