如何用肘法选择k-medoids中的k值?

时间:2016-11-06 14:26:53

标签: python cluster-analysis k-means

我正在尝试此代码: https://gist.github.com/jaganadhg/9a25fb531df47beb13e3

import pylab as plt
import numpy as np
from scipy.spatial.distance import cdist, pdist
from sklearn.cluster import KMeans
from sklearn.datasets import load_iris

iris = load_iris()

k = range(1,11)

clusters = [KMeans(n_clusters = c,init = 'k-means++').fit(iris.data) for c in k]
centr_lst = [cc.cluster_centers_ for cc in clusters]

k_distance = [cdist(iris.data, cent, 'euclidean') for cent in centr_lst]
clust_indx = [np.argmin(kd,axis=1) for kd in k_distance]
distances = [np.min(kd,axis=1) for kd in k_distance]
avg_within = [np.sum(dist)/iris.data.shape[0] for dist in distances]

with_in_sum_square = [np.sum(dist ** 2) for dist in distances]
to_sum_square = np.sum(pdist(iris.data) ** 2)/iris.data.shape[0]
bet_sum_square = to_sum_square - with_in_sum_square

kidx = 2

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(k, avg_within, 'g*-')
ax.plot(k[kidx], avg_within[kidx], marker='o', markersize=12, \
markeredgewidth=2, markeredgecolor='r', markerfacecolor='None')
plt.grid(True)
plt.xlabel('Number of clusters')
plt.ylabel('Average within-cluster sum of squares')
plt.title('Elbow for KMeans clustering (IRIS Data)')

并且对k-means没有任何问题。 但是当我把k-means改为k-medoids时

clusters = [KMedoids(n_clusters = c).fit(iris.data) for c in k]

(我使用pyclust k-medoids https://github.com/mirjalil/pyclust/blob/master/pyclust/_kmedoids.py

我得到了#34;没有"数组

  

[无,无,无,无,无,无,无]

出了什么问题? 有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

因为您下载但不理解的代码的fit方法不会返回值。

添加return selfreturn self._clusters或类似于fit方法,它可能会按预期运行,至少在此步骤中。