Skip to main content

A lightweight package that analyzes multiple metrics directly from confusion matrix efficiently

Project description

cm2metrics

A lightweight package that analyzes multiple metrics directly from confusion matrix.

Features

  • Get all classes parsing results in native Dataframe format
  • Print all classes or specified class parsing summary in friendly format
  • Only requires numpy and pandas, without relying on other machine learning packages.
  • Easy to use with a few APIs
  • Supports 16 metrics for each class:
    • tp: true positive
    • tn: true negative
    • fp: false positive
    • fn: false negative
    • tpr: true positive rate
    • tnr: true negative rate
    • fpr: false positive rate
    • fnr: false negative rate
    • atc: actual true count
    • afc: actual false count
    • ptc: predict true count
    • pfc: predict false count
    • accruacy
    • precision
    • recall
    • f1

General

  • Version: 0.1
  • Dependency: Python(3.6,3.7.3.8), numpy, pandas

Install

pip install cm2metrics

Use

General use

  1. Generate a confusion matrix
#  use scikitlearn
from sklearn.metrics import confusion_matrix

#cm is ndarray, convert to dataframe
cm = confusion_matrix(true_target, pred_target)
df_cm = pd.DataFrame(cm, index=class_names, columns=class_names)

# or, use a randomly generated confusion matrix(for test)
# see details in cm_test.py
class_names = {0:"class0", 1:"class1", 2:"class2"}
df_cm = pd.DataFrame([(1,2,3),(4,5,6),(7,8,9)])
df_cm.rename(index=class_names, columns=class_names, inplace=True)
  1. Init a confusion matrix parser
from cm2metrics.parse_cm import  ConfusionMatrixParser
cm_parser = ConfusionMatrixParser(df_cm)
  1. Parse the confusion matrix
# parsing result(cm_parsed) is a dataframe
cm_parsed = cm_parser.parse_confusion_matrix()
print(cm_parsed)  

Sample output:

        tp  fp  tn  fn       tpr       fpr       tnr       fnr  atc  afc  ptc  pfc  accuracy  precision    recall        f1
class0   1  28  11   5  0.166667  0.717949  0.282051  0.833333    6   39   12   33  0.644444   0.083333  0.166667  0.111111
class1   5  20  10  10  0.333333  0.666667  0.333333  0.666667   15   30   15   30  0.555556   0.333333  0.333333  0.333333
class2   9  12   9  15  0.375000  0.571429  0.428571  0.625000   24   21   18   27  0.466667   0.500000  0.375000  0.428571


# get class0 true positive
tp = cm_parsed.loc["class0"].at["tp"]

Print parsing summary in friendly format

# print one class summary by name using class_name parameter
cm_parser.print_summary(class_name="class0")

# print one class summary by index in confusion matrix using class_index parameter
cm_parser.print_summary(class_index=0)

# print all classes summary by not specifying parameters
cm_parser.print_summary()

Sample output for class0 summary:
Summary for class0
TP: 1
TN: 28
FP: 11
FN: 5
TPR: 0.167
TNR: 0.718
FPR: 0.282
FNR: 0.833
Actual true count: 6
Actual false count: 39
Predict true count: 12
Predict false count: 33
Accuracy: 0.644
Precision: 0.083
Recall: 0.167
F1: 0.111

License

MIT license.

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

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

Source Distribution

cm2metrics-0.1.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

cm2metrics-0.1-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file cm2metrics-0.1.tar.gz.

File metadata

  • Download URL: cm2metrics-0.1.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.8.2

File hashes

Hashes for cm2metrics-0.1.tar.gz
Algorithm Hash digest
SHA256 79d7fe3830e3afe331aaaede7aa824c2b80f84300f3c5e5859139459e2a02785
MD5 457cbd54fe4ec41d5660c9b1c0aa75f3
BLAKE2b-256 5b86d2be8a6565b545f8ec15528a724b166ed06a09b7a7831cd4e9140cc56f77

See more details on using hashes here.

File details

Details for the file cm2metrics-0.1-py3-none-any.whl.

File metadata

  • Download URL: cm2metrics-0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.8.2

File hashes

Hashes for cm2metrics-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 030632279e9674f3523272c2ba05dcf7b3bcd5a16318024b574abb0788b1d223
MD5 5eb6123f0a4b2848c425b08d9ccfb74f
BLAKE2b-256 329749a6c18a95b7c2a65586dec66b9fa4b987fdcfcce36630e2051c3bcfa2d2

See more details on using hashes here.

Supported by

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