【发布时间】:2016-04-17 18:32:43
【问题描述】:
我想获得 k-means 成本(inertia 在 scikit kmeans 中)。
提醒一下:
成本是每个点到最近集群的距离平方和。
我在 scikit('inertia') 的成本计算之间发现了一个奇怪的差异,
以及我自己计算成本的简单方法
请看下面的例子:
p = np.random.rand(1000000,2)
from sklearn.cluster import KMeans
a = KMeans(n_clusters=3).fit(p)
print a.inertia_ , "****"
means = a.cluster_centers_
s = 0
for x in p:
best = float("inf")
for y in means:
if np.linalg.norm(x-y)**2 < best:
best = np.linalg.norm(x-y)**2
s += best
print s, "*****"
我运行的输出是:
66178.4232156 ****
66173.7928716 *****
在我自己的数据集上,结果更显着(20% 的差异)。
这是 scikit 实现中的错误吗?
【问题讨论】:
标签: python numpy machine-learning scikit-learn k-means