Flexible Naive Bayes classifiers using scipy.stats distributions
Project description
StatsNB - Flexible Naive Bayes Classifiers
Naive Bayes classifiers using any scipy.stats continuous distribution, fully compatible with scikit-learn.
What is StatsNB?
GaussianNB from scikit-learn assumes Gaussian (normal) distributions. But what if your data follows a different distribution?
StatsNB lets you use all continuous distributions from scipy.stats, see scipy.stats documentation.
When to Use StatsNB?
Use StatsNB when:
- Your data is heavy-tailed (try
torcauchydistributions) - Your data is bounded (try
betadistribution) - Your data is strictly positive and skewed (try
gammaorlognorm) - You want to experiment with different distributions
Installation
pip install statsnb
Quick Start
from statsnb import StatsNB
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
# Load data
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
# Use t-distribution (more robust than Gaussian)
clf = StatsNB(dist='t')
clf.fit(X_train, y_train)
print(f"Accuracy: {clf.score(X_test, y_test):.3f}")
# Try different distributions
for dist in ['norm', 't', 'laplace', 'cauchy']:
clf = StatsNB(dist=dist)
clf.fit(X_train, y_train)
print(f"{dist:10s}: {clf.score(X_test, y_test):.3f}")
Advanced Features
Fix Parameters During Fitting
# Fix degrees of freedom for t-distribution
clf = StatsNB(dist='t', fit_args={'fdf': 5})
clf.fit(X_train, y_train)
# Fix location for exponential distribution
clf = StatsNB(dist='expon', fit_args={'floc': 0})
clf.fit(X_train, y_train)
Custom Priors
# Specify class prior probabilities
clf = StatsNB(dist='gamma', priors=[0.2, 0.3, 0.5])
clf.fit(X_train, y_train)
Use Method of Moments (Faster)
# MM is much faster than MLE for some distributions
clf = StatsNB(dist='norm', fit_args={'method': 'MM'})
clf.fit(X_train, y_train)
API Compatibility
StatsNB is fully compatible with scikit-learn:
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import GridSearchCV
# Use in pipelines
pipe = Pipeline([
('scaler', StandardScaler()),
('clf', StatsNB())
])
pipe.fit(X_train, y_train)
# Use in grid search
param_grid = {'clf__dist': ['norm', 't', 'laplace']}
grid = GridSearchCV(pipe, param_grid, cv=5)
grid.fit(X_train, y_train)
print(f"Best distribution: {grid.best_params_}")
Performance Note
Unlike GaussianNB which uses analytical solutions, StatsNB uses numerical optimization (MLE) to fit distributions. This makes it:
-
~5-10x slower than
GaussianNB -
No incremental learning (
partial_fitnot supported)- Numerical optimization doesn't support online updates
-
No sample weights
scipy.stats.fit()doesn't support weighted MLE
-
More flexible - supports any distribution
-
Still efficient for most real-world datasets
However, for norm,expon,gamma,laplace, parameter inference can be done with Methods of Momentum, thus support all features of GaussianNB.
Therefore, We provide MomentNB, which supports all GaussianMB features
- Incremental learning
- Sample weights
- Variance Smoothing
- And much faster!
However,MomentNB only supports norm,expon,gamma,laplace, setting dist= other distributions will result in an error.
Examples
See the examples/ directory for more.
Requirements
- Python ≥ 3.8
- numpy ≥ 1.20.0
- scipy ≥ 1.7.0
- scikit-learn ≥ 1.0.0
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT License - see LICENSE file for details.
Citation
If you use StatsNB in your research, please cite:
@software{statsnb2025,
author = {Your Name},
title = {StatsNB: Flexible Naive Bayes Classifiers},
year = {2025},
url = {https://github.com/yourusername/statsnb}
}
Acknowledgments
- Built on top of scikit-learn and scipy
- Inspired by the flexibility needs of real-world data science
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file statsnb-0.2.0.tar.gz.
File metadata
- Download URL: statsnb-0.2.0.tar.gz
- Upload date:
- Size: 18.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2c21d6093ab9ab1e25c8c9cceb20bd632fb6a3e81fbcd4a99bf94f859ef913a6
|
|
| MD5 |
2762519f98a3619f2818ec019b6d320c
|
|
| BLAKE2b-256 |
71c4d798276135a9283d20051301a38c07597d1e2994c4eacfaa87e281114806
|
File details
Details for the file statsnb-0.2.0-py3-none-any.whl.
File metadata
- Download URL: statsnb-0.2.0-py3-none-any.whl
- Upload date:
- Size: 16.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e4c921d9798073e5828df281c4c2b5da530782c22f2d5ee368aab533e2969ab6
|
|
| MD5 |
8e7b92137b20ff7b1f4f0b54f15f6f82
|
|
| BLAKE2b-256 |
29cbd547e53d310b97e4128cee63964430e1ebbeebef60602cc2896eb34683d4
|