A scikit-learn compatible package for classification with monetary outcomes
Project description
Monetary Classifier
A scikit-learn compatible package for classification with monetary outcomes. This package allows you to optimize classifiers based on the actual monetary value of correct and incorrect predictions rather than just accuracy or other traditional metrics.
Installation
# Basic installation
pip install monetary-classifier
# With gradient boosting integrations
pip install monetary-classifier[xgboost,lightgbm,catboost]
# For development
pip install monetary-classifier[dev]
Or install directly from the repository:
git clone https://github.com/yourusername/monetary-classifier.git
cd monetary-classifier
pip install -e .
Quick Start
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from monetary_classifier import MonetaryClassifier, create_lending_monetary_values
# Prepare your data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# Define monetary values for each prediction outcome
monetary_values = {
'tp': 100, # Profit from true positive (e.g., approving a good loan)
'tn': 0, # No gain/loss from true negative (e.g., denying a bad loan)
'fp': -1000, # Loss from false positive (e.g., approving a bad loan)
'fn': -100, # Loss from false negative (e.g., denying a good loan)
}
# Or use the built-in function for lending scenarios
monetary_values = create_lending_monetary_values(
loan_amount=10000,
interest_rate=0.15,
default_cost_factor=1.0
)
# Create and train the monetary classifier
clf = MonetaryClassifier(
base_estimator=RandomForestClassifier(),
monetary_values=monetary_values,
use_monetary_weights=True # Use monetary values as weights during training
)
clf.fit(X_train, y_train)
# Evaluate the monetary outcome
monetary_score = clf.monetary_score(X_test, y_test)
print(f"Monetary outcome: ${monetary_score:.2f}")
# Get detailed breakdown
summary = clf.get_monetary_summary(X_test, y_test)
print(summary)
Key Features
- Monetary value-based classification: Optimize for business value rather than just accuracy
- Automatic threshold optimization: Find the optimal decision threshold to maximize monetary outcome
- Cost-sensitive learning: Use monetary values as weights during model training
- Direct objective optimization: Custom loss functions for gradient boosting models (XGBoost, LightGBM, CatBoost)
- scikit-learn compatible: Works with existing scikit-learn pipelines and models
- Comprehensive evaluation: Detailed monetary analysis of model performance
Examples
Check the examples directory for detailed usage examples:
lending_example.py: Basic example for a lending scenarioweighted_model_example.py: Advanced example with different weighting strategies
License
This project is licensed under the MIT License - see the LICENSE file for details.
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 monetary_classifier-0.1.0.tar.gz.
File metadata
- Download URL: monetary_classifier-0.1.0.tar.gz
- Upload date:
- Size: 14.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
71ec7326684935d20f35923ebcacbb10bcfc9f2ef8b54ab01a8b514b248de476
|
|
| MD5 |
10bb1131eed0c3c3cbe271a37ab7fda3
|
|
| BLAKE2b-256 |
d6fa9a07b6d33975f052b0d1452f4fcf27c72554cdca71894ef6e67af779e649
|
File details
Details for the file monetary_classifier-0.1.0-py3-none-any.whl.
File metadata
- Download URL: monetary_classifier-0.1.0-py3-none-any.whl
- Upload date:
- Size: 15.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c0456c50d926f4d8df36ef4a113154ff4dffb9fb3d602fc2b923d3019c366dc3
|
|
| MD5 |
54dabe7f3363f1af423037852cc895d3
|
|
| BLAKE2b-256 |
826d6d0ee9fa1c935e50f829a961acd7572fdec7622ea115588a7376b909edc3
|