【问题标题】:sklearn KMedoids returns empty clusterssklearn KMedoids 返回空簇
【发布时间】:2020-09-24 15:49:20
【问题描述】:

我正在使用来自 sklearn_extra.cluster 的 KMedoids。我将它与预先计算的距离矩阵(metric='precomputed')一起使用,它曾经可以工作。但是,我们在计算距离矩阵的方式中发现了一个错误,因此必须自己实现它。从那时起,KMedoids 算法就不再起作用了。这是堆栈跟踪:

C:\Users\...\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sklearn_extra\cluster\_k_medoids.py:231: UserWarning: Cluster 1 is empty! self.labels_[self.medoid_indices_[1]] may not be labeled with its corresponding cluster (1).
  warnings.warn(enter code here
C:\Users\...\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sklearn_extra\cluster\_k_medoids.py:231: UserWarning: Cluster 2 is empty! self.labels_[self.medoid_indices_[2]] may not be labeled with its corresponding cluster (2).
  warnings.warn(
C:\Users\...\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sklearn_extra\cluster\_k_medoids.py:231: UserWarning: Cluster 3 is empty! self.labels_[self.medoid_indices_[3]] may not be labeled with its corresponding cluster (3).
  warnings.warn(
C:\Users\...\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sklearn_extra\cluster\_k_medoids.py:231: UserWarning: Cluster 4 is empty! self.labels_[self.medoid_indices_[4]] may not be labeled with its corresponding cluster (4).
  warnings.warn(
C:\Users\...\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sklearn_extra\cluster\_k_medoids.py:231: UserWarning: Cluster 5 is empty! self.labels_[self.medoid_indices_[5]] may not be labeled with its corresponding cluster (5).
  warnings.warn(
C:\Users\...\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sklearn_extra\cluster\_k_medoids.py:231: UserWarning: Cluster 6 is empty! self.labels_[self.medoid_indices_[6]] may not be labeled with its corresponding cluster (6).
  warnings.warn(
C:\Users\...\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sklearn_extra\cluster\_k_medoids.py:231: UserWarning: Cluster 7 is empty! self.labels_[self.medoid_indices_[7]] may not be labeled with its corresponding cluster (7).
  warnings.warn(

我检查了距离矩阵,它是一个二维 nparray,维度为 n_data x n_data,其中对角线上的值为零,所以这不应该是问题。所有值都在 0 和 1 之间。我们曾经使用 this algorithm for the Gower distance,但是当我们由于某种原因只有分类数据时,这不起作用。我们所有的值都是布尔值。 Gower 距离返回如下:

File "C:\Users\...\AppData\Local\Programs\Python\Python38-32\lib\site-packages\gower\gower_dist.py", line 62, in gower_matrix
    Z_num = np.divide(Z_num ,num_max,out=np.zeros_like(Z_num), where=num_max!=0)
TypeError: ufunc 'true_divide' output (typecode 'd') could not be coerced to provided output parameter (typecode '?') according to the casting rule ''same_kind''

我还尝试了 pyclustering KMedoids,并且确实有效。但是,您需要使用 pyclustering 自己定义初始中心点,而我发现的方法不适用于分类数据。 (见下文)

initial_medoids = kmeans_plusplus_initializer(data, n_clus, kmeans_plusplus_initializer.FARTHEST_CENTER_CANDIDATE).initialize(return_index=True)

堆栈跟踪:

File "path_to_file", line 19, in <module>
    initial_medoids = kmeans_plusplus_initializer(data, n_clus, kmeans_plusplus_initializer.FARTHEST_CENTER_CANDIDATE).initialize(return_index=True)
  File "path\Python\Python38-32\lib\site-packages\pyclustering\cluster\center_initializer.py", line 357, in initialize
    index_point = self.__get_next_center(centers)
  File "path\Python\Python38-32\lib\site-packages\pyclustering\cluster\center_initializer.py", line 256, in __get_next_center
    distances = self.__calculate_shortest_distances(self.__data, centers)
  File "path\Python\Python38-32\lib\site-packages\pyclustering\cluster\center_initializer.py", line 236, in __calculate_shortest_distances      
    dataset_differences[index_center] = numpy.sum(numpy.square(data - center), axis=1).T
TypeError: numpy boolean subtract, the `-` operator, is not supported, use the bitwise_xor, the `^` operator, or the logical_xor function instead.

我的问题可以通过三种方式解决,所以希望有人可以帮助我:

  1. 有人知道为什么 sk-learn 的 KMedoids 不起作用并且可以帮助我解决这个问题,所以我可以使用它。
  2. 有人知道我在使用 PyPI 的 Gower 函数时做错了什么,所以我可以使用 pyclustering 或 sklearn。
  3. 有人知道我可以如何轻松找到用于 pyclustering 的初始 medoid,因此我可以使用 pyclustering。

我已经发布了下面代码的简单版本。

import pandas as pd
import gower_distance as dist
from sklearn_extra.cluster import KMedoids

data = pd.read_csv(path_to_data)
dist = calcDist(data) # Returns NxN array where N is the amount of data points
# I'm using 8 clusters, which is the default, so I haven't defined it
kmedoids = KMedoids(metric='precomputed').fit(dist)
labels = kmedoids.predict(dist)

【问题讨论】:

    标签: python scikit-learn cluster-analysis


    【解决方案1】:

    我也收到了该警告(但是使用欧几里得距离)。使用集群核心的另一个初始化为我修复了它:

    kmedoids = KMedoids(metric='precomputed', init='k-medoids++').fit(dist)
    

    【讨论】:

      【解决方案2】:

      从训练好的模型中获取集群标签(即训练标签),

      data = pd.read_csv(path_to_data)
      dist = calcDist(data)
      kmedoids = KMedoids(metric='precomputed').fit(dist)
      labels = kmedoids.labels_
      

      要将kmedoids.predict 与使用经过训练的k-medoids 模型的任何预测数据一起使用,您需要计算N x KN 预测数据到K medoids 的距离矩阵,正确索引 .

      medoids = predictData[kmedoids.medoid_indices_, :]
      distToMedoids = calcDistToMedoids(predictData, medoids) # with the same metric used in training
      predict_labels = kmedoids.predict(distToMedoids)
      predict_labels = np.argmin(distToMedoids, axis=1) # what .predict() does 
      

      您可以通过the source code查看更多信息。

      【讨论】:

        猜你喜欢
        • 2012-11-03
        • 2018-12-07
        • 1970-01-01
        • 2021-03-03
        • 2018-03-15
        • 2020-05-27
        • 1970-01-01
        • 2014-05-05
        • 2018-05-20
        相关资源
        最近更新 更多