python k-means聚類實例


port  sys
reload(sys)
sys.setdefaultencoding('utf-8')

import matplotlib.pyplot as plt
import numpy as np

culster1 = np.random.uniform(0.5, 1.5, (2, 20))
culster2 = np.random.uniform(1.5, 2.5, (2, 20))
culster3 = np.random.uniform(1.5, 3.5, (2, 20))
culster4 = np.random.uniform(3.5, 4.5, (2, 20))

x1 = np.hstack((culster1,culster2))
x2 = np.hstack((culster2,culster3))
x = np.hstack((x1,x2)).T

plt.figure()
plt.axis([0, 5, 0, 5])
plt.xlabel('x')
plt.ylabel('y')
plt.grid(True)
plt.plot(x[:,0],x[:,1], 'k.', markersize = 12)

from sklearn.cluster import KMeans
from scipy.spatial.distance import cdist

kmeans = KMeans(n_clusters = 2)
kmeans.fit(x)
plt.plot(kmeans.cluster_centers_[:,0],kmeans.cluster_centers_[:,1],'ro')

K = range(1, 10)
meandistortions = []
for k in K:
    kmeans = KMeans(n_clusters=k)
    kmeans.fit(x)
    meandistortions.append(sum(np.min(cdist(x, kmeans.cluster_centers_,'euclidean'), axis=1)) / x.shape[0])#選擇每行最小距離求和
plt.figure()
plt.grid(True)
plt1 = plt.subplot(2,1,1)
plt1.plot(x[:,0], x[:,1], 'k.')
plt2 = plt.subplot(2,1,2)
plt2.plot(K, meandistortions)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM