我的Python程序:
#!/usr/bin/python
from pylab import plot,show
from numpy import vstack,array
from numpy.random import rand
from scipy.cluster.vq import kmeans, vq, whiten
import csv
if __name__ == "__main__":
K = 3
data_arr = []
clust_name_arr = []
with open('clustering.csv', 'rb') as f:
reader = csv.reader(f)
for row in reader:
data_arr.append([float(x) for x in row[1:]])
clust_name_arr.append([row[0]])
data = vstack( data_arr )
clust_name = vstack(clust_name_arr)
data = whiten(data)
centroids, distortion = kmeans(data,3)
print "distortion = " + str(distortion)
idx,_ = vq(data,centroids)
plot(data[idx==0,0], data[idx==0,1],'ob',
data[idx==1,0], data[idx==1,1],'or',
data[idx==2,0], data[idx==2,1],'og')
print clust_name
print data
for i in range(K):
result_names = clust_name[idx==i, 0]
print "================================="
print "Cluster " + str(i+1)
for name in result_names:
print name
plot(centroids[:,0],
centroids[:,1],
'sg',markersize=8)
show()
错误讯息: Traceback(最近一次调用最后一次): 文件" /用户//桌面/分配4(2).py",第7行,in 来自pylab import plot,show File" /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pylab.py" ;,第1行,in 来自matplotlib.pylab import * File" /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/pylab.py" ;,第222行,在 来自matplotlib import mpl#pulls in most modules File" /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/mpl.py" ;,第1行,in 来自matplotlib导入艺术家 File" /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/artist.py" ;,第7行,in 来自变换导入Bbox,IdentityTransform,TransformedBbox,\ File" /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/transforms.py" ;,第35行,在 来自matplotlib._path import(affine_transform,count_bboxes_overlapping_bbox, ImportError:dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/_path.so,2):找不到合适的图像。找到了: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/_path.so:通用包装器中没有匹配的架构