python - sklearn KMedoids 返回空簇
问题描述
我正在使用来自 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 之间。我们曾经使用这个算法来计算 Gower distance,但是当我们由于某种原因只有分类数据时,它就不起作用了。我们所有的值都是布尔值。高尔距离返回以下内容:
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.
我的问题可以通过三种方式解决,所以我希望有人可以帮助我:
- 有人知道为什么 sk-learn 的 KMedoids 不起作用并且可以帮助我,所以我可以使用它。
- 有人知道我在 PyPI 的 Gower 函数上做错了什么,所以我可以使用 pyclustering 或 sklearn。
- 有人知道我如何轻松找到用于 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)
解决方案
我也收到了这个警告(但是使用欧几里得距离)。使用集群核心的另一个初始化为我修复了它:
kmedoids = KMedoids(metric='precomputed', init='k-medoids++').fit(dist)
推荐阅读
- github - YubiKey 5Ci FIPS 与 Windows 上的 GitHub
- sql - SQL 错误 [909] [42000]:ORA-00909:参数数量无效
- python - 多个用户上的 Django 石墨烯 JWT
- javascript - 如何自动为自动生成的元素赋予 id
- python - 同时检查多个 URL 的内容更改
- foreach - 表格格式上 jstl 中的哈希集问题
- file - Octave:如何从文件中读取格式化的输入
- dynamics-business-central - 我在哪里可以找到 Business Central 中现有表格的表格和字段规范?
- mongoose - 为什么我需要一个模式来使用 mongoose 查询现有集合?
- javascript - 我们可以在 node js 中使用 Eloquent orm 吗?