K 均值聚类:更新每个聚类的质心并选择颜色的函数

K Means Clustering: function to update the centroid of each cluster and choose color

这是我正在研究的关于 K 均值聚类的示例的摘录。有人可以帮我理解最后两行发生了什么吗?

具体来说:

  1. class_of_points = compare_to_first_center > compare_to_second_center在做什么?它只是返回一个布尔值吗?
  2. 还有下一行colors_map[class_of_points + 1 - 1]在做什么?

提前致谢各位。

import random # library for random number generation
import numpy as np # library for vectorized computation
import pandas as pd # library to process data as dataframes

import matplotlib.pyplot as plt

from sklearn.cluster import KMeans 
from sklearn.datasets.samples_generator import make_blobs

# data
x1 = [-4.9, -3.5, 0, -4.5, -3, -1, -1.2, -4.5, -1.5, -4.5, -1, -2, -2.5, -2, -1.5, 4, 1.8, 2, 2.5, 3, 4, 2.25, 1, 0, 1, 2.5, 5, 2.8, 2, 2]
x2 = [-3.5, -4, -3.5, -3, -2.9, -3, -2.6, -2.1, 0, -0.5, -0.8, -0.8, -1.5, -1.75, -1.75, 0, 0.8, 0.9, 1, 1, 1, 1.75, 2, 2.5, 2.5, 2.5, 2.5, 3, 6, 6.5]

#Define a function that updates the centroid of each cluster

colors_map = np.array(['b', 'r'])
def assign_members(x1, x2, centers):

    compare_to_first_center = np.sqrt(np.square(np.array(x1) - centers[0][0]) + np.square(np.array(x2) - centers[0][1]))
    compare_to_second_center = np.sqrt(np.square(np.array(x1) - centers[1][0]) + np.square(np.array(x2) - centers[1][1]))
    class_of_points = compare_to_first_center > compare_to_second_center
    colors = colors_map[class_of_points + 1 - 1]
    return colors, class_of_points

好像给出了两个中心列表。此代码将计算每个点到每个中心点的欧氏距离,并将蓝色分配给更接近 centers[0][:] 中心点的点,将红色分配给更接近 centers[1][:] 中心点的点.

def assign_members(x1, x2, centers):

    # In the following two lines, the eucledean distances are being calculated
    compare_to_first_center = np.sqrt(np.square(np.array(x1) - centers[0][0]) + np.square(np.array(x2) - centers[0][1]))
    compare_to_second_center = np.sqrt(np.square(np.array(x1) - centers[1][0]) + np.square(np.array(x2) - centers[1][1]))

    # Check which center is closer to each point
    # So class_of_points is a binary arary specifies the class number
    class_of_points = compare_to_first_center > compare_to_second_center

    # Depending on the class number (i.e. 0 or 1) it chooses the colour (blue or red)
    colors = colors_map[class_of_points + 1 - 1]
    return colors, class_of_points

compare_to_first_center是所有点到centers[0]的距离,同理,compare_to_second_center是所有点到centers[1]的距离。现在,class_of_points 是一个与您的点大小相同的布尔数组,说明每个点是否更接近 center[0]centers[1]。如果 class_of_points[i]True,则您数据中的 point[i] 更接近 centers[0]

colors = colors_map[class_of_points + 1 - 1] 将颜色 br 分配给点,b 如果它们更接近 centers[1]rcenters[0]。请注意,为了将布尔掩码 class_of_points 转换为索引数组,他们加 1 并减去 1,以便输出将 False 转换为 0 并将 True 转换为 1,这使它们成为指数。一个例子是:

np.array([True, False, True])+1-1 

相同
[1, 0, 1]

或者,您可以简单地将其替换为:

colors = colors_map[class_of_points + 0]