1
我想绘制3“k表示”散点图中的点。在散点图上计算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
'数据[IDX == 0,0]'你有什么用它来实现?它不是python valide语法 – Oz123
@ Oz123 - 'data [idx == 0,0]'是完全有效的Python语法,它在numpy中是一个非常常见的习惯用法(虽然它在其他地方也是如此)。 –
@JoeKington,我敢说:你能举个实例吗?我想学习新的东西! – Oz123