Skip to main content

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

  1. Monetary value-based classification: Optimize for business value rather than just accuracy
  2. Automatic threshold optimization: Find the optimal decision threshold to maximize monetary outcome
  3. Cost-sensitive learning: Use monetary values as weights during model training
  4. Direct objective optimization: Custom loss functions for gradient boosting models (XGBoost, LightGBM, CatBoost)
  5. scikit-learn compatible: Works with existing scikit-learn pipelines and models
  6. 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 scenario
  • weighted_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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

monetary_classifier-0.1.0.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

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

monetary_classifier-0.1.0-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

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

Hashes for monetary_classifier-0.1.0.tar.gz
Algorithm Hash digest
SHA256 71ec7326684935d20f35923ebcacbb10bcfc9f2ef8b54ab01a8b514b248de476
MD5 10bb1131eed0c3c3cbe271a37ab7fda3
BLAKE2b-256 d6fa9a07b6d33975f052b0d1452f4fcf27c72554cdca71894ef6e67af779e649

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for monetary_classifier-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c0456c50d926f4d8df36ef4a113154ff4dffb9fb3d602fc2b923d3019c366dc3
MD5 54dabe7f3363f1af423037852cc895d3
BLAKE2b-256 826d6d0ee9fa1c935e50f829a961acd7572fdec7622ea115588a7376b909edc3

See more details on using hashes here.

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