【发布时间】:2016-02-01 05:27:41
【问题描述】:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
x = [916,684,613,612,593,552,487,484,475,474,438,431,421,418,409,391,389,388,
380,374,371,369,357,356,340,338,328,317,316,315,313,303,283,257,255,254,245,
234,232,227,227,222,221,221,219,214,201,200,194,169,155,140]
kmeans = KMeans(n_clusters=4)
a = kmeans.fit(np.reshape(x,(len(x),1)))
centroids = kmeans.cluster_centers_
labels = kmeans.labels_
print(centroids)
print(labels)
colors = ["g.","r.","y.","b."]
for i in range(len(x)):
plt.plot(x[i], colors[labels[i]], markersize = 10)
plt.scatter(centroids[:, 0], marker = "x", s = 150, linewidths = 5, zorder = 10)
plt.show()
上面的代码显示了 4 个集群,但它们绝对不是我想要的。
我也收到了一个错误,这使得情况变得更糟。我得到的输出如下图所示。
我得到的错误是:TypeError: scatter() missing 1 required positional argument: 'y' 错误没什么大不了的,因为无论如何我都不喜欢我拥有的东西。
下图是我希望集群输出的样子。
【问题讨论】:
标签: python cluster-analysis k-means