k-means ++究竟是如何工作的?

时间:2011-03-28 23:45:21

标签: algorithm language-agnostic machine-learning cluster-analysis k-means

我无法完全理解k-means ++算法。我很感兴趣的是如何挑选出第一个k质心(其余的就像在原始的k-means中一样)。

  1. 概率函数是基于距离还是高斯?
  2. 同时,最长的远点(来自其他质心)被选中以获得新的质心。
  3. 我将欣赏一步一步的解释和一个例子。维基百科中的那个还不够清楚。一个评论很好的源代码也会有所帮助。如果您使用的是6个阵列,那么请告诉我们哪个是阵列。

3 个答案:

答案 0 :(得分:58)

有趣的问题。感谢您提醒我这篇论文。 PDF link here of the original paper.

简单来说,群集中心最初是从输入观察向量集中随机选择的,如果x不在任何先前选择的中心附近,则选择向量x的概率很高。 / p>

这是一个一维的例子。我们的观察结果是[0,1,2,3,4]。设第一个中心c1为0.下一个群集中心c2为x的概率与||c1-x||^2成正比。所以,P(c2 = 1)= 1a,P(c2 = 2)= 4a,P(c2 = 3)= 9a,P(c2 = 4)= 16a,其中a = 1 /(1 + 4 + 9 + 16)。

假设c2 = 4。然后,P(c3 = 1)= 1a,P(c3 = 2)= 4a,P(c3 = 3)= 1a,其中a = 1 /(1 + 4 + 1)。

我用Python编写了初始化过程;我不知道这对你有帮助。

def initialize(X, K):
    C = [X[0]]
    for k in range(1, K):
        D2 = scipy.array([min([scipy.inner(c-x,c-x) for c in C]) for x in X])
        probs = D2/D2.sum()
        cumprobs = probs.cumsum()
        r = scipy.rand()
        for j,p in enumerate(cumprobs):
            if r < p:
                i = j
                break
        C.append(X[i])
    return C

编辑澄清:cumsum的输出为我们提供了划分区间[0,1]的边界。这些分区的长度等于相应点被选为中心的概率。那么,由于r在[0,1]之间统一选择,因此它将恰好属于这些区间中的一个(因为break)。 for循环检查以查看r所在的分区。

示例:

probs = [0.1, 0.2, 0.3, 0.4]
cumprobs = [0.1, 0.3, 0.6, 1.0]
if r < cumprobs[0]:
    # this event has probability 0.1
    i = 0
elif r < cumprobs[1]:
    # this event has probability 0.2
    i = 1
elif r < cumprobs[2]:
    # this event has probability 0.3
    i = 2
elif r < cumprobs[3]:
    # this event has probability 0.4
    i = 3

答案 1 :(得分:4)

一个班轮。

假设我们需要选择2个聚类中心,而不是随机选择它们{就像我们用简单的k表示},我们将随机选择第一个聚类中心,然后找到距离第一个中心最远的点{这些点很可能不属于第一个集群中心,因为它们离它很远}并在这些远点附近分配第二个集群中心。

答案 2 :(得分:3)

我已经根据Toby Segaran的“集体智慧”一书和这里提供的k-menas ++初始化准备了k-means ++的完整源代码实现。

这里确实有两个距离函数。对于初始质心,使用基于numpy.inner的标准质心,然后对于质心固定使用Pearson。也许Pearson也可以用于初始质心。他们说这样更好。

from __future__ import division

def readfile(filename):
  lines=[line for line in file(filename)]
  rownames=[]
  data=[]
  for line in lines:
    p=line.strip().split(' ') #single space as separator
    #print p
    # First column in each row is the rowname
    rownames.append(p[0])
    # The data for this row is the remainder of the row
    data.append([float(x) for x in p[1:]])
    #print [float(x) for x in p[1:]]
  return rownames,data

from math import sqrt

def pearson(v1,v2):
  # Simple sums
  sum1=sum(v1)
  sum2=sum(v2)

  # Sums of the squares
  sum1Sq=sum([pow(v,2) for v in v1])
  sum2Sq=sum([pow(v,2) for v in v2])    

  # Sum of the products
  pSum=sum([v1[i]*v2[i] for i in range(len(v1))])

  # Calculate r (Pearson score)
  num=pSum-(sum1*sum2/len(v1))
  den=sqrt((sum1Sq-pow(sum1,2)/len(v1))*(sum2Sq-pow(sum2,2)/len(v1)))
  if den==0: return 0

  return 1.0-num/den

import numpy
from numpy.random import *

def initialize(X, K):
    C = [X[0]]
    for _ in range(1, K):
        #D2 = numpy.array([min([numpy.inner(c-x,c-x) for c in C]) for x in X])
        D2 = numpy.array([min([numpy.inner(numpy.array(c)-numpy.array(x),numpy.array(c)-numpy.array(x)) for c in C]) for x in X])
        probs = D2/D2.sum()
        cumprobs = probs.cumsum()
        #print "cumprobs=",cumprobs
        r = rand()
        #print "r=",r
        i=-1
        for j,p in enumerate(cumprobs):
            if r 0:
        for rowid in bestmatches[i]:
          for m in range(len(rows[rowid])):
            avgs[m]+=rows[rowid][m]
        for j in range(len(avgs)):
          avgs[j]/=len(bestmatches[i])
        clusters[i]=avgs

  return bestmatches

rows,data=readfile('/home/toncho/Desktop/data.txt')

kclust = kcluster(data,k=4)

print "Result:"
for c in kclust:
    out = ""
    for r in c:
        out+=rows[r] +' '
    print "["+out[:-1]+"]"

print 'done'

data.txt中:

p1 1 5 6
p2 9 4 3
p3 2 3 1
p4 4 5 6
p5 7 8 9
p6 4 5 4
p7 2 5 6
p8 3 4 5
p9 6 7 8

相关问题