Optimized Rounder
An efficient optimizer for finding optimal thresholds in ordinal classification problems. This package uses Optuna for efficient threshold search with support for cross-validation and multiple evaluation metrics.
Installation
pip install optimized-rounder
Features
- Threshold Optimization: Find optimal thresholds to convert continuous predictions to discrete classes
- Multiple Metrics: Support for various evaluation metrics including quadratic kappa, linear kappa, RMSE, accuracy, and F1 scores
- Cross-Validation: Built-in support for K-fold and stratified cross-validation
- Efficient Search: Uses Optuna for efficient Bayesian optimization of thresholds
- Comprehensive Evaluation: Evaluate models using multiple metrics simultaneously
Quick Start
from oprounder import OptimizedRounder
import numpy as np
from sklearn.metrics import cohen_kappa_score
# Generate synthetic data
np.random.seed(42)
n_classes = 4
n_samples = 1000
y_true = np.random.randint(0, n_classes, size=n_samples)
output = y_true + np.random.normal(0, 0.9, size=n_samples) # dummy model output
# Initialize and fit the optimizer
rounder = OptimizedRounder(n_classes=n_classes, n_trials=100)
rounder.fit(output, y_true)
# Get the optimal thresholds
print(f'Optimal thresholds: {rounder.thresholds}')
# Make predictions
y_pred = rounder.predict(output)
kappa = cohen_kappa_score(y_true, y_pred, weights='quadratic')
print(f'Optimal Quadratic kappa: {kappa:.4f}')
y_pred_default = rounder.apply_thresholds(output, rounder.default_thresholds) # [0.5, 1.5, 2.5, 3.5]
kappa_default = cohen_kappa_score(y_true, y_pred_default, weights='quadratic')
print(f'Default Quadratic kappa: {kappa_default:.4f}')
Advanced Usage
With Cross-Validation
# Use 5-fold stratified cross-validation
rounder = OptimizedRounder(
n_classes=4,
n_trials=200,
cv=5,
stratified=True,
metric='quadratic_kappa',
verbose=True
)
rounder.fit(output, y_true)
print(f'CV Results: {rounder.cv_results_}')
Using Different Metrics
# Optimize for F1 weighted score
rounder = OptimizedRounder(
n_classes=4,
n_trials=200,
metric='f1_weighted'
)
rounder.fit(output, y_true)
# Comprehensive evaluation
output_val = y_true + np.random.normal(0, 0.8, size=n_samples)
metrics = rounder.evaluate(output_val, y_true)
for metric_name, value in metrics.items():
print(f"{metric_name}: {value:.4f}")
API Reference
OptimizedRounder
OptimizedRounder(
n_classes=None,
n_trials=200,
cv=None,
stratified=True,
metric='quadratic_kappa',
verbose=False,
random_state=42
)
Parameters
- n_classes: Number of target classes (0, 1, 2, ..., n_classes-1)
- n_trials: Number of optimization trials for Optuna
- cv: Number of cross-validation folds or a CV splitter object
- stratified: Whether to use stratified CV (only when cv is an integer)
- metric: Metric to optimize ('quadratic_kappa', 'linear_kappa', 'rmse', 'accuracy', 'f1_macro', 'f1_weighted', 'f1_micro')
- verbose: Whether to display Optuna's optimization progress
- random_state: Random seed for reproducibility
Methods
- fit(X, y): Find optimal thresholds using Optuna optimization
- predict(X): Convert continuous predictions to discrete classes using optimal thresholds
- fit_predict(X, y): Train the optimizer and return predictions in one step
- coefficients(): Get the optimal thresholds found during training
- evaluate(X, y): Evaluate the model on multiple metrics
Use Cases
- Regression to Classification: Convert regression outputs to discrete classes
- Ordinal Classification: Optimize thresholds for ordinal targets
- Ensemble Calibration: Calibrate probability outputs from ensemble models
- Competition Metrics: Optimize directly for competition metrics like quadratic kappa
License
This project is licensed under the MIT License - see the LICENSE file for details.
Metadata
Release files for optimized-rounder 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| optimized_rounder-0.1.3.tar.gz | 7.4 kB | Without provenance | Details for optimized_rounder-0.1.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| optimized_rounder-0.1.3-py3-none-any.whl | Python 3 | none | any | Details for optimized_rounder-0.1.3-py3-none-any.whl |
Total release size: 14.3 kB
Release files / optimized_rounder-0.1.3.tar.gz
| Download URL | optimized_rounder-0.1.3.tar.gz |
|---|---|
| Size | 7.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
103de637635f24633b6dcbbc590cdd3a336b8af8e247d6319b0e5f85e13b83ba
|
|
BLAKE2b-256 checksum How to use checksums |
f14bcab100588360b605d7bd79a9466c241979afee9449fc91a5970cfee1bfdd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.10.14
|
Release files / optimized_rounder-0.1.3-py3-none-any.whl
| Download URL | optimized_rounder-0.1.3-py3-none-any.whl |
|---|---|
| Size | 6.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c02db2c9ca52b883a7063d8d8a737388c2ef224779c3b3a1e52a14ec41af46d5
|
|
BLAKE2b-256 checksum How to use checksums |
ef45853d069bd1101ed74fe34c2870cdee83a9a452511b1621dd2b2e4152ad95
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.10.14
|
Security for optimized-rounder 0.1.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.