Skip to main content

Threshold-Based and Iterative Threshold-Based Naive Bayes classifier

Project description

itbnb — Threshold-Based Naive Bayes

Threshold-Based Naïve Bayes (Tb-NB) is a reformulation of the classical Naïve Bayes classifier for binary classification, where the final decision is taken by comparing a continuous score against a data-driven decision threshold instead of relying on posterior probabilities. An optional iterative refinement procedure (iTb-NB) can be enabled to adapt the decision boundary locally around uncertain regions. The implementation follows the scikit-learn API and is designed to be used as a drop-in classifier in standard pipelines.


Overview

In classical Naïve Bayes, posterior probabilities are often poorly calibrated due to the violation of the conditional independence assumption. While these probabilities are useful for ranking observations, their direct interpretation are often unreliable.

Tb-NB addresses this issue by:

  • computing a continuous log-likelihood ratio score;
  • learning an optimal decision threshold τ directly from the data via cross-validation;
  • optionally refining the threshold locally using an iterative procedure.

Main features

  • Binary Naïve Bayes classifier with threshold-based decision rule
  • Cross-validated threshold optimization via ThresholdOptimizer
  • Multiple optimization criteria (accuracy, F1, MCC, balanced error, etc.)
  • Optional iterative refinement near the decision boundary (iTb-NB)
  • Fully compatible with scikit-learn (fit, predict, decision_function)
  • Works with dense or sparse (CSR) feature matrices
  • Designed for use in pipelines and model selection workflows

Installation

Clone the repository and install in editable mode:

git clone https://github.com/francescotiddia/itbnb.git
cd itbnb
pip install -e .

Basic usage

import numpy as np
from scipy.sparse import csr_matrix
from itbnb import TbNB

# Example binary Bag-of-Words matrix
X = csr_matrix([
    [1, 0, 1],
    [0, 1, 0],
    [1, 1, 0],
    [0, 0, 1],
])
y = np.array([1, 0, 1, 0])

# Initialize Tb-NB with automatic threshold optimization
clf = TbNB(
    alpha=1.0,
    optimize_threshold=True,
    criterion="balanced_error",
    K=5,
)

clf.fit(X, y)

# Predictions and scores
y_pred = clf.predict(X)
scores = clf.decision_function(X)

Threshold optimization

When optimize_threshold=True, the decision threshold τ is selected by cross-validation using the ThresholdOptimizer.

The optimizer evaluates a grid of candidate thresholds and selects the one that optimizes a chosen metric.

Available criteria include:

  • accuracy
  • precision
  • recall
  • specificity
  • fpr
  • fnr
  • f1
  • mcc
  • misclassification_error
  • balanced_error

The selected threshold is stored in the fitted classifier as threshold_.


Iterative refinement (iTb-NB)

If iterative=True, Tb-NB applies an iterative refinement procedure after the global threshold has been selected.

At each iteration:

  1. observations close to the current threshold are selected;
  2. local class score densities are estimated;
  3. a refined threshold is computed from their intersection;
  4. the procedure continues until convergence or lack of data.

The result is a sequence of local decision rules that improve classification in regions of class overlap.


References

Romano, M., Contu, G., Mola, F., & Conversano, C. (2023). Threshold-based Naïve Bayes classifier. Advances in Data Analysis and Classification, 18, 325–361. https://doi.org/10.1007/s11634-023-00536-8

Romano, M., Zammarchi, G., & Conversano, C. (2024). Iterative Threshold-Based Naïve Bayes Classifier. Statistical Methods & Applications, 33, 235–265. https://doi.org/10.1007/s10260-023-00721-1

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

itbnb-0.1.0.tar.gz (25.3 kB view details)

Uploaded Source

Built Distribution

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

itbnb-0.1.0-py3-none-any.whl (26.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: itbnb-0.1.0.tar.gz
  • Upload date:
  • Size: 25.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for itbnb-0.1.0.tar.gz
Algorithm Hash digest
SHA256 15ec7af7b30a989c2c490a9c592229c8f284d1c574eeadfc881f75f8b797628e
MD5 0fb44ee9d3991feee6f310f1c72c1eec
BLAKE2b-256 e6113d5372a0fa1ba57eecee0605fb4d65125a1925798b81e9ae54535dd19929

See more details on using hashes here.

Provenance

The following attestation bundles were made for itbnb-0.1.0.tar.gz:

Publisher: publish.yml on francescotiddia/itbnb

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: itbnb-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 26.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for itbnb-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bf6367dbd9f738a374499d40c89b58fd2f44f2200b6bb442aebdc2a04a5cc9e2
MD5 d1a94f6f17db0dccea056713514c75a4
BLAKE2b-256 6c9ee6500a4e51d352e8917bbbf2e296cb73fc6ee8e470db8ca4d0f83e9076f1

See more details on using hashes here.

Provenance

The following attestation bundles were made for itbnb-0.1.0-py3-none-any.whl:

Publisher: publish.yml on francescotiddia/itbnb

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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