A Python library for filtering and stabilizing probability predictions from multiple models inspired by Rick and Morty
Project description
Probability Stasis Filter Library
A Python library for filtering and stabilizing probability predictions from multiple models.
Installation
Clone this repository and install locally or just pip install it:
pip install .
Usage
Basic Example
from probability_stasis import StasisFilter
import numpy as np
# Define a simple model
class DummyModel:
def predict_proba(self, X):
return np.random.random((len(X), 2))
# Initialize filter and model
filter = StasisFilter(threshold=0.1, window_size=3)
filter.add_model(DummyModel())
# Make predictions
X = np.random.random((5, 10))
predictions = filter.predict(X)
print(predictions[0]) # Filtered probabilities from model 0
API Documentation
StasisFilter
Main class for probability stasis filtering.
Parameters
threshold(float): Maximum allowed probability deviation (default: 0.1)window_size(int): Number of predictions to track (default: 3)models(List[PredictionModel], optional): Initial list of prediction models
Methods
add_model(model): Add a new prediction modelpredict(X): Make filtered predictionsreset_history(): Clear prediction history
PredictionModel
Abstract base class for models. Must implement:
predict_proba(X): Return probability predictions as numpy array
Features
- Maintains probability stability within threshold
- Supports multiple models simultaneously
- Rolling window of predictions
- Automatic stabilization when thresholds are exceeded
example_usage.py
import numpy as np
from probability_stasis import StasisFilter
class SimpleModel:
def predict_proba(self, X):
return np.random.random((len(X), 2))
def main():
# Initialize filter with two models
filter = StasisFilter(threshold=0.05, window_size=4)
filter.add_model(SimpleModel())
filter.add_model(SimpleModel())
# Generate sample data
X = np.random.random((3, 5))
# Make multiple predictions to demonstrate stasis
for _ in range(5):
preds = filter.predict(X)
print(f"\nPredictions:")
for model_idx, probs in preds.items():
print(f"Model {model_idx}: {probs}")
if __name__ == "__main__":
main()
How to Use the Library
-
Installation:
- Place all files in the directory structure as shown
- Install using
pip install .from theprobability_stasisdirectory
-
Basic Usage:
- Create a model class that inherits from
PredictionModelor implementspredict_proba - Initialize
StasisFilterwith desired threshold and window size - Add models using
add_model() - Call
predict()with input data
- Create a model class that inherits from
-
Key Features:
- The filter maintains a history of predictions per model
- Checks if new predictions deviate beyond the threshold from recent history
- Stabilizes predictions by averaging when stasis is broken
- Handles multiple models independently
-
Customization:
- Adjust
thresholdfor sensitivity to probability changes - Modify
window_sizefor how many past predictions to consider - Add as many models as needed
- Adjust
This implementation provides a robust, reusable library that can be integrated with various prediction models while ensuring probability stability across predictions. The example shows how to use it with dummy models, but you can replace these with real ML models (e.g., from scikit-learn) that implement the predict_proba method.
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 probability_stasis-0.1.0.tar.gz.
File metadata
- Download URL: probability_stasis-0.1.0.tar.gz
- Upload date:
- Size: 2.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9d2ec8c3b976b4055ec95f8b71f5a647a47382a5f58963791c28d74e11d1ab8b
|
|
| MD5 |
598449b9c5481142dc38ef55fc73ad2b
|
|
| BLAKE2b-256 |
fc1f91780faf21be294cd7dd1dea20f43ac045f67aa9289863fd5e5e46097604
|
File details
Details for the file probability_stasis-0.1.0-py3-none-any.whl.
File metadata
- Download URL: probability_stasis-0.1.0-py3-none-any.whl
- Upload date:
- Size: 2.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fbcbd52844d8319bb6e3bb433cc7dec333c7660a64d55c69278aba9531e9eb79
|
|
| MD5 |
39fb760f6f1a40a7cea4c32ccf4089ce
|
|
| BLAKE2b-256 |
28946673cdd1f1ec694297179c35eb8628d62c3343b3ea90d18a887937fdd979
|