Calculating the completeness score using sklearn in Python

An entirely complete clustering is one where each cluster has information that directs a place toward a similar class cluster. Completeness portrays the closeness of the clustering algorithm to this (completeness_score) perfection. 

This metric is autonomous of the outright values of the labels. A permutation of the cluster label values won’t change the score value in any way.

sklearn.metrics.completeness_score()

Syntax: sklearn.metrics.completeness_score(labels_true, labels_pred)

Parameters:

  • labels_true:<int array, shape = [n_samples]>: It accepts the ground truth class labels to be used as a reference.
  • labels_pred: <array-like of shape (n_samples,)>: It accepts the cluster labels to evaluate.

Returns: completeness score between 0.0 and 1.0. 1.0 stands for perfectly completeness labeling.



Switching label_true with label_pred will return the homogeneity_score.

Example 1:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:

0.8471148027985769

Example 2: Perfectly completeness:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Impoting 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)

chevron_right


Output:

1.0

Example 3: Non-perfect labeling that further split classes into more clusters can be perfectly completeness:



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output:

0.9999999999999999

Example 4: Include samples from different classes don’t make for completeness labeling:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output:

0.0

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.