Blog

Calculating BCubed metrics for clustering tasks

· 4 min read

Originally published in Open AI on Medium: read it on Medium

Wooden spheres spilling out of a clear tube on the left and blue cubes tumbling into a tube on the right, against a pale grey background.
Photo by New Material on Unsplash

Introduction

The B-Cubed algorithm was first introduced by Bagga, A. and Baldwin B. (1998) in their paper on Entity-Based Cross-Document Coreferencing Using the Vector Space Model. The algorithm compares a predicted clustering with a ground truth (or gold standard) clustering through element-wise precision and recall scores. BCubed metrics provide a robust evaluation method for comparing predicted clusters with the ground truth, particularly in cases of overlapping or soft clusters.

For each element, the predicted and ground truth clusters containing the element are compared, and then the mean over all elements is taken. The B-Cubed algorithm can be useful in unsupervised techniques where the cluster labels are not available, because unlike macro-averaged metrics, it focuses on element-wise operations.

From the paper, two simple equations were devised calculating precision and recall scores for the predicted clustering:

Precision=1∑elements∑i=1n(count of element)2count of all elements in cluster
Recall=1∑elements∑i=1n(count of element)2count of total elements from this category
F-score=1k∑i=1n2×Precision(C)k×Recall(C)kPrecision(C)k+Recall(C)k

where n above denotes the number of categories in the cluster and k is the number of predicted clusters. Precision(C)_k and Recall(C)_k are the ‘partial’ precision and recalls for each cluster.

Visual Example

The image below consists of four clusters as predicted from a model, where each data point is assigned a colour representing a “label” or “category”: Blue, Red and Green.

Four predicted clusters drawn as circles. Each mixes blue, red and green points: cluster 1 has 4 blue, 2 red and 1 green; cluster 2 has 2 blue, 2 red and 3 green; cluster 3 has 1 blue and 5 red; cluster 4 has 1 blue, 2 red and 3 green.
Predicted Clustering for 3 Labels (Blue, Red, Green)

The correct clustering however would result in three clusters, instead of four, representing the amount of labels. This is called ground truth clustering. The “Blue” cluster would contain 8 data points, the “Red” would contain 11 and the “Green” would contain 7, summing up to 26 data points in total.

Three ground truth clusters drawn as circles: one of blue points, one of green points and one of red points.
Ground Truth Clustering for 3 Labels (Blue, Red, Green)

The tables below show the element-wise precision, recall, and f-score, which are calculated based on the provided formulas and focusing on the summation part to show the element-wise operations. For precision, the denominator equals the total elements in the predicted cluster, while for recall the denominator is the total elements for that label (also equal to the ground truth cluster for that element).

Table 1: Precision
Cluster / LabelBlueRedGreen
Cluster 142/7 = 2.28622/7 = 0.57112/7 = 0.143
Cluster 222/7 = 0.57122/7 = 0.57132/7 = 1.286
Cluster 312/6 = 0.16752/6 = 4.16702/6 = 0
Cluster 412/6 = 0.16722/6 = 0.66732/6 = 1.500
Table 2: Recall
Cluster / LabelBlueRedGreen
Cluster 142/8 = 2.00022/11 = 0.36412/7 = 0.143
Cluster 222/8 = 0.50022/11 = 0.36432/7 = 1.286
Cluster 312/8 = 0.12552/11 = 2.27302/7 = 0
Cluster 412/8 = 0.12522/11 = 0.36432/7 = 1.286
Table 3: F-score
Cluster / LabelBlueRedGreen
Cluster 10.5330.2220.143
Cluster 20.2670.2220.429
Cluster 30.1430.5880
Cluster 40.1430.2350.462
Element-wise metrics: Precision (Table 1), Recall (Table 2), F-score (Table 3)

By summing the above values in each table for each cluster separately, and following the formulas, the BCubed metrics are:

MetricCluster 1Cluster 2Cluster 3Cluster 4
Partial Precision42/7 + 22/7 + 12/7 = 332/7 + 22/7 + 22/7 = 2.42812/6 + 0 + 52/6 = 4.33312/6 + 32/6 + 22/6 = 2.333
Partial Recall42/8 + 12/7 + 22/11 = 2.50622/8 + 32/7 + 22/11 = 2.14912/8 + 0 + 52/11 = 2.39812/8 + 32/7 + 22/11 = 1.774
Partial F-score2.732.2793.0882.015
Partial Precision, Recall and F-score for cluster classes calculated with the BCubed method
BCubed metricsValue
Precision0.465 / 46.5%
Recall0.339 / 33.9%
F-score0.392 / 39.2%
Final BCubed metrics

BCubed Python Package

The bcubed-metrics package can help calculate the metrics only by providing 2 dictionaries; the predicted clustering and ground truth clustering.

You can find the documentation here along with a usage example. To install the package simply use pip:

pip install bcubed-metrics