python - 将对象 dtype 列转换为 float 或 int
问题描述
我正在尝试对一些数据进行聚类,但是当我尝试运行 k-means 时出现错误:float() 参数必须是字符串或数字,而不是“点”
我想我需要改变 dtype
我的 pandas df 给了我以下 dtypes:
Trajectory ID object
Ordinal of the point within the trajectory float64
Timestamp object
Latitude float64
Longitude float64
dtype: object
我需要将对象转换为 float 或 int 以放入 k-means 聚类算法
解决方案
scikit-learn 中有一个辅助类很好地实现了这一点,它被称为 sklearn.preprocessing.LabelEncoder
使用此类的fit_transform
函数对字符串进行编码并inverse_transform
恢复原始
推荐阅读
- python - 排列函数问题
- swift - 在使用 PNG 纹理图集时获得最佳 GPU 性能的提示?
- python - 您如何让 pexpect 在 linux 命令行提示符的 process.expect("$") 上触发?
- javascript - 为每个悬停的元素渲染唯一的 div
- python - 有没有一种简单的方法来重新排序不同大小的列表以进行交互?
- automation - 在 PENTAHO 中生成一系列查询
- mysql - MySQL 事件可以立即删除多行吗?
- java - Java Spring Boot 覆盖注释错误
- ios - 如何在 SwiftUI 上登录 Facebook?
- r - Windows 的 conda 环境中的 r-base 错误