gpt4 book ai didi

python - 如何自动计算集群数量?

转载 作者:行者123 更新时间:2023-11-30 08:51:55 24 4
gpt4 key购买 nike

我一直在使用以下脚本:

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.cluster import KMeans
from sklearn.metrics import adjusted_rand_score
import textract
import os

folder_to_scan = '/media/sf_Documents/clustering'
dict_of_docs = {}

# Gets all the files to scan with textract
for root, sub, files in os.walk(folder_to_scan):
for file in files:
full_path = os.path.join(root, file)
print(f'Processing {file}')
try:
text = textract.process(full_path)
dict_of_docs[file] = text
except Exception as e:
print(e)


vectorizer = TfidfVectorizer(stop_words='english')
X = vectorizer.fit_transform(dict_of_docs.values())

true_k = 3
model = KMeans(n_clusters=true_k, init='k-means++', max_iter=100, n_init=1)
model.fit(X)

print("Top terms per cluster:")
order_centroids = model.cluster_centers_.argsort()[:, ::-1]
terms = vectorizer.get_feature_names()
for i in range(true_k):
print("Cluster %d:" % i,)
for ind in order_centroids[i, :10]:
print(' %s' % terms[ind],)

它扫描作为扫描文档的图像文件夹,提取文本,然后对文本进行聚类。我知道事实上有 3 种不同类型的文档,因此我将 true_k 设置为 3。但是如果我有一个未知文档的文件夹,其中可能有 1 到 100 个不同的文档类型,该怎么办?

最佳答案

这是一个不稳定的领域,因为在没有任何真实标签的情况下很难衡量聚类算法的“好”程度。为了进行自动选择,您需要一个指标来比较 KMeans 对于不同 n_clusters 值的执行情况。

一个流行的选择是剪影分数。您可以找到更多有关它的详细信息here 。这是 scikit-learn 文档:

The Silhouette Coefficient is calculated using the mean intra-cluster distance (a) and the mean nearest-cluster distance (b) for each sample. The Silhouette Coefficient for a sample is (b - a) / max(a, b). To clarify, b is the distance between a sample and the nearest cluster that the sample is not a part of. Note that Silhouette Coefficient is only defined if number of labels is 2 <= n_labels <= n_samples - 1.

因此,您只能计算 n_clusters >= 2 的轮廓分数(不幸的是,鉴于您的问题描述,这可能是您的限制)。

这就是您在虚拟数据集上使用它的方式(然后您可以将其调整为您的代码,这只是为了有一个可重现的示例):

from sklearn.datasets import load_iris
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score

iris = load_iris()
X = iris.data

sil_score_max = -1 #this is the minimum possible score

for n_clusters in range(2,10):
model = KMeans(n_clusters = n_clusters, init='k-means++', max_iter=100, n_init=1)
labels = model.fit_predict(X)
sil_score = silhouette_score(X, labels)
print("The average silhouette score for %i clusters is %0.2f" %(n_clusters,sil_score))
if sil_score > sil_score_max:
sil_score_max = sil_score
best_n_clusters = n_clusters

这将返回:

The average silhouette score for 2 clusters is 0.68
The average silhouette score for 3 clusters is 0.55
The average silhouette score for 4 clusters is 0.50
The average silhouette score for 5 clusters is 0.49
The average silhouette score for 6 clusters is 0.36
The average silhouette score for 7 clusters is 0.46
The average silhouette score for 8 clusters is 0.34
The average silhouette score for 9 clusters is 0.31

因此,您将拥有 best_n_clusters = 2 (注意:实际上,Iris 有三个类别...)

关于python - 如何自动计算集群数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54936518/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com