本文介绍了每次在Python scikit-learn中运行后,聚类结果的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆句子,我想使用scikit-learn光谱聚类对它们进行聚类.我已经运行了代码并毫无问题地得到了结果.但是,每次运行它,我都会得到不同的结果.我知道这是启动问题,但我不知道如何解决.这是我在句子上运行的代码的一部分:

I have a bunch of sentences and I want to cluster them using scikit-learn spectral clustering. I've run the code and get the results with no problem. But, every time I run it I get different results. I know this is the problem with initiation but I don't know how to fix it. This is my a part of my code that runs on sentences:

vectorizer = TfidfVectorizer(norm='l2',sublinear_tf=True,tokenizer=tokenize,stop_words='english',charset_error="ignore",ngram_range=(1, 5),min_df=1)
X = vectorizer.fit_transform(data)
# connectivity matrix for structured Ward
connectivity = kneighbors_graph(X, n_neighbors=5)
# make connectivity symmetric
connectivity = 0.5 * (connectivity + connectivity.T)
distances = euclidean_distances(X)
spectral = cluster.SpectralClustering(n_clusters=number_of_k,eigen_solver='arpack',affinity="nearest_neighbors",assign_labels="discretize")
spectral.fit(X)

数据是句子列表.每次运行代码,我的聚类结果都会不同.如何使用光谱聚类获得一致的结果.我对Kmean也有同样的问题.这是我给Kmean的代码:

Data is a list of sentences. Everytime the code runs, my clustering results differs. How can I get consistent results using Spectral clustering. I also have the same problem with Kmean. This is my code for Kmean:

vectorizer = TfidfVectorizer(sublinear_tf=True,stop_words='english',charset_error="ignore")
X_data = vectorizer.fit_transform(data)
km = KMeans(n_clusters=number_of_k, init='k-means++', max_iter=100, n_init=1,verbose=0)
km.fit(X_data)

感谢您的帮助.

推荐答案

使用k-means时,您要在KMeans中设置random_state参数(请参见).将其设置为int或 RandomState 实例.

When using k-means, you want to set the random_state parameter in KMeans (see the documentation). Set this to either an int or a RandomState instance.

km = KMeans(n_clusters=number_of_k, init='k-means++',
            max_iter=100, n_init=1, verbose=0, random_state=3425)
km.fit(X_data)

这很重要,因为k均值不是确定性算法.它通常以某种随机的初始化过程开始,这种随机性意味着不同的运行将在不同的点开始.为伪随机数生成器播种可确保对于相同的种子,此随机性将始终相同.

This is important because k-means is not a deterministic algorithm. It usually starts with some randomized initialization procedure, and this randomness means that different runs will start at different points. Seeding the pseudo-random number generator ensures that this randomness will always be the same for identical seeds.

我不确定光谱聚类的例子.从参数的文档中: 伪随机数生成器,用于在eigen_solver == 'amg'时并通过K-Means初始化来初始化lobpcg本征向量.在这些情况下,OP的代码似乎不包含在其中,尽管设置参数可能值得一试.

I'm not sure about the spectral clustering example though. From the documentation on the random_state parameter: "A pseudo random number generator used for the initialization of the lobpcg eigen vectors decomposition when eigen_solver == 'amg' and by the K-Means initialization." OP's code doesn't seem to be contained in those cases, though setting the parameter might be worth a shot.

这篇关于每次在Python scikit-learn中运行后,聚类结果的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 20:41