skills/nlp/spearman-correlation-callback/SKILL.md
Custom Keras callback that evaluates Spearman correlation on validation data each epoch with optional early stopping.
npx skillsauth add wenmin-wu/ds-skills nlp-spearman-correlation-callbackInstall 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 ranking tasks where Spearman correlation is the evaluation metric, build a custom Keras callback that computes per-target Spearman rho on validation data after each epoch. Enables early stopping on the actual competition metric rather than a proxy loss like MSE.
import numpy as np
from scipy.stats import spearmanr
from tensorflow.keras.callbacks import Callback
class SpearmanCallback(Callback):
"""Evaluate mean Spearman correlation per epoch."""
def __init__(self, val_data, val_targets, patience=3):
super().__init__()
self.val_data = val_data
self.val_targets = val_targets
self.patience = patience
self.best_score = -1
self.wait = 0
def on_epoch_end(self, epoch, logs=None):
preds = self.model.predict(self.val_data, verbose=0)
scores = []
for i in range(self.val_targets.shape[1]):
rho, _ = spearmanr(self.val_targets[:, i], preds[:, i])
scores.append(rho)
mean_rho = np.mean(scores)
print(f" val_spearman: {mean_rho:.4f}")
if mean_rho > self.best_score:
self.best_score = mean_rho
self.wait = 0
else:
self.wait += 1
if self.wait >= self.patience:
self.model.stop_training = True
model.fit(callbacks=[spearman_cb])val_spearman in logs for convergenceModelCheckpoint to save best-scoring weights1e-7 * np.random.randn) if targets have many tiesdata-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