close
三維資料分群
matplot 3d, ref: http://matplotlib.org/1.3.1/mpl_toolkits/mplot3d/tutorial.html
numpy.random.rand, ref: http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.rand.html#numpy.random.rand
numpy.vstack(vertically), ref: http://docs.scipy.org/doc/numpy/reference/generated/numpy.vstack.html
scipy.cluster.vq.vq(vector quantization), ref: http://docs.scipy.org/doc/scipy-0.13.0/reference/generated/scipy.cluster.vq.vq.html
scipy.cluster.vq.kmeans, ref: http://docs.scipy.org/doc/scipy-0.13.0/reference/generated/scipy.cluster.vq.kmeans.html
from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from numpy import vstack,array from numpy.random import rand from scipy.cluster.vq import kmeans,vq #隨機產生兩群資料,並在xyz各加上0.8讓兩群的數值有距離#rand(100,3) 產生100x3的float資料( uniform distribution over [0, 1). ) data = vstack((rand(100,3) + array([.8,.8,.8]),rand(100,3))) # K-Means, 分兩群, return 兩群的centroid centroids,d = kmeans(data,2) #分群,return 每一筆資料被分到哪一群 which_c,dis = vq(data,centroids) #畫圖 fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(data[which_c==0,0], data[which_c==0,1], data[which_c==0,2], c='b', marker='o') ax.scatter(data[which_c==1,0], data[which_c==1,1], data[which_c==1,2], c='r', marker='o') ax.scatter(centroids[:,0], centroids[:,1], centroids[:,2], c='g', marker='*', s=100) ax.set_xlabel('X Label') ax.set_ylabel('Y Label') ax.set_zlabel('Z Label') plt.show()
全站熱搜