首页 > 解决方案 > ValueError:无法将字符串转换为浮点数:'France'

问题描述

在 Python 中使用 oneHotEncoder 时出错。

我在 Udemy 课程之后使用 Python 学习机器学习。当我尝试使用 oneHotEncorder 对源数据中的国家名称进行编码时,出现以下错误。

array = np.array(array, dtype=dtype, order=order, copy=copy)
ValueError: could not convert string to float: 'France'

代码:

from sklearn.preprocessing import OneHotEncoder
oneHotEncObj=OneHotEncoder(categorical_features=[0])
X=oneHotEncObj.fit_transform(X).toarray()

在此处输入图像描述

在此处输入图像描述

标签: pythonmachine-learningscikit-learn

解决方案


您正在寻找:https ://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.LabelEncoder.html

尝试将 onehotencoder 替换为preprocessing.LabelEncoder()


推荐阅读