nproc: Neyman-Pearson (NP) Classification Algorithms and NP Receiver Operating Characteristic (NP-ROC) Curves
Python Implementation Authors
Richard Zhao, Yang Feng, Jingyi Jessica Li and Xin Tong
In many binary classification applications, such as disease diagnosis and spam detection, practitioners commonly face the need to limit type I error rate (i.e., the conditional probability of misclassifying a class 0 observation as class 1) so that it remains below a desired threshold. To address this need, the Neyman-Pearson (NP) classification paradigm is a natural choice; it minimizes type II error rate (i.e., the conditional probability of misclassifying a class 1 observation as class 0) while enforcing an upper bound, alpha, on the type I error rate. Although the NP paradigm has a century-long history in hypothesis testing, it has not been well recognized and implemented in classification schemes. Common practices that directly limit the empirical type I error rate to no more than alpha do not satisfy the type I error rate control objective because the resulting classifiers are still likely to have type I error rates much larger than alpha. As a result, the NP paradigm has not been properly implemented for many classification scenarios in practice. In this work, we develop the first umbrella algorithm that implements the NP paradigm for all scoring-type classification methods, including popular methods such as logistic regression, support vector machines and random forests. Powered by this umbrella algorithm, we propose a novel graphical tool for NP classification methods: NP receiver operating characteristic (NP-ROC) bands, motivated by the popular receiver operating characteristic (ROC) curves. NP-ROC bands will help choose in a data adaptive way and compare different NP classifiers.
Details
See details in: https://doi.org/10.1126/sciadv.aao1659
Social media application: https://doi.org/10.1080/01621459.2020.1740711
Usage
npc(x, y, method = ("logistic", "svm", "nb", "rf", "",...), model = None, alpha = 0.05, delta = 0.05, split = 1, split_ratio = 0.5, n_cores = 1, band = False, randSeed = 0)
Arguments
x n * p observation matrix. n observations, p covariates.
y n 0/1 observatons.
method logistic: Logistic Regression.
svm: Support Vector Machine.
nb: Gaussian Naive Bayes.
nb_m: Multinomial Naive Bayes.
rf: Random Forest.
dt: Decision Tree.
keras: Keras Deep Learning. Model must be provided.
model use the specified model instead of method. Default is None and model is created from method.
alpha the desirable upper bound on type I error rate. Default = 0.05.
delta the violation rate of the type I error rate. Default = 0.05.
split the number of splits for the class 0 sample. Default = 1. For ensemble version, choose split > 1.
split_ratio the ratio of splits used for the class 0 sample to train the classifier. Default = 0.5.
n_cores number of cores used for parallel computing. Default = 1.
band whether to generate both lower and upper bounds of type II error rate. Default = False.
randSeed the random seed used in the algorithm.
Example
import numpy as np
import os
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
from nproc import npc
test = npc()
np.random.seed()
# Create a dataset (x,y) with 2 features, binary label and sample size 10000.
n = 10000
x = np.random.normal(0, 1, (n,2))
c = 1+3*x[:,0]
y = np.random.binomial(1, 1/(1+np.exp(-c)), n)
# Call the npc function to construct Neyman-Pearson classifiers.
# The default type I error rate upper bound is alpha=0.05.
fit = test.npc(x, y, 'logistic', n_cores=os.cpu_count())
# Evaluate the prediction of the NP classifier fit on a test set (xtest, ytest).
x_test = np.random.normal(0, 1, (n,2))
c_test = 1+3*x_test[:,0]
y_test = np.random.binomial(1, 1/(1+np.exp(-c_test)), n)
# Calculate the overall accuracy of the classifier as well as the realized
# type I error rate on test data.
# Strictly speaking, to demonstrate the effectiveness of the fit classifier
# under the NP paradigm, we should repeat this experiment many times, and
# show that in 1 - delta of these repetitions, type I error rate is smaller than alpha.
fitted_score = test.predict(fit,x)
print("Accuracy on training set:", accuracy_score(fitted_score[0], y))
pred_score = test.predict(fit,x_test)
print("Accuracy on test set:", accuracy_score(pred_score[0], y_test))
cm = confusion_matrix(y_test, pred_score[0])
print("Confusion matrix:")
print(cm)
tn, fp, fn, tp = cm.ravel()
print("Type I error rate: {:.5f}".format(fp/(fp+tn)))
print("Type II error rate: {:.5f}".format(fn/(fn+tp)))
Metadata
Release files for nproc 1.5.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| nproc-1.5.2.tar.gz | 19.1 kB | Without provenance | Details for nproc-1.5.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| nproc-1.5.2-py3-none-any.whl | Python 3 | none | any | Details for nproc-1.5.2-py3-none-any.whl |
Total release size: 38.4 kB
Release files / nproc-1.5.2.tar.gz
| Download URL | nproc-1.5.2.tar.gz |
|---|---|
| Size | 19.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8dbc61c7ed13c86950f90f2dfb5c40296b22c97ab2ee9845a08474eff4be5365
|
|
BLAKE2b-256 checksum How to use checksums |
97d978d02d08b93d7ca263cc58cfc44c683acb447d12e4ebbc095cfaea7bd0d8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.8
|
Release files / nproc-1.5.2-py3-none-any.whl
| Download URL | nproc-1.5.2-py3-none-any.whl |
|---|---|
| Size | 19.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
694a53a61f761fef12be0e07c560a8833ec6d37d66c9f8ecd475a3e191546187
|
|
BLAKE2b-256 checksum How to use checksums |
8b1a9fceb13509040c67e4813695d19410f9f78fe9fd17da9db0fdc6d2fee358
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.8
|
Security for nproc 1.5.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.