Skip to main content

B-Cubed Metrics

A simple Python package to calculate B-Cubed precision, recall, and F1-score for clustering evaluation.

What are B-Cubed Metrics

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. 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 = \frac{1}{\sum {elements}}\sum_{i=1}^n {\frac{(count ; of ; element)^2}{count ; of ; all ; elements ; in ; cluster}} $$

$$ Recall = \frac{1}{\sum {elements}}\sum_{i=1}^n {\frac{(count ; of ; element)^2}{count ; of ; total ; elements ; from ; this ; category}} $$

$$ F-score = \frac{1}{k}\sum_{i=1}^n {\frac{2\times Precision(C)_k \times Recall(C)_k}{Precision(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.

Installation and Use

Download the package from any terminal using:
pip install bcubed-metrics
To use the B-Cubed class you need to import it and provide 2 dictionaries - one for the predicted clustering, and one for the ground truth clustering (actual labels):
from bcubed_metrics.bcubed import Bcubed

predicted_clustering = [
            {'blue': 4, 'red': 2, 'green': 1},
            {'blue': 2, 'red': 2, 'green': 3},
            {'blue': 1, 'red': 5},
            {'blue': 1, 'red': 2, 'green': 3}
        ]

ground_truth_clustering = {'blue': 8, 'red': 11, 'green': 7}

bcubed = Bcubed(predicted_clustering=predicted_clustering, ground_truth_clustering=ground_truth_clustering)

metrics = bcubed.get_metrics() # returns all metrics as dictionary

bcubed.print_metrics() # prints all metrics

Metadata

Release files for bcubed-metrics 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bcubed-metrics 1.0.1
File Size Uploaded
bcubed_metrics-1.0.1.tar.gz 4.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for bcubed-metrics 1.0.1
File Interpreter ABI Platform
bcubed_metrics-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 9.6 kB

Release files / bcubed_metrics-1.0.1.tar.gz

Download URL bcubed_metrics-1.0.1.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
b844bbedf124789dcfa5bd47b6f4cd035a683ed807c8731bc6352105e5329319
BLAKE2b-256 checksum
How to use checksums
032eb4a9579578ad73eb2350eb18fe422ea4f046975f499b7fc07749003a67cd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.5

Release files / bcubed_metrics-1.0.1-py3-none-any.whl

Download URL bcubed_metrics-1.0.1-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c783d4f4c82b4550f6e2ec57d686495ce323df3275bb96e385cfcc8aa24e13dc
BLAKE2b-256 checksum
How to use checksums
0b43b75c9908abcb520eb52c159268dfe628d57974c82d14f0f4914e86049683
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.5

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page