Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

threshold_optimizer

This python library allows you to conveniently evaluate predicted probablilities during a binary classification task by presenting you with the optimum probability thresholds.

Introduction

Classification tasks in machine learning involves models or algorithms learning to assign class lables to elements of a set. Binary Classification is the process of assigning elements to two class labels on the basis of a classification rule. Some of the examples of binary classification includes classifying mails under 'spam' or 'not a spam', medical tests ('cancer detected' or 'cancer not detected') and churn prediction ('churn' or 'not').

Evaluating machine learning models is an important aspect of building models. These evaluations are done using classification metrics, the metrics used depends on the nature of the problem you're solving and the cost of falsely predicted values. Some of these metrics include: confusion matrix, accuracy, precision, recall, F1 score and ROC curve. However these decisions by the metrics are based on a set threshold.

For instance, in order to map a probability representation from logistic regression to a binary category, you must define a classification threshold (also called the decision threshold). In say a cancer patient classification, a value above that threshold indicates "Patient has cancer"; a value below indicates "Patient does not have cancer." It is tempting to assume that the classification threshold should always be 0.5, but thresholds are problem-dependent, and are therefore values that you must tune.

This library allows you to output the optimum threshold value for the metric you're using to evaluate your classification model. The metrics for which you can get the optimum threshold outputs are:

Accuracy

F1 Score

Recall

Specificity

Precision

Requirements

scikit-learn == 0.24.0

pandas == 0.25.1

numpy == 1.17.1

Installation

Usage

Code To Follow

from threshold_optimizer import ThresholdOptimizer
import pandas as pd
import numpy as np
from sklearn import datasets
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split

# load data sets
X, y = datasets.load_breast_cancer(return_X_y=True)

# train, val, test splits
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.25, random_state=1)

# fit estimator
clf = LogisticRegression(random_state=0).fit(X_train, y_train)
# predict probabilities
predicted_probabilities = clf.predict_proba(X_val)

# apply optimization
thresh_opt = ThresholdOptimizer(
        y_score = predicted_probabilities,
        y_true = y_val
    )

# optimize for accuracy and f1 score
thresh_opt.optimize_metrics(
        metrics=['accuracy', 'f1'],
        verbose=True
    )

# display results
print(thresh_opt.optimized_metrics)

# access threshold per metric
accuracy_threshold = thresh_opt.optimized_metrics.accuracy.best_threshold
f1_threshold = thresh_opt.optimized_metrics.f1.best_threshold

# use best accuracy threshold for test set to convert probabilities to classes
predicted_probabilities = clf.predict_proba(X_test)
classes = np.where(predicted_probabilities[:,1], > accuracy_threshold, 1, 0)
print(classes)

Key Terminologies

:TODO

Release files for threshold-optimizer 0.0.1a2

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

Source distribution (sdist)

Source distribution for threshold-optimizer 0.0.1a2
File Size Uploaded
threshold_optimizer-0.0.1a2.tar.gz 5.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for threshold-optimizer 0.0.1a2
File Interpreter ABI Platform
threshold_optimizer-0.0.1a2-py3-none-any.whl Python 3 none any Details

Total release size:12.7 kB

Release files / threshold_optimizer-0.0.1a2.tar.gz

Download URL threshold_optimizer-0.0.1a2.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
468ece1e1f944d97470da0bc7af2fadd2ccf172cf0c3b6dc0dadc95d5eb89252
BLAKE2b-256 checksum
How to use checksums
de1a5b11c75290779bc0efaedd7a58fb653e4c54b9befa80f86ab8f3716aea33
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.0.0.post20200309 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.6

Release files / threshold_optimizer-0.0.1a2-py3-none-any.whl

Download URL threshold_optimizer-0.0.1a2-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d26eeb3fdd5cfdaea841c2eaff10890d0514a904c954474b8024532c0e47d1cc
BLAKE2b-256 checksum
How to use checksums
536fb597cedad110ee871668b1a422f5c7b225e27a732a46c7a07edc1643ed13
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.0.0.post20200309 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.6

Release history Release notifications | RSS feed

This release

0.0.1a2 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