1.用python實現K均值算法
import numpy as np x=np.random.randint(1,100,[20,1]) y=np.zeros(20) k=3
def initcenter(x,k): return x[:k] kc=initcenter(x,k) kc
def nearest(kc,i): d=(abs(kc-i)) w=np.where(d==np.min(d)) return w[0][0] kc=initcenter(x,k) nearest(kc,93)
for i in range(x.shape[0]): y[i]=nearest(kc,x[i]) print(y)
def nearest(kc,i): d=(abs(kc-i)) w=np.where(d==np.min(d)) return w[0][0]
def initcenter(x,k): return x[:k] def nearest(kc,i): d=(abs(kc - i)) w=np.where(d==np.min(d)) return w[0][0] def xclassify(x,y,kc): for i in range(x.shape[0]): y[i]=nearest(kc,x[i]) return y kc=initcenter(x,k) y=xclassify(x,y,kc) print(kc,y)
m=np.where(y==0) m
np.mean(x[m])
kc[0]=66 flag=True
def kcmean (x,y,kc,k): #計算各聚類新均值 l=list(kc) flag=False for c in range(k): m=np.where(y==c) n=np.mean(x[m]) if l[c] !=n: l[c]=n flag=True #聚類中心發生變化 return (np.array(l),flag)
def xclassify(x,y,kc): for i in range (x.shape[0]): #對數組的每個值分類 y[i]=nearest(kc,x[i]) return y
flag = True print(x,y,kc,flag) while flag: y = xclassify(x,y,kc) kc,flag = kcmean(x,y,kc,k) print(y,kc,type(kc))
import matplotlib.pyplot as plt plt.scatter(x,x,s=50,cmap="rainbow"); plt.show()
2. 鳶尾花花瓣長度數據做聚類並用散點圖顯示。
import numpy as np from sklearn.datasets import load_iris iris = load_iris() x = iris.data[:, 1] y = np.zeros(150) def initcent(x, k): # 初始聚類中心數組 return x[0:k].reshape(k) def nearest(kc, i): # 數組中的值,與聚類中心最小距離所在類別的索引號 d = (abs(kc - i)) w = np.where(d == np.min(d)) return w[0][0] def kcmean(x, y, kc, k): # 計算各聚類新均值 l = list(kc) flag = False for c in range(k): m = np.where(y == c) n = np.mean(x[m]) if l[c] != n: l[c] = n flag = True # 聚類中心發生變化 return (np.array(l), flag) def xclassify(x, y, kc): for i in range(x.shape[0]): # 對數組的每個值分類 y[i] = nearest(kc, x[i]) return y k = 3 kc = initcent(x, k) flag = True print(x, y, kc, flag) while flag: y = xclassify(x, y, kc) kc, flag = kcmean(x, y, kc, k) print(y, kc, type(kc))
import matplotlib.pyplot as plt plt.scatter(x,x,c=y,s=50,cmap='rainbow',marker='p',alpha=0.5); plt.show()
3. 用sklearn.cluster.KMeans,鳶尾花花瓣長度數據做聚類並用散點圖顯示.
import matplotlib.pyplot as plt import numpy as np from sklearn.datasets import load_iris iris=load_iris() X=iris.data print(X) from sklearn.cluster import KMeans est=KMeans(n_clusters=3) est.fit(X) kc=est.cluster_centers_ y_kmeans=est.predict(X) print(y_kmeans,kc) print(kc.shape,y_kmeans.shape,X.shape) plt.scatter(X[:,0],X[:,1],c=y_kmeans,s=50,cmap='rainbow'); plt.show()
4. 鳶尾花完整數據做聚類並用散點圖顯示.
from sklearn.cluster import KMeans import numpy as np from sklearn.datasets import load_iris import matplotlib.pyplot as plt data = load_iris() iris = data.data petal_len = iris print(petal_len) k_means = KMeans(n_clusters=3) #三個聚類中心 result = k_means.fit(petal_len) #Kmeans自動分類 kc = result.cluster_centers_ #自動分類后的聚類中心 y_means = k_means.predict(petal_len) #預測Y值 plt.scatter(petal_len[:,0],petal_len[:,2],c=y_means, marker='*', label='see') plt.show()