1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import os import matplotlib.pyplot as plt import numpy as np from scipy.stats import norm from numpy import array,vstack from scipy.cluster.vq import vq,kmeans #from mpl_toolkits.mplot3d import Axes3D os.chdir(r'D:\projects\wordpress\ex50') os.getcwd() # generate data data=norm.rvs(0,0.3,size=(10000,2)) inside_ball=np.hypot(data[:,0],data[:,1])<1.0 data=data[inside_ball] data = vstack((data, data+array([1,1]),data+array([-1,1]))) #request the data to be separated into three clusters centroids, distortion = kmeans(data,3) cluster_assignment, distances = vq(data,centroids) plt.plot(data[cluster_assignment==0,0], data[cluster_assignment==0,1], 'r.') plt.plot(data[cluster_assignment==1,0], data[cluster_assignment==1,1], 'g.') plt.plot(data[cluster_assignment==2,0], data[cluster_assignment==2,1], 'b.') plt.savefig("example50.png", dpi=100) plt.show() plt.close() |
Recent Comments