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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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