来自scipy.cluster.kmeans的不稳定结果 [英] unstable result from scipy.cluster.kmeans
问题描述
以下代码在每次运行时给出不同的结果,同时使用k means方法将数据分为3部分:
The following code gives different results at every runtime while clustering the data into 3 parts using the k means method:
from numpy import array
from scipy.cluster.vq import kmeans,vq
data = array([1,1,1,1,1,1,3,3,3,3,3,3,7,7,7,7,7,7])
centroids = kmeans(data,3,100) #with 100 iterations
print (centroids)
获得的三种可能的结果是:
Three possible results obtained were:
(array([1, 3, 7]), 0.0)
(array([3, 7, 1]), 0.0)
(array([7, 3, 1]), 0.0)
实际上,所计算的k均值的顺序不同.但是,分配哪个k意味着点属于哪个集群不是很不稳定吗?有什么想法吗?
Actually, the order of the calculated k means are different. But, does not it unstable to assign which k means point belongs to which cluster? Any idea??
推荐答案
这是因为如果传递整数作为k_or_guess
参数,则会从输入观测值集中随机选择 k 个初始质心(这就是 Forgy方法).
That's because if you pass an integer as the k_or_guess
parameter, k initial centroids are chosen randomly from the set of input observations (this is known as the Forgy method).
来自文档:
k_or_guess : int或ndarray
要生成的质心数.一种 代码分配给每个质心,也是质心的行索引 在code_book矩阵中生成质心.
The number of centroids to generate. A code is assigned to each centroid, which is also the row index of the centroid in the code_book matrix generated.
最初的k个质心 通过从观察中随机选择观察来选择 矩阵.或者,将k乘N数组指定初始k 重心.
The initial k centroids are chosen by randomly selecting observations from the observation matrix. Alternatively, passing a k by N array specifies the initial k centroids.
尝试猜测一下:
kmeans(data,np.array([1,3,7]),100)
# (array([1, 3, 7]), 0.0)
# (array([1, 3, 7]), 0.0)
# (array([1, 3, 7]), 0.0)
这篇关于来自scipy.cluster.kmeans的不稳定结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!