部分定义scikit学习K-Means聚类的初始质心



Scikit文档指出:

初始化方法:

"k-means++":以智能的方式为k-means聚类选择初始聚类中心,以加快收敛速度。有关更多详细信息,请参阅k_init中的"注释"部分。

如果ndarray通过,它的形状应该是(n_clusters,n_features(,并给出初始中心。

我的数据有10个(预测的(集群和7个特征。然而,我想通过10乘6形状的数组,即我希望的质心的6个维度由我预定义,但第7个维度使用k-mean++自由迭代。(换句话说,我不想指定初始质心,而是控制6个维度,只留下一个维度来改变初始集群(

我试着通过10x6的尺寸,希望它能起作用,但它只是抛出了错误。

Sklearn不允许执行这种精细操作。

唯一的可能性是提供随机的或类似于Kmeans++将实现的第7个特征值。

因此,基本上你可以估计一个好的价值如下:

import numpy as np
from sklearn.cluster import KMeans
nb_clust = 10
# your data
X = np.random.randn(7*1000).reshape( (1000,7) )   
# your 6col centroids  
cent_6cols = np.random.randn(6*nb_clust).reshape( (nb_clust,6) ) 
# artificially fix your centroids
km = KMeans( n_clusters=10 )
km.cluster_centers_ = cent_6cols
# find the points laying on each cluster given your initialization
initial_prediction = km.predict(X[:,0:6])
# For the 7th column you'll provide the average value 
# of the points laying on the cluster given by your partial centroids    
cent_7cols = np.zeros( (nb_clust,7) )
cent_7cols[:,0:6] = cent_6cols
for i in range(nb_clust):
init_7th = X[ np.where( initial_prediction == i ), 6].mean()
cent_7cols[i,6] =  init_7th
# now you have initialized the 7th column with a Kmeans ++ alike 
# So now you can use the cent_7cols as your centroids
truekm = KMeans( n_clusters=10, init=cent_7cols )

这是k-均值的非常的非标准变体。因此,你不能指望sklearn为每一种奇异的变化做好准备。这会让其他人的sklearn变慢。

事实上,您的方法更像某些回归方法(预测聚类中心的最后一个值(,而不是聚类。我还怀疑,与仅使用其他6个维度将最后一个值设置为分配给聚类中心的所有点的平均值相比,结果会好得多。尝试根据最近的中心对数据进行分区(忽略最后一列(,然后将最后一列设置为指定数据的算术平均值。

然而,sklearn是开源

所以获取源代码,并修改k-means。随机初始化最后一个组件,运行k表示只更新最后一列。以这种方式修改它很容易,但很难设计一个高效的API来允许通过琐碎的参数进行此类自定义-使用源代码在他的级别进行自定义。

最新更新