小编Mas*_*ind的帖子

Python中的K均值聚类

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()
Run Code Online (Sandbox Code Playgroud)

上面的代码显示了4个群集,但是它们绝对不是我想要的。

我还会收到一个错误,这甚至更糟。我得到的输出在下面的图片中。

我得到的错误是:TypeError: scatter() missing 1 required positional argument: 'y'错误不是大问题,因为无论如何我都不喜欢我拥有的东西。

集群输出

以下是我希望群集输出如何显示的图像。

我想要的丛集

python cluster-analysis k-means

5
推荐指数
1
解决办法
4933
查看次数

标签 统计

cluster-analysis ×1

k-means ×1

python ×1