我想在散点图中绘制3“k表示”点。
from pylab import plot,show
from numpy import array
from scipy.cluster.vq import kmeans,vq
data = array([1,1,1,1,1,1,3,3,3,3,3,3,7,7,7,7,7,7])
plot(data,marker='*',linewidth=0)
centroids,x = kmeans(data,3)
idx,x = vq(data,centroids)
plot(data[idx==0,0],data[idx==0,1],'yellow',
data[idx==1,0],data[idx==1,1],'yellow',
data[idx==2,0],data[idx==2,1],'yellow')
plot(centroids[:,0],centroids[:,1],'red',markersize=8)
show()
由于产生了以下错误,上面的代码出了什么问题:
plot(data[idx==0,0],data[idx==0,1],'yellow',
IndexError: too many indices for array
答案 0 :(得分:2)
您的语法data[idx==0,0]
不正确。
>>> data[idx==0,0]
Traceback (most recent call last):
...
IndexError: too many indices for array
稍后,centroids[:,0]
也会导致IndexError: too many indices
错误,因为centroids
是一维数组。
问题在于您的数据是1-d,并且要绘制散点图,您需要2个坐标的值。以下是:
>>> data = data.reshape(9,2) # 2d array of x,y coordinates
>>> data
array([[1, 1],
[1, 1],
[1, 1],
[3, 3],
[3, 3],
[3, 3],
[7, 7],
[7, 7],
[7, 7]])
>>> centroids, x = kmeans(data,3) # clusters in 2d
>>> idx, x = vq(data,centroids)
群集0
x-cooridinates
>>> data[idx==0][:,0]
array([1, 1, 1])
群集0
y坐标
>>> data[idx==0][:,1]
array([1, 1, 1])