本文來自同步博客。
前面幾篇文章介紹了回歸或分類的幾個算法般眉,它們的共同點是訓練數(shù)據(jù)包含了輸出結果,要求算法能夠通過訓練數(shù)據(jù)掌握規(guī)律甸赃,用于預測新輸入數(shù)據(jù)的輸出值柿汛。因此,回歸算法或分類算法被稱之為監(jiān)督學習(Supervised Learning)埠对。
本篇文章將接觸有別于監(jiān)督學習的另一類機器學習算法——無監(jiān)督學習(Unsupervised Learning)苛茂。無監(jiān)督學習是尋找缺乏標準答案的輸入數(shù)據(jù)的規(guī)律。其中聚類算法是無監(jiān)督學習主要的分支鸠窗。今天介紹的K-Means算法就是聚類算法的其中一種比較常見的算法妓羊。
K-Means算法原理
K-Means算法的K指的是輸出類別的數(shù)目。該算法是一個迭代過程稍计,每一次迭代分為兩個步驟躁绸,第一步為分類成簇,第二步為移動簇中心净刮,直到簇中心不變。
分類成簇的判定方法是將與簇中心的歐幾里得距離最小的數(shù)據(jù)點歸為對應的一類硅则。而簇中心的計算方式是該類所有數(shù)據(jù)點的平均值淹父,這就是均值‘Mean
’一詞的由來。
下圖演示了K-Means算法每一次迭代數(shù)據(jù)點的分類情況:
可以從上圖看到怎虫,K-Means經(jīng)過4次迭代就完成了聚類過程暑认。每次迭代,圓圈表示的數(shù)據(jù)點都被分類到離它最近的“x”表示的中心點大审,然后對中心點進行了更新蘸际。
K-Means算法實現(xiàn)
下面的代碼展示了K-Means算法的原理,上面的圖片也是通過這塊代碼生成的徒扶。依舊通過注釋方式講代碼,請看:
import numpy as np
import matplotlib.pyplot as plt
# Input data set
X = np.array([
[-4, -3.5], [-3.5, -5], [-2.7, -4.5],
[-2, -4.5], [-2.9, -2.9], [-0.4, -4.5],
[-1.4, -2.5], [-1.6, -2], [-1.5, -1.3],
[-0.5, -2.1], [-0.6, -1], [0, -1.6],
[-2.8, -1], [-2.4, -0.6], [-3.5, 0],
[-0.2, 4], [0.9, 1.8], [1, 2.2],
[1.1, 2.8], [1.1, 3.4], [1, 4.5],
[1.8, 0.3], [2.2, 1.3], [2.9, 0],
[2.7, 1.2], [3, 3], [3.4, 2.8],
[3, 5], [5.4, 1.2], [6.3, 2]
])
# K-Means
def k_means(data, k=2):
if not isinstance(k, int) or k <= 0 or len(data) < k:
return
# Select first K points as centroids
centroids = {0: data[0], 1: data[1]}
# configurations
limit = 0.0001
max_loop_count = 300
total_steps = []
# Loop
for i in range(max_loop_count):
# Classification data into K groups
groups = {}
for j in range(k):
groups[j] = []
for item in data:
dist = [np.linalg.norm(centroids[centroid] - item) for centroid in centroids]
index = dist.index(min(dist))
groups[index].append(item)
# Calculate new centroids
new_centroids = [np.average(groups[i], axis=0) for i in groups]
# Store data for matplotlib
total_steps.append({
'loop': i,
'groups': groups,
'centroids': centroids.copy()
})
# Check whether they change or not
stop_loop = True
for c in centroids:
if abs(np.sum((new_centroids[c] - centroids[c])/centroids[c]*100.0)) > limit:
stop_loop = False
break
if stop_loop:
break
# Update centroids
for c in centroids:
centroids[c] = new_centroids[c]
# Draw pictures
colors = k*['g', 'r', 'b', 'c', 'm', 'y', 'k', 'w']
fig = plt.figure()
for step in total_steps:
# This may cause error if len(total_steps) > 9
ax = fig.add_subplot(1, len(total_steps), step['loop'] + 1)
for g in step['groups']:
for point in step['groups'][g]:
ax.scatter(point[0], point[1], s=20, color=colors[g])
ax.scatter(step['centroids'][g][0], step['centroids'][g][1], marker='x', s=30, color=colors[g])
plt.show()
k_means(X)
scikit-learn
中的KMeans
scikit-learn
中的KMeans存在cluster模塊中,在官方有關KMeans的API文檔中可以看到导坟,數(shù)據(jù)處理結果存放在‘cluster_centers_’屿良、‘labels_’和‘ inertia_’中。下面用到了前兩者惫周,分別是聚類中心點和標簽尘惧。
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
# Input data set
X = np.array([
[-4, -3.5], [-3.5, -5], [-2.7, -4.5],
[-2, -4.5], [-2.9, -2.9], [-0.4, -4.5],
[-1.4, -2.5], [-1.6, -2], [-1.5, -1.3],
[-0.5, -2.1], [-0.6, -1], [0, -1.6],
[-2.8, -1], [-2.4, -0.6], [-3.5, 0],
[-0.2, 4], [0.9, 1.8], [1, 2.2],
[1.1, 2.8], [1.1, 3.4], [1, 4.5],
[1.8, 0.3], [2.2, 1.3], [2.9, 0],
[2.7, 1.2], [3, 3], [3.4, 2.8],
[3, 5], [5.4, 1.2], [6.3, 2]
])
clf = KMeans(n_clusters=2)
clf.fit(X)
centroids = clf.cluster_centers_
labels = clf.labels_
colors = ['r', 'g']
for i in range(len(X)):
plt.scatter(X[i][0], X[i][1], color=colors[labels[i]], s=20)
plt.scatter(centroids[:, 0], centroids[:, 1], marker='x', s=30)
plt.show()
執(zhí)行結果如下: