Spearman correlation callback

From wenmin-wu/ds-skills · View interactive version

Spearman correlation callback is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Custom Keras callback that evaluates Spearman correlation on validation data each epoch with optional early stopping.

How to use this skill

Skill file

Spearman Correlation Callback

Overview

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.

Quick Start

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

Workflow

  1. Prepare validation data separately from training generator
  2. Instantiate callback with val features and targets
  3. Pass callback to model.fit(callbacks=[spearman_cb])
  4. Monitor val_spearman in logs for convergence
  5. Combine with ModelCheckpoint to save best-scoring weights

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.