亲和传播聚类的调整兰德指数

Adjusted Rand Index for Affinity Propagation Clustering

我想计算亲和力传播的 Adjusted Rand Index。我有一个包含这样的句子的数据集:

Youtube
Facebook
Whatsapp
Open Youtube

我的亲和传播代码如下:

import nltk, string 
from sklearn.feature_extraction.text
import TfidfVectorizer from sklearn.cluster
import AffinityPropagation
import pandas as pd

punctuation_map = dict((ord(char), None) for char in string.punctuation) stemmer = nltk.stem.snowball.SpanishStemmer()

def stem_tokens(tokens):
    return [stemmer.stem(item) for item in tokens]

def normalize(text):
    return stem_tokens(nltk.word_tokenize(text.lower().translate(punctuation_map)))

vectorizer = TfidfVectorizer(tokenizer=normalize)

def get_clusters(sentences):
    tf_idf_matrix = vectorizer.fit_transform(sentences)
    similarity_matrix = (tf_idf_matrix * tf_idf_matrix.T).A
    affinity_propagation = AffinityPropagation(affinity="precomputed", damping=0.5)
    affinity_propagation.fit(similarity_matrix)

    labels = affinity_propagation.labels_
    global cluster_centers
    cluster_centers = affinity_propagation.cluster_centers_indices_


    tagged_sentences = zip(sentences, labels)
    clusters = {}

    for sentence, cluster_id in tagged_sentences:
        clusters.setdefault(sentences[cluster_centers[cluster_id]], []).append(sentence)
        #print(len(sentence))

    return clusters

#csv file filename = "/home/ubuntu/data/local_queries.csv" df = pd.read_csv(filename, header = None)

sentences = df.iloc[:, 0].values.tolist()

clusters = get_clusters(sentences) print() for cluster in clusters:
    print(cluster, ':')
    for element in clusters[cluster]:
        print('  - ', element)

对于 ARI,我们需要实际标签和预测标签。我没有实际的标签,因为我的数据集中只有句子。谁能解释一下在这种情况下我应该如何计算 ARI?

ARI 是一种外部 评估措施。

只能用于比较两个结果。通常,您将聚类与已知的 class 标签进行比较,以测试实施是否有效。

如果您只有一个结果且没有 "true" 标签,您 无法 计算 ARI。

在这种情况下,您只能使用内部评估措施,但它们存在所有缺点。