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

Python 部分定义scikit学习K-均值聚类的初始质心,python,machine-learning,scikit-learn,cluster-analysis,k-means,Python,Machine Learning,Scikit Learn,Cluster Analysis,K Means,Scikit文件说明: 初始化方法: “k-means++”:以智能方式为k-means聚类选择初始聚类中心,以加快收敛速度。有关更多详细信息,请参阅k_init中的注释部分 如果一个数组被传递,它应该是形状(n_簇,n_特征)并给出初始中心 我的数据有10个(预测)簇和7个特征。但是,我希望传递10×6形状的数组,即我希望由我预定义的质心的6维,但第7维要使用k-mean++自由迭代。(换句话说,我不想指定初始质心,而是控制6维,只留下一个维来改变初始簇) 我试图通过10x6维,希望它能工作,

Scikit文件说明:

初始化方法:

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

如果一个数组被传递,它应该是形状(n_簇,n_特征)并给出初始中心

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


我试图通过10x6维,希望它能工作,但它只是抛出了错误。

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

唯一的可能性是提供一个随机的第七个特性值,或者类似于Kmeans++所能实现的特性值

因此,基本上,您可以通过以下方式对其进行评估:

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 )

Sklearn不允许您执行此类精细操作

唯一的可能性是提供一个随机的第七个特性值,或者类似于Kmeans++所能实现的特性值

因此,基本上,您可以通过以下方式对其进行评估:

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-均值的一个非常不标准的变化。所以,你不能指望我学会为每一种异国情调做好准备。这会让其他人学习得更慢

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

然而,sklearn是开源的

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

这是k-means的一个非常不标准的变体。所以,你不能指望我学会为每一种异国情调做好准备。这会让其他人学习得更慢

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

然而,sklearn是开源的


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

我得到一个ValueError:功能数量不正确。得到7个特征,预期6>初始预测=km.predict(X[0:6])>X=self.\u检查测试数据(X)>n个特征,预期n个特征)我还检查了X.shape(7455,7)和cent_6cols.shape(10,6)。这就是为什么在传递X进行预测之前必须对X进行切片。我更正了代码,切片应按如下方式进行
initial_prediction=km.predict(X[:,0:6])
我得到了一个ValueError:不正确的特征数。得到7个特征,预期6>初始预测=km.predict(X[0:6])>X=self.\u检查测试数据(X)>n个特征,预期n个特征)我还检查了X.shape(7455,7)和cent_6cols.shape(10,6)。这就是为什么在传递X进行预测之前必须对X进行切片。我更正了代码,切片应该按如下方式进行
initial\u prediction=km.predict(X[:,0:6])