Skip to content
Snippets Groups Projects
Commit fa50e688 authored by Lars Buitinck's avatar Lars Buitinck
Browse files

rename ari_score adjusted_rand_score

parent aca6fa0e
Branches
Tags
No related merge requests found
......@@ -295,7 +295,7 @@ Clustering metrics
:toctree: generated/
:template: function.rst
metrics.ari_score
metrics.adjusted_rand_score
metrics.homogeneity_completeness_v_measure
metrics.homogeneity_score
metrics.completeness_score
......
......@@ -358,34 +358,34 @@ chance normalization**::
>>> labels_true = [0, 0, 0, 1, 1, 1]
>>> labels_pred = [0, 0, 1, 1, 2, 2]
>>> metrics.ari_score(labels_true, labels_pred) # doctest: +ELLIPSIS
>>> metrics.adjusted_rand_score(labels_true, labels_pred) # doctest: +ELLIPSIS
0.24...
One can permute 0 and 1 in the predicted labels and rename `2` by `3` and get
the same score::
>>> labels_pred = [1, 1, 0, 0, 3, 3]
>>> metrics.ari_score(labels_true, labels_pred) # doctest: +ELLIPSIS
>>> metrics.adjusted_rand_score(labels_true, labels_pred) # doctest: +ELLIPSIS
0.24...
Furthermore, :func:`ari_score` is **symmetric**: swapping the argument
Furthermore, :func:`adjusted_rand_score` is **symmetric**: swapping the argument
does not change the score. It can thus be used as a **consensus
measure**::
>>> metrics.ari_score(labels_pred, labels_true) # doctest: +ELLIPSIS
>>> metrics.adjusted_rand_score(labels_pred, labels_true) # doctest: +ELLIPSIS
0.24...
Perfect labeling is scored 1.0::
>>> labels_pred = labels_true[:]
>>> metrics.ari_score(labels_true, labels_pred)
>>> metrics.adjusted_rand_score(labels_true, labels_pred)
1.0
Bad (e.g. independent labelings) have negative or close to 0.0 scores::
>>> labels_true = [0, 1, 2, 0, 3, 4, 5, 1]
>>> labels_pred = [1, 1, 0, 0, 2, 2, 2, 2]
>>> metrics.ari_score(labels_true, labels_pred) # doctest: +ELLIPSIS
>>> metrics.adjusted_rand_score(labels_true, labels_pred) # doctest: +ELLIPSIS
-0.12...
......
......@@ -56,7 +56,7 @@ def uniform_labelings_scores(score_func, n_samples, n_clusters_range,
return scores
score_funcs = [
metrics.ari_score,
metrics.adjusted_rand_score,
metrics.v_measure_score,
]
......
......@@ -38,7 +38,8 @@ print 'Estimated number of clusters: %d' % n_clusters_
print "Homogeneity: %0.3f" % metrics.homogeneity_score(labels_true, labels)
print "Completeness: %0.3f" % metrics.completeness_score(labels_true, labels)
print "V-measure: %0.3f" % metrics.v_measure_score(labels_true, labels)
print "Adjusted Rand Index: %0.3f" % metrics.ari_score(labels_true, labels)
print "Adjusted Rand Index: %0.3f" % \
metrics.adjusted_rand_score(labels_true, labels)
##############################################################################
# Plot result
......
......@@ -39,7 +39,8 @@ print 'Estimated number of clusters: %d' % n_clusters_
print "Homogeneity: %0.3f" % metrics.homogeneity_score(labels_true, labels)
print "Completeness: %0.3f" % metrics.completeness_score(labels_true, labels)
print "V-measure: %0.3f" % metrics.v_measure_score(labels_true, labels)
print "Adjusted Rand Index: %0.3f" % metrics.ari_score(labels_true, labels)
print "Adjusted Rand Index: %0.3f" % \
metrics.adjusted_rand_score(labels_true, labels)
##############################################################################
# Plot result
......
......@@ -44,7 +44,8 @@ print "Inertia: %f" % km.inertia_
print "Homogeneity: %0.3f" % metrics.homogeneity_score(labels, km.labels_)
print "Completeness: %0.3f" % metrics.completeness_score(labels, km.labels_)
print "V-measure: %0.3f" % metrics.v_measure_score(labels, km.labels_)
print "Adjusted Rand Index: %0.3f" % metrics.ari_score(labels, km.labels_)
print "Adjusted Rand Index: %0.3f" % \
metrics.adjusted_rand_score(labels, km.labels_)
print
print "Raw k-means with random centroid init..."
......@@ -55,7 +56,8 @@ print "Inertia: %f" % km.inertia_
print "Homogeneity: %0.3f" % metrics.homogeneity_score(labels, km.labels_)
print "Completeness: %0.3f" % metrics.completeness_score(labels, km.labels_)
print "V-measure: %0.3f" % metrics.v_measure_score(labels, km.labels_)
print "Adjusted Rand Index: %0.3f" % metrics.ari_score(labels, km.labels_)
print "Adjusted Rand Index: %0.3f" % \
metrics.adjusted_rand_score(labels, km.labels_)
print
print "Raw k-means with PCA-based centroid init..."
......@@ -69,7 +71,8 @@ print "Inertia: %f" % km.inertia_
print "Homogeneity: %0.3f" % metrics.homogeneity_score(labels, km.labels_)
print "Completeness: %0.3f" % metrics.completeness_score(labels, km.labels_)
print "V-measure: %0.3f" % metrics.v_measure_score(labels, km.labels_)
print "Adjusted Rand Index: %0.3f" % metrics.ari_score(labels, km.labels_)
print "Adjusted Rand Index: %0.3f" % \
metrics.adjusted_rand_score(labels, km.labels_)
print
# Plot k-means++ form on a 2D plot using PCA
......
......@@ -85,5 +85,6 @@ print
print "Homogeneity: %0.3f" % metrics.homogeneity_score(labels, mbkm.labels_)
print "Completeness: %0.3f" % metrics.completeness_score(labels, mbkm.labels_)
print "V-measure: %0.3f" % metrics.v_measure_score(labels, mbkm.labels_)
print "Adjusted Rand-Index: %.3f" % metrics.ari_score(labels, mbkm.labels_)
print "Adjusted Rand-Index: %.3f" % \
metrics.adjusted_rand_score(labels, mbkm.labels_)
print
......@@ -9,7 +9,7 @@ from .metrics import confusion_matrix, roc_curve, auc, precision_score, \
precision_recall_curve, explained_variance_score, r2_score, \
zero_one, mean_square_error, hinge_loss
from .cluster import ari_score
from .cluster import adjusted_rand_score
from .cluster import homogeneity_completeness_v_measure
from .cluster import homogeneity_score
from .cluster import completeness_score
......
......@@ -12,6 +12,7 @@ from scipy import comb
import numpy as np
# the exact version if faster for k == 2: use it by default globally in
# this module instead of the float approximate variant
def comb2(n):
......@@ -39,7 +40,7 @@ def check_clusterings(labels_true, labels_pred):
# clustering measures
def ari_score(labels_true, labels_pred):
def adjusted_rand_score(labels_true, labels_pred):
"""Rand index adjusted for chance
The Rand Index computes a similarity measure between two clusterings
......@@ -59,7 +60,7 @@ def ari_score(labels_true, labels_pred):
ARI is a symmetric measure::
ari_score(a, b) == ari_score(b, a)
adjusted_rand_score(a, b) == adjusted_rand_score(b, a)
Parameters
----------
......@@ -80,28 +81,28 @@ def ari_score(labels_true, labels_pred):
Perfectly maching labelings have a score of 1 even
>>> from sklearn.metrics.cluster import ari_score
>>> ari_score([0, 0, 1, 1], [0, 0, 1, 1])
>>> from sklearn.metrics.cluster import adjusted_rand_score
>>> adjusted_rand_score([0, 0, 1, 1], [0, 0, 1, 1])
1.0
>>> ari_score([0, 0, 1, 1], [1, 1, 0, 0])
>>> adjusted_rand_score([0, 0, 1, 1], [1, 1, 0, 0])
1.0
Labelings that assign all classes members to the same clusters
are complete be not always pure, hence penalized::
>>> ari_score([0, 0, 1, 2], [0, 0, 1, 1]) # doctest: +ELLIPSIS
>>> adjusted_rand_score([0, 0, 1, 2], [0, 0, 1, 1]) # doctest: +ELLIPSIS
0.57...
ARI is symmetric, so labelings that have pure clusters with members
coming from the same classes but unnecessary splits are penalized::
>>> ari_score([0, 0, 1, 1], [0, 0, 1, 2]) # doctest: +ELLIPSIS
>>> adjusted_rand_score([0, 0, 1, 1], [0, 0, 1, 2]) # doctest: +ELLIPSIS
0.57...
If classes members are completely split across different clusters, the
assignment is totally incomplete, hence the ARI is very low::
>>> ari_score([0, 0, 0, 0], [0, 1, 2, 3])
>>> adjusted_rand_score([0, 0, 0, 0], [0, 1, 2, 3])
0.0
References
......
import numpy as np
from sklearn.metrics.cluster import ari_score
from sklearn.metrics.cluster import adjusted_rand_score
from sklearn.metrics.cluster import homogeneity_score
from sklearn.metrics.cluster import completeness_score
from sklearn.metrics.cluster import v_measure_score
......@@ -12,12 +12,13 @@ from numpy.testing import assert_array_almost_equal
score_funcs = [
ari_score,
adjusted_rand_score,
homogeneity_score,
completeness_score,
v_measure_score,
]
def assert_raise_message(exception, message, callable, *args, **kwargs):
"""Helper function to test error messages in exceptions"""
try:
......@@ -97,8 +98,8 @@ def test_non_consicutive_labels():
assert_almost_equal(c, 0.42, 2)
assert_almost_equal(v, 0.52, 2)
ari_1 = ari_score([0, 0, 0, 1, 1, 1], [0, 1, 0, 1, 2, 2])
ari_2 = ari_score([0, 0, 0, 1, 1, 1], [0, 4, 0, 4, 2, 2])
ari_1 = adjusted_rand_score([0, 0, 0, 1, 1, 1], [0, 1, 0, 1, 2, 2])
ari_2 = adjusted_rand_score([0, 0, 0, 1, 1, 1], [0, 4, 0, 4, 2, 2])
assert_almost_equal(ari_1, 0.24, 2)
assert_almost_equal(ari_2, 0.24, 2)
......@@ -123,7 +124,7 @@ def test_adjustment_for_chance():
n_runs = 10
scores = uniform_labelings_scores(
ari_score, n_samples, n_clusters_range, n_runs)
adjusted_rand_score, n_samples, n_clusters_range, n_runs)
max_abs_scores = np.abs(scores).max(axis=1)
assert_array_almost_equal(max_abs_scores, [0.02, 0.03, 0.03, 0.02], 2)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment