【发布时间】:2017-03-22 15:39:30
【问题描述】:
我用的是jupyter notebook,这里是内核信息
Python 3.5.2 |Anaconda 4.1.1(64 位)| (默认,2016 年 7 月 2 日,17:53:06) [GCC 4.4.7 20120313(红帽 4.4.7-1)]
我正在使用 k-means 聚类。当我聚类时,唯一使用的颜色是蓝色。目前如何设置这不是一个大问题,但我需要按比例放大它,因此颜色需要不同。我遵循了一个教程,所以我不能 100% 理解所有代码。代码如下。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
style.use("ggplot")
from sklearn.cluster import KMeans
x = [1,5,1.5,8,1,9]
y = [2,8,1.8,8,.6,11]
plt.scatter(x,y)
plt.show()
X = np.array([[1,2],[5,8],[1.5,1.8],[8,8],[1,.6],[9,11]])
kmeans = KMeans(n_clusters=2)
kmeans.fit(X)
centroids = kmeans.cluster_centers_
labels = kmeans.labels_
print(centroids)
print(labels)
colors = ['r','b','y','g','c','m']
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()
plt.scatter(x,y)
plt.scatter(centroids[:, 0],centroids[:, 1], marker = "x", s=150, linewidths = 5, zorder = 10)
plt.show()
我认为我的问题在于这块。
colors = ['r','b','y','g','c','m']
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)
【问题讨论】:
标签: python matplotlib k-means jupyter-notebook