skills/tabular/regression-to-ordinal-thresholding/SKILL.md
Converts regression predictions to ordinal classes by optimizing bin thresholds to maximize Quadratic Weighted Kappa.
npx skillsauth add wenmin-wu/ds-skills tabular-regression-to-ordinal-thresholdingInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
When the target is ordinal (e.g., 0-3 rating) but you train as regression, you need optimal thresholds to bin continuous predictions back into classes. Naive rounding loses performance. Instead, optimize thresholds directly against the evaluation metric (e.g., QWK) using Nelder-Mead or percentile-based binning.
import numpy as np
from scipy.optimize import minimize
from sklearn.metrics import cohen_kappa_score
def qwk(y_true, y_pred):
return cohen_kappa_score(y_true, y_pred, weights='quadratic')
def optimize_thresholds(y_true, y_pred, n_classes=4):
"""Find optimal thresholds to maximize QWK."""
def loss(coef):
bins = [-np.inf] + sorted(coef.tolist()) + [np.inf]
y_binned = np.digitize(y_pred, bins[1:-1])
return -qwk(y_true, y_binned)
initial = np.arange(1, n_classes) - 0.5 # e.g., [0.5, 1.5, 2.5]
result = minimize(loss, initial, method='nelder-mead')
return sorted(result.x.tolist())
# Usage
thresholds = optimize_thresholds(y_val, val_preds)
bins = [-np.inf] + thresholds + [np.inf]
final_preds = np.digitize(test_preds, bins[1:-1])
data-ai
Scaled Pinball Loss (SPL) metric for evaluating quantile forecasts, normalized by mean absolute successive differences of training data
data-ai
Walk backward through a time series and multiplicatively rescale segments when jumps exceed a fraction of the running mean to correct data collection anomalies
testing
Transform forecasting target to next/current ratio minus one so that optimizing MAE or squared error implicitly minimizes SMAPE
tools
Convert point forecasts to prediction intervals by scaling with logit-transformed quantile ratios passed through a Normal CDF