skills/cv/optimized-ordinal-thresholds/SKILL.md
Uses Nelder-Mead optimization to find per-class decision thresholds that maximize Quadratic Weighted Kappa for regression-to-ordinal conversion.
npx skillsauth add wenmin-wu/ds-skills cv-optimized-ordinal-thresholdsInstall 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.
Regression models output continuous values that must be mapped to ordinal classes. Simple rounding (0.5, 1.5, 2.5...) is suboptimal because class boundaries aren't equidistant and the evaluation metric (often QWK) isn't linear. Nelder-Mead optimization directly searches for the threshold values that maximize QWK on validation data, typically improving the score by 0.01-0.05 over naive rounding.
import numpy as np
from functools import partial
from scipy.optimize import minimize
from sklearn.metrics import cohen_kappa_score
class OptimizedRounder:
def __init__(self, n_classes=5):
self.coef_ = None
self.n_classes = n_classes
def _kappa_loss(self, coef, X, y):
X_binned = np.digitize(X, coef)
return -cohen_kappa_score(y, X_binned, weights='quadratic')
def fit(self, X, y):
initial = np.arange(0.5, self.n_classes - 0.5) # [0.5, 1.5, 2.5, 3.5]
result = minimize(
partial(self._kappa_loss, X=X, y=y),
initial, method='nelder-mead'
)
self.coef_ = np.sort(result.x)
def predict(self, X):
return np.digitize(X, self.coef_)
# Usage
rounder = OptimizedRounder(n_classes=5)
rounder.fit(val_preds, val_labels)
print(f"Thresholds: {rounder.coef_}")
test_classes = rounder.predict(test_preds)
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