Skip to main content

Multi-Label Confusion Matrix

Project description

Multi-Label Confusion Matrix

This repository provides a corrected implementation of the multilabel confusion matrix described in the original paper "MLCM: Multi-Label Confusion Matrix" by Heydarian et al.

Issues Fixed

The original repository contained:

  • Deprecated np.int usage which breaks with modern NumPy versions
  • Small inconsistencies between the implementation and the procedure described in the paper

This implementation fixes these issues and provides a working, faithful reference implementation.

License

The original work is licensed under the Creative Commons Attribution 4.0 License (CC BY 4.0). This repository is not affiliated with the original authors. It is an independent implementation based on the algorithm described in the paper.

References

This implementation is based on:

M. Heydarian, T. Doyle, and R. Samavi, "MLCM: Multi-Label Confusion Matrix," IEEE Access, Feb. 2022, DOI: 10.1109/ACCESS.2022.3151048

Usage

Basic Usage

To compute a confusion matrix, use the cm() function:

from mlcm_fixed import cm
import numpy as np

# Create binary encoded labels (one row per instance, one column per class)
true_labels = np.array([[1, 0, 1], [0, 1, 1]])      # 2 instances, 3 classes
predicted_labels = np.array([[1, 0, 0], [0, 1, 1]])

# Get raw and normalized confusion matrices
raw_cm, normalized_cm = cm(true_labels, predicted_labels)
print(raw_cm)
print(normalized_cm)

Label Encoding Example

For instance with True Labels: A, D, E and Predicted Labels: A, C, D

true_labels = [1, 0, 0, 1, 1]      # Classes: A, B, C, D, E
predicted_labels = [1, 0, 1, 1, 0] # Classes: A, B, C, D, E

Case Distinction

The implementation distinguishes three cases based on how true and predicted labels relate:

Case I: P ⊆ T (Predicted is subset of True)

All predicted labels are correct, but some true labels are missing.

Case II: T ⊂ P (True is subset of Predicted)

All true labels are predicted, but some additional incorrect predictions exist.

Case III: Neither P ⊆ T nor T ⊆ P

Some true labels are missing AND some incorrect predictions exist simultaneously.

The algorithms for handling each case are described in the original paper.

Available Functions

  • cm(label_true, label_pred) - Main function, returns raw and normalized confusion matrices
  • conf_mat_case_1(label_true, label_pred) - Confusion matrix for Case I
  • conf_mat_case_2(label_true, label_pred) - Confusion matrix for Case II
  • con_mat_case_3(label_true, label_pred) - Confusion matrix for Case III
  • normalize_conf_matrix(matrix) - Normalize confusion matrix row-wise
  • category_of_instance(label_instance_true, label_instance_pred) - Determine which case an instance belongs to

Example Correction

The original repository would fail on this case:

pred_labels = [1, 0, 0, 1]
true_labels = [1, 1, 1, 0]

Original (incorrect) output:

[[1 0 1 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]
 [0 1 1 0 0]
 [0 0 0 0 0]]

Corrected output:

[[1 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]
 [0 1 1 0 0]
 [0 0 0 0 0]]

Complete Example

Here's a complete example demonstrating all three cases:

from mlcm_fixed import cm, conf_mat_case_1, conf_mat_case_2, con_mat_case_3
import numpy as np

# Example with 100 instances and 5 classes
num_instances = 100
num_classes = 5
true_labels = np.random.randint(2, size=(num_instances, num_classes))
pred_labels = np.random.randint(2, size=(num_instances, num_classes))

# Get confusion matrices
raw_cm, normalized_cm = cm(true_labels, pred_labels)

print("Raw Confusion Matrix:")
print(raw_cm)
print("\nNormalized Confusion Matrix:")
print(normalized_cm)

# You can also get individual case matrices
case1_cm = conf_mat_case_1(true_labels, pred_labels)
case2_cm = conf_mat_case_2(true_labels, pred_labels)
case3_cm = con_mat_case_3(true_labels, pred_labels)

print("\nCase I (P ⊆ T):")
print(case1_cm)
print("\nCase II (T ⊂ P):")
print(case2_cm)
print("\nCase III (neither):")
print(case3_cm)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mlcm_fixed-0.1.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mlcm_fixed-0.1.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file mlcm_fixed-0.1.0.tar.gz.

File metadata

  • Download URL: mlcm_fixed-0.1.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for mlcm_fixed-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ba6f9fefdec2ae535b0a7f927300bc7521940fdb8a972094c881734f8dcdc38b
MD5 85564ff9fe373d56d8ab90095350ff7d
BLAKE2b-256 83db6c190ec2eec01aa0805ca34100e456552f596f59743ba8c6808cfdf6245b

See more details on using hashes here.

File details

Details for the file mlcm_fixed-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: mlcm_fixed-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for mlcm_fixed-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e0f671c70d8a3683b1ddb131852622227bfd1768c0561b94504cd7d3e0602284
MD5 89c4f9d9bec2651fcbf259ea2af3c0a3
BLAKE2b-256 edd9d947476f2c6abe777207a18e07efec0a55eb0fbf7428d8b3981a2eba2a2c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page