python进行聚类(scikit-lean、scipy)

来源:互联网 发布:智百威软件800 编辑:程序博客网 时间:2024/05/19 08:26
 

python进行聚类(scikit-lean、scipy)

[python] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. 用于聚类的数据集  
[python] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. %matplotlib inline  
  2. import scipy.io as sio  
  3. import matplotlib.pyplot as plt  
  4.   
  5.   
  6. ''''' 
  7. 各种聚类数据 
  8. '''  
  9. #two_cluster  
  10. def two_cluster():  
  11.     two_cluster=u'cluster_data/two_cluster.mat'  
  12.     two_cluster=sio.loadmat(two_cluster)['X'].T  
  13.     data = two_cluster  
  14.     return data  
  15. #three_cluster  
  16. def three_cluster():  
  17.     path=u'cluster_data/three_cluster.mat'  
  18.     three_cluster=sio.loadmat(path)['X'].T  
  19.     data = three_cluster      
  20.     return data  
  21. #five_cluster  
  22. def five_cluster():  
  23.     path=u'cluster_data/five_cluster.mat'  
  24.     five_cluster=sio.loadmat(path)  
  25.     x=five_cluster['x'#得到的数据为二行n列  
  26.     y=five_cluster['y'#到的数据为一行n列  
  27.     data = np.vstack((x,y)).T #先垂直合并,而后转置  
  28.     #data = np.array([x[0,:],x[1,:],y[0,:]]).T #list与array互换  
  29.     return data  
  30. #spiral  
  31. def spiral():  
  32.     path=u'cluster_data/spiral.mat'  
  33.     spiral=sio.loadmat(path)['spiral']  
  34.     spiral = spiral[0::3,:] #每隔3行取一个数据  
  35.     data = spiral  
  36.     data = np.array([data[:,1],data[:,2],data[:,0]]).T #list与array互换  
  37.     return data  
  38. #spiral_unbalance  
  39. def spiral_unbalance():  
  40.     path=u'cluster_data/spiral_unbalance.mat'  
  41.     spiral_unbalance=sio.loadmat(path)['spiral_unbalance']  
  42.     spiral_unbalance = spiral_unbalance[0::3,:] #每隔3行取一个数据  
  43.     data = spiral_unbalance  
  44.     data = np.array([data[:,1],data[:,2],data[:,0]]).T #list与array互换  
  45.     return data  
  46. #ThreeCircles  
  47. def ThreeCircles():  
  48.     path=u'cluster_data/ThreeCircles.mat'  
  49.     ThreeCircles=sio.loadmat(path)['ThreeCircles']  
  50.     ThreeCircles = ThreeCircles[0::3,:] #每隔3行取一个数据  
  51.     data = ThreeCircles  
  52.     data = np.array([data[:,1],data[:,2],data[:,0]]).T #list与array互换  
  53.     return data  
  54. #Twomoons  
  55. def Twomoons():  
  56.     path=u'cluster_data/Twomoons.mat'  
  57.     Twomoons=sio.loadmat(path)['Twomoons']  
  58.     Twomoons = Twomoons[0::3,:] #每隔3行取一个数据  
  59.     data = Twomoons  
  60.     data = np.array([data[:,1],data[:,2],data[:,0]]).T #list与array互换  
  61.     plt.scatter(data[:,0],data[:,1],c=data[:,2])  
  62.     return data  
  63. #Twomoons1  
  64. def Twomoons1():  
  65.     path=u'cluster_data/Twomoons.mat'  
  66.     Twomoons1=sio.loadmat(path)['Twomoons']  
  67.     Twomoons1 = Twomoons1[0::3,:] #每隔3行取一个数据  
  68.     data = Twomoons1  
  69.     data = np.array([data[:,1],data[:,2],data[:,0]]).T #list与array互换  
  70.     return data  
  71. def test():  
  72.     print 'test'  
  73.   
  74.   
  75. def show_all():  
  76.     plt.figure(figsize=(16,8))  
  77.     #动态调用方法  
  78.     func_name_list = ['two_cluster','three_cluster','five_cluster','spiral','spiral_unbalance','ThreeCircles','Twomoons','Twomoons1']  
  79.     for i in range(8):  
  80.         data_list.append(eval(func_name_list[i])())  
  81.     #动态画图  
  82.     for i in range(8):  
  83.         data = data_list[i]  
  84.         plt.subplot(2,4,i+1)  
  85.         #plt.figure()  
  86.         plt.scatter(data[:,0],data[:,1],c=data[:,2])  
  87.       
  88. data_list = []  
  89. show_all()  


[python] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. 使用scikit的kmeans进行聚类  
[python] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. %matplotlib inline  
  2. import scipy.io as sio  
  3. #matlab文件名    
  4. two_cluster=u'cluster_data/two_cluster.mat'  
  5. data=sio.loadmat(two_cluster)  
  6. print data  
[python] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. %matplotlib inline  
  2. import matplotlib.pyplot as plt  
  3. x = data['X']  
  4. cValue = x[2]  
  5. plt.scatter(x[0],x[1],c=cValue)  
[python] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. from sklearn import cluster, datasets  
  2. b = np.array(x).T  
  3. b = b[:,0:2]  
  4.   
  5. y_pred = cluster.KMeans(n_clusters=2, random_state=170).fit_predict(b)  
  6.   
  7. cValue = x[2]  
  8. plt.scatter(x[0],x[1],c=y_pred)  

数据集下载

scikit-learn教程


[python] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. %matplotlib inline  
  2. import scipy.io as sio  
  3. #matlab文件名    
  4. two_cluster=u'cluster_data/spiral.mat'  
  5. spiral=sio.loadmat(two_cluster)['spiral']  
  6. spiral = spiral[0::3,:] #每隔3行取一个数据  
  7. print len(spiral),len(spiral[0])  
  8. cValue = spiral[:,0]  
  9. print cValue.shape  
  10. color = ['b','y']  
  11. cValue = [color[int(i)] for i in list(cValue)]  
  12. plt.scatter(spiral[:,1],spiral[:,2],c=cValue)  


使用kmeans结果

[python] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. from sklearn import cluster, datasets  
  2.   
  3. y_pred = cluster.KMeans(n_clusters=2, random_state=170).fit_predict(spiral[:,1:3])  
  4.   
  5. plt.scatter(spiral[:,1],spiral[:,2],c=y_pred)  


使用scipy进行聚类效果

[python] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. # -*- coding: utf8 -*-  
  2. %matplotlib inline  
  3. import scipy.io as sio  
  4. import matplotlib.pyplot as plt  
  5. import scipy.cluster.hierarchy as hcluster  
  6. from sklearn.cluster import AgglomerativeClustering  
  7. import numpy.random as random    
  8. import numpy as np    
  9. import numpy.core.fromnumeric    
  10.   
  11.   
  12. def loadData():  
  13.     #matlab文件名    
  14.     two_cluster=u'cluster_data/spiral.mat'  
  15.     spiral=sio.loadmat(two_cluster)['spiral']  
  16.     spiral = spiral[0::3,:] #每隔3行取一个数据  
  17.     print len(spiral),len(spiral[0])  
  18.     cValue = spiral[:,0]  
  19.     print cValue.shape  
  20.     color = ['b','y']  
  21.     cValue = [color[int(i)] for i in list(cValue)]  
  22.     plt.scatter(spiral[:,1],spiral[:,2],c=cValue)  
  23.   
  24.   
  25. def spiralSample():  
  26.     plt.subplot(131)  
  27.     plt.title(u'origal data')  
  28.     plt.scatter(spiral[:,1],spiral[:,2],c=spiral[:,0])  
  29.     #scipy进行聚类,默认depth=2(可得到两类),阈值t为距离阈值,设置criterion='maxclust',找到两类之间最小距离小于t的进行合并  
  30.     #http://docs.scipy.org/doc/scipy/reference/generated/scipy.cluster.hierarchy.fcluster.html#scipy.cluster.hierarchy.fcluster  
  31.     y_pred=hcluster.fclusterdata(spiral[:,1:3],criterion='maxclust',t=2)      
  32.     plt.subplot(132)  
  33.     plt.title(u'use scipy to hierarchy cluster')  
  34.     plt.scatter(spiral[:,1],spiral[:,2],c=y_pred)  
  35.     #scikit进行聚类  
  36.     plt.subplot(133)  
  37.     plt.title(u'use scikit to hierarchy cluster')  
  38.     y_pred = AgglomerativeClustering(n_clusters=2, linkage='ward').fit_predict(spiral[:,1:3])      
  39.     plt.scatter(spiral[:,1],spiral[:,2],c=y_pred)  
  40.     plt.show()  
  41. spiralSample()  


转自:http://blog.csdn.net/yan456jie/article/details/52214815
0 0
原创粉丝点击