机器学习08:K-Means聚类算法

本文来自同步博客

前面几篇文章介绍了回归或分类的几个算法,它们的共同点是训练数据包含了输出结果,要求算法能够通过训练数据掌握规律,用于预测新输入数据的输出值。因此,回归算法或分类算法被称之为监督学习(Supervised Learning)。

本篇文章将接触有别于监督学习的另一类机器学习算法——无监督学习(Unsupervised Learning)。无监督学习是寻找缺乏标准答案的输入数据的规律。其中聚类算法是无监督学习主要的分支。今天介绍的K-Means算法就是聚类算法的其中一种比较常见的算法。

K-Means算法原理

K-Means算法的K指的是输出类别的数目。该算法是一个迭代过程,每一次迭代分为两个步骤,第一步为分类成簇,第二步为移动簇中心,直到簇中心不变。

分类成簇的判定方法是将与簇中心的欧几里得距离最小的数据点归为对应的一类。而簇中心的计算方式是该类所有数据点的平均值,这就是均值‘Mean’一词的由来。

下图演示了K-Means算法每一次迭代数据点的分类情况:

 
迭代过程图

可以从上图看到,K-Means经过4次迭代就完成了聚类过程。每次迭代,圆圈表示的数据点都被分类到离它最近的“x”表示的中心点,然后对中心点进行了更新。

K-Means算法实现

下面的代码展示了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文档中可以看到,数据处理结果存放在‘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()

代码链接

执行结果如下:

 
sklearn的KMeans执行结果

猜你喜欢

转载自www.cnblogs.com/developerdaily/p/9262426.html