【发布时间】:2020-05-13 16:40:03
【问题描述】:
按照教程,我正在学习如何使用 Kmeans。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
style.use("ggplot")
from sklearn.cluster import KMeans
X = np.array([[1, 2],
[5, 8],
[1.5, 1.8],
[8, 8],
[1, 0.6],
[9, 11]])
kmeans = KMeans(n_clusters=2 )
kmeans.fit(X)
centroids = kmeans.cluster_centers_
labels = kmeans.labels_
print(centroids)
print(labels)
colors = ["g.","r.","c.","y."]
for i in range(len(X)):
print("coordinate:",X[i], "label:", labels[i])
plt.plot(X[i][0], X[i][1], colors[labels[i]], markersize = 10)
plt.scatter(centroids[:, 0],centroids[:, 1], marker = "x", s=150, linewidths = 5, zorder = 10)
plt.show()
我想读取一个 csv 文件,然后使用其中一个数据框列来代替上面使用的数组。
我尝试了以下但我没有工作
df=pd.read_csv("Output.csv",encoding='latin1')
X=pd.DataFrame([['Column_1']])
我收到以下错误
ValueError: could not convert string to float: 'Column_1'
这是我使用df.head时输出的样子
x id ... Column_name v Column_1
0 25 0001 ... NaN 854
1 28 0002 ... NaN 85,4
2 29 0003 ... NaN 1524
3 32 NaN ... NaN 0
4 85 0004 ... NaN 0
【问题讨论】:
-
一个原因是,在您的“Column_1”数据集中,可能存在一些无法转换为浮点数的垃圾数据。
-
@N.Moudgil,数据很好。它只是数字,而其中一些可能是小数
-
在您的代码中:我猜 X=pd.DataFrame([['Column_1']]) 是一个错误。您是否尝试过 X=df[['Column_1']] 之类的方法?
-
@Louis Hulot,我收到错误消息说
ValueError: could not convert string to float: '3352,4' -
我认为您没有正确读取 csv(,是 csv 文件中的默认分隔符)。将它放在字符串中并不常见。您能否显示类似 df.head() 的内容,以便我们知道您的数据中有什么?