Sklearn LabelEncoder在排序时抛出TypeError

我正在使用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'

数据集描述


回答:

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

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

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注