A robust measure of multiclass classifier performance for observations with individual weights.
Project description
Weighted MCC: Robust Multiclass Metrics
Weighted MCC is a Python package that implements robust performance metrics for binary and multiclass classification tasks where individual observations have different importance weights.
Based on the paper describing Weighted Matthews Correlation Coefficient (MCC), this package provides a mathematically sound way to evaluate classifiers in high-stakes domains like medical imaging (segmentation) and autonomous driving, where some errors are costlier than others.
Features
- Weighted Binary MCC: Calculate MCC for binary tasks with per-sample weights.
- Multiclass Extensions:
- ECC (Extended Correlation Coefficient): A robust multiclass generalization of MCC.
- MPC (Multivariate Pearson Correlation): Variants (MPC1, MPC2) derived from covariance matrix theory.
- Robustness Analysis:
- Compute theoretical upper bounds on metric stability given weight perturbations ($\epsilon$).
- Theoretically proven stability ensures metrics are not brittle to small weight changes.
- Efficient Implementation: Vectorized operations using NumPy for high performance on large datasets.
Installation
pip install weighted_mcc
Usage
Binary Classification
import numpy as np
from weighted_mcc import weighted_mcc
y_true = np.array([1, 0, 1, 1, 0])
y_pred = np.array([1, 0, 0, 1, 1])
weights = np.array([2.0, 1.0, 5.0, 1.0, 1.0]) # 3rd sample is critical
# Calculate Weighted MCC
score = weighted_mcc(y_true, y_pred, weights)
print(f"Weighted MCC: {score:.4f}")
Multiclass Classification
For multiclass, inputs should generally be one-hot encoded for the mathematical functions, or use helper utilities if provided (check documentation).
import numpy as np
from weighted_mcc import extended_corr_coef, mpc_trace_ratio
# Example: 3 classes, 4 samples
y_true = np.array([[1,0,0], [0,1,0], [0,0,1], [1,0,0]])
y_pred = np.array([[1,0,0], [0,0,1], [0,0,1], [0,1,0]])
weights = np.array([1.0, 1.0, 2.0, 1.0])
# Extended Correlation Coefficient
ecc = extended_corr_coef(y_true, y_pred, weights)
print(f"ECC: {ecc:.4f}")
# Multivariate Pearson Correlation (Trace Ratio)
mpc1 = mpc_trace_ratio(y_true, y_pred, weights)
print(f"MPC1: {mpc1:.4f}")
Robustness Check
Verify if your metric score is stable under potential weight noise (e.g., if weights are subjective).
from weighted_mcc import calculate_multiclass_stability_bound
epsilon = 0.01 # Max potential deviation in weights
bound = calculate_multiclass_stability_bound(y_true, y_pred, weights, epsilon, metric_type='ECC')
print(f"Score could vary by at most ±{bound:.4f} given epsilon={epsilon}")
Hardware Requirements
- Minimum: Modern CPU, 8GB RAM (small datasets).
- Recommended: Multi-core CPU with AVX2, 16GB+ RAM for large image segmentation tasks.
References
This project incorporates research from the following paper:
- Weighted MCC: A Robust Measure of Multiclass Classifier Performance for Observations with Individual Weights Rommel Cortez, Bala Krishnamoorthy arXiv:2512.20811
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file weighted_mcc-0.1.0.tar.gz.
File metadata
- Download URL: weighted_mcc-0.1.0.tar.gz
- Upload date:
- Size: 10.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ad0be16a0e18c5febf01b656027c0efefdfb7223099337e923488dd3bbf80be9
|
|
| MD5 |
f4b001630fb5d7c528764683c39cde24
|
|
| BLAKE2b-256 |
feb48e833c017cbe05cb6ded846e9a7f6a9e90cec270290fa19ca705915a97cc
|
Provenance
The following attestation bundles were made for weighted_mcc-0.1.0.tar.gz:
Publisher:
publish.yml on kuslavicek/weighted_mcc
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
weighted_mcc-0.1.0.tar.gz -
Subject digest:
ad0be16a0e18c5febf01b656027c0efefdfb7223099337e923488dd3bbf80be9 - Sigstore transparency entry: 868321592
- Sigstore integration time:
-
Permalink:
kuslavicek/weighted_mcc@8285721497f41345ae1fc955245b75dde40f6cf1 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/kuslavicek
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@8285721497f41345ae1fc955245b75dde40f6cf1 -
Trigger Event:
release
-
Statement type:
File details
Details for the file weighted_mcc-0.1.0-py3-none-any.whl.
File metadata
- Download URL: weighted_mcc-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
40f8f448f4050943f04ec28f99cc511dadb75b32524a0cb3bd127c00dc71f939
|
|
| MD5 |
ac1ed2a715e7f11d6170e095ad63a783
|
|
| BLAKE2b-256 |
d33b195b326ebff2615025c874159055827e5b536614085a105dc556de551aed
|
Provenance
The following attestation bundles were made for weighted_mcc-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on kuslavicek/weighted_mcc
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
weighted_mcc-0.1.0-py3-none-any.whl -
Subject digest:
40f8f448f4050943f04ec28f99cc511dadb75b32524a0cb3bd127c00dc71f939 - Sigstore transparency entry: 868321593
- Sigstore integration time:
-
Permalink:
kuslavicek/weighted_mcc@8285721497f41345ae1fc955245b75dde40f6cf1 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/kuslavicek
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@8285721497f41345ae1fc955245b75dde40f6cf1 -
Trigger Event:
release
-
Statement type: