python - 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()
解决方案
您正在寻找:https ://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.LabelEncoder.html
尝试将 onehotencoder 替换为preprocessing.LabelEncoder()
推荐阅读
- bash - 压缩文件夹时如何排除文件
- bots - 社交媒体机器人在生产中的子文件夹中时不会访问网站
- javascript - 在电子节点重新加载过程中复制图标
- java - 带有多个数据源的 Camel + Spring Boot + JPA 找不到正确的持久性单元
- testing - 弹出确认窗口的 Web UI 自动化
- flutter - 无法读取 null、ListView.Builder 的属性
- android - Android 上 Kotlin 中的 fixedRateTimer 无法运行
- html - 数据 URI 中的数据部分可以包含保留字符吗?
- javascript - v-for 中每个组件的唯一内容
- javascript - 使用 Leaflet.Draw.Circle 将 Java 更新到 1.8.0_301 JavaFX WebView 版本后,Leaflet.Edit.Circle 无法正常工作,因为 OSM 不可拖动