📜  在Python中使用 sklearn 计算完整性分数

📅  最后修改于: 2022-05-13 01:55:02.984000             🧑  作者: Mango

在Python中使用 sklearn 计算完整性分数

一个完全完整的聚类是每个聚类都有信息将一个地方指向一个相似的类聚类。完整性描述了聚类算法与此 (completeness_score) 完美度的接近程度。

该度量独立于标签的完全值。集群标签值的排列不会以任何方式改变分数值。

sklearn.metrics.completeness_score()

使用 label_pred 切换 label_true 将返回 homogeneity_score。

示例 1:

Python3
# Importing the modules
import pandas as pd 
from sklearn import datasets
from sklearn.cluster import KMeans 
from sklearn.metrics import completeness_score
 
# Loading the data 
digits = datasets.load_digits()
 
# Separating the dependent and independent variables 
Y = digits.target
X = digits.data
 
# Building the clustering model 
kmeans = KMeans(n_clusters = 2) 
 
# Training the clustering model 
kmeans.fit(X) 
 
# Storing the predicted Clustering labels 
labels = kmeans.predict(X) 
 
# Evaluating the performance 
print(completeness_score(Y, labels))


Python3
# Importing the module
from sklearn.metrics.cluster import completeness_score
 
# Evaluating the score
Cscore = completeness_score([0, 1, 0, 1],
                            [1, 0, 1, 0])
print(Cscore)


Python3
# Importing the module
from sklearn.metrics.cluster import completeness_score
 
# Evaluating the score
Cscore = completeness_score([0, 1, 2, 3],
                            [0, 0, 1, 1])
print(Cscore)


Python3
# Importing the module
from sklearn.metrics.cluster import completeness_score
 
# Evaluating the score
Cscore = completeness_score([0, 0, 0, 0],
                            [0, 1, 2, 3])
print(Cscore)


输出:

0.8471148027985769

示例 2:完全完备性:

蟒蛇3

# Importing the module
from sklearn.metrics.cluster import completeness_score
 
# Evaluating the score
Cscore = completeness_score([0, 1, 0, 1],
                            [1, 0, 1, 0])
print(Cscore)


输出:

1.0 

示例 3:将类进一步拆分为更多集群的非完美标记可以是完美的完整性:

蟒蛇3

# Importing the module
from sklearn.metrics.cluster import completeness_score
 
# Evaluating the score
Cscore = completeness_score([0, 1, 2, 3],
                            [0, 0, 1, 1])
print(Cscore)

输出:

0.9999999999999999

示例 4:包含来自不同类别的样本不会用于完整性标记:

蟒蛇3

# Importing the module
from sklearn.metrics.cluster import completeness_score
 
# Evaluating the score
Cscore = completeness_score([0, 0, 0, 0],
                            [0, 1, 2, 3])
print(Cscore)

输出:

0.0