首页 > 解决方案 > 将对象 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 聚类算法

标签: pythondtype

解决方案


scikit-learn 中有一个辅助类很好地实现了这一点,它被称为 sklearn.preprocessing.LabelEncoder

使用此类的fit_transform函数对字符串进行编码并inverse_transform恢复原始


推荐阅读