skills/timeseries/class-weighted-multiclass-logloss/SKILL.md
Custom multiclass log-loss that weights per-class contributions by class frequency and domain importance, usable as both training loss and eval metric
npx skillsauth add wenmin-wu/ds-skills timeseries-class-weighted-multiclass-loglossInstall 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.
For imbalanced multiclass problems, standard log-loss is dominated by frequent classes. Weight each class's contribution by (1) its inverse frequency and (2) an optional domain-importance multiplier. Normalize the sum by total weight. Implement as both a Keras/TF training loss and a numpy evaluation metric for consistent optimization.
import numpy as np
import tensorflow as tf
def weighted_logloss_np(y_true_ohe, y_pred, class_weights, class_counts):
"""Numpy version for OOF evaluation.
Args:
y_true_ohe: one-hot encoded ground truth
y_pred: predicted probabilities
class_weights: dict {class_id: importance_weight}
class_counts: per-class sample counts
"""
y_pred = np.clip(y_pred, 1e-15, 1 - 1e-15)
per_class_loss = np.sum(y_true_ohe * np.log(y_pred), axis=0)
weights = np.array([class_weights[c] for c in sorted(class_weights)])
weighted_loss = per_class_loss * weights / class_counts
return -np.sum(weighted_loss) / np.sum(weights)
def weighted_logloss_tf(class_freq):
"""Keras-compatible training loss."""
freq = tf.constant(class_freq, dtype=tf.float32)
def loss(y_true, y_pred):
y_pred = tf.clip_by_value(y_pred, 1e-15, 1 - 1e-15)
per_class = tf.reduce_mean(y_true * tf.math.log(y_pred), axis=0)
return -tf.reduce_mean(per_class / freq)
return loss
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