Pearson correlation metric

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

Pearson correlation metric is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Use Pearson correlation coefficient as evaluation metric for semantic similarity regression tasks, selecting best checkpoint by correlation rather than loss

How to use this skill

Skill file

Pearson Correlation Metric

Overview

For regression tasks where the target represents similarity or relatedness scores, Pearson correlation measures linear agreement between predictions and labels — invariant to scale and shift. This makes it better than MSE for model selection: a model with correct ranking but wrong scale scores poorly on MSE but perfectly on Pearson r. Standard metric for STS benchmarks and similarity competitions.

Quick Start

import numpy as np
from scipy import stats

def pearson_score(y_true, y_pred):
    return stats.pearsonr(y_true, y_pred)[0]

# HuggingFace Trainer integration
def compute_metrics(eval_pred):
    predictions, labels = eval_pred
    predictions = predictions.reshape(-1)
    return {'pearson': np.corrcoef(predictions, labels)[0][1]}

trainer = Trainer(
    model=model, args=args,
    train_dataset=train_ds, eval_dataset=val_ds,
    compute_metrics=compute_metrics,
)

Workflow

  1. Define compute_metrics returning Pearson r from predictions and labels
  2. Pass to HuggingFace Trainer or compute manually in a training loop
  3. Use Pearson r for checkpoint selection: if score > best_score: save_model()
  4. Report final CV score as mean Pearson r across folds

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.