【问题标题】:K Means Clustering: function to update the centroid of each cluster and choose colorK 均值聚类:更新每个聚类的质心并选择颜色的功能
【发布时间】:2023-03-30 08:06:01
【问题描述】:

这是我正在经历的关于 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

【问题讨论】:

    标签: python matplotlib scikit-learn cluster-analysis k-means


    【解决方案1】:

    似乎给出了两个中心列表。此代码将计算每个点到每个中心点的欧几里得距离,并将蓝色分配给更接近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
    

    【讨论】:

      【解决方案2】:

      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]r 则为centers[0]。请注意,为了将布尔掩码 class_of_points 转换为索引数组,它们加 1 并减 1,以便输出将 False 转换为 0True 为 1,这使它们成为索引。一个例子是:

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

      一样
      [1, 0, 1]
      

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

      colors = colors_map[class_of_points + 0]
      

      【讨论】:

      • 感谢您的详细解释。很有帮助。
      猜你喜欢
      • 2017-09-24
      • 2018-09-19
      • 2018-12-20
      • 2019-06-22
      • 2020-02-21
      • 2019-09-26
      • 2017-09-12
      • 2014-09-16
      • 2017-12-30
      相关资源
      最近更新 更多