Calculating BCubed metrics for clustering tasks
· 4 min read
Originally published in Open AI on Medium: read it on Medium

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:
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.
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.
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).
| Cluster / Label | Blue | Red | Green |
|---|---|---|---|
| Cluster 1 | 42/7 = 2.286 | 22/7 = 0.571 | 12/7 = 0.143 |
| Cluster 2 | 22/7 = 0.571 | 22/7 = 0.571 | 32/7 = 1.286 |
| Cluster 3 | 12/6 = 0.167 | 52/6 = 4.167 | 02/6 = 0 |
| Cluster 4 | 12/6 = 0.167 | 22/6 = 0.667 | 32/6 = 1.500 |
| Cluster / Label | Blue | Red | Green |
|---|---|---|---|
| Cluster 1 | 42/8 = 2.000 | 22/11 = 0.364 | 12/7 = 0.143 |
| Cluster 2 | 22/8 = 0.500 | 22/11 = 0.364 | 32/7 = 1.286 |
| Cluster 3 | 12/8 = 0.125 | 52/11 = 2.273 | 02/7 = 0 |
| Cluster 4 | 12/8 = 0.125 | 22/11 = 0.364 | 32/7 = 1.286 |
| Cluster / Label | Blue | Red | Green |
|---|---|---|---|
| Cluster 1 | 0.533 | 0.222 | 0.143 |
| Cluster 2 | 0.267 | 0.222 | 0.429 |
| Cluster 3 | 0.143 | 0.588 | 0 |
| Cluster 4 | 0.143 | 0.235 | 0.462 |
By summing the above values in each table for each cluster separately, and following the formulas, the BCubed metrics are:
| Metric | Cluster 1 | Cluster 2 | Cluster 3 | Cluster 4 |
|---|---|---|---|---|
| Partial Precision | 42/7 + 22/7 + 12/7 = 3 | 32/7 + 22/7 + 22/7 = 2.428 | 12/6 + 0 + 52/6 = 4.333 | 12/6 + 32/6 + 22/6 = 2.333 |
| Partial Recall | 42/8 + 12/7 + 22/11 = 2.506 | 22/8 + 32/7 + 22/11 = 2.149 | 12/8 + 0 + 52/11 = 2.398 | 12/8 + 32/7 + 22/11 = 1.774 |
| Partial F-score | 2.73 | 2.279 | 3.088 | 2.015 |
| BCubed metrics | Value |
|---|---|
| Precision | 0.465 / 46.5% |
| Recall | 0.339 / 33.9% |
| F-score | 0.392 / 39.2% |
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