我不确定如何在python中使用kmedoids。我已经从https://pypi.org/project/pyclustering/安装了pyclustering模块,但不确定如何调用kmedoids?我正在尝试在我的行距矩阵上实施PAM。
我正在尝试从交易数据集中对要素进行聚类。我使用此https://sourceforge.net/projects/gower-distance-4python/files/来计算矩阵上的行距。然后我使用这个我称为D的矩阵来通过PAM / kmedoids
import pyclustering
import pyclustering.cluster.kmedoids
from sklearn.metrics.pairwise import pairwise_distances
import numpy as np
D = gower_distances(trade_data)
pam=pyclustering.kmedoids(D)
AttributeError: module 'pyclustering' has no attribute 'kmedoids'
我收到上述错误,我如何称呼kmedoids /使用PAM?
答案 0 :(得分:2)
您需要更正导入和K-Medoids初始化:
from pyclustering.cluster.kmedoids import kmedoids
... ...
pam=kmedoids(D, initial_medoids)
答案 1 :(得分:2)
您需要将kmedoids导入为
from pyclustering.cluster.kmedoids import kmedoids
您可以在https://codedocs.xyz/annoviko/pyclustering/classpyclustering_1_1cluster_1_1kmedoids_1_1kmedoids.html
的pyclustering文档中阅读有关此内容的更多信息。答案 2 :(得分:0)
这是来自https://stats.stackexchange.com/questions/94172/how-to-perform-k-medoids-when-having-the-distance-matrix/470141#470141的非常小的代码示例。它以一个已经给定的距离矩阵开始,然后使用gower_distances()。
from pyclustering.cluster.kmedoids import kmedoids
import numpy as np
dm = np.array(
[[0.,1.91,2.23,3.14,4.25,3.37],
[0.,0.,2.15,1.82,2.41,2.58],
[0.,0.,0.,3.12,3.83,4.64],
[0.,0.,0.,0.,1.9,2.66],
[0.,0.,0.,0.,0.,3.12],
[0.,0.,0.,0.,0.,0.]])
dm = dm + np.transpose(dm)
k = 2
# choose medoid 2 and 4 in your C1 and C2 because min(D) in their cluster
initial_medoids = [1,3]
kmedoids_instance = kmedoids(dm, initial_medoids, data_type = 'distance_matrix')
# Run cluster analysis and obtain results.
kmedoids_instance.process()
clusters = kmedoids_instance.get_clusters()
centers = kmedoids_instance.get_medoids()
print(clusters)
# [[1, 0, 2, 5], [3, 4]]
print(centers)
# [1, 3]