Misurare il drift con PSI e KS
Quantifichi lo spostamento della distribuzione per ogni feature.
Misurare il drift con PSI e KS è una lezione MLOps Academy gratuita su CoddyKit. Questa è la lezione 2 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento MLOps Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso MLOps Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
From Hunch to Number
Saying a feature feels different is not enough. You need a number that says how much its distribution moved. Two trusted tools are PSI and the KS test. 📏
What PSI Measures
The Population Stability Index compares a feature now against training, bin by bin. The bigger the gap in each bin, the higher the score.
Reading PSI Values
A common rule of thumb: PSI below 0.1 means stable, 0.1 to 0.25 means moderate shift, and above 0.25 means significant drift worth acting on.
How PSI Is Computed
For each bin, PSI takes the percent difference times the log ratio of the two percentages, then sums across all bins into one score.
import numpy as np
def psi(expected, actual):
return np.sum((actual - expected) * np.log(actual / expected))Binning Comes First
PSI needs both samples placed into the same bins, often deciles of the training data. Same edges for both sides keep the comparison fair.
edges = np.quantile(reference, np.linspace(0, 1, 11))
exp = np.histogram(reference, edges)[0] / len(reference)
act = np.histogram(current, edges)[0] / len(current)What the KS Test Measures
The Kolmogorov-Smirnov test finds the largest gap between two cumulative distributions. That maximum distance is the KS statistic.
KS in One Line
SciPy gives you the KS test directly. A small p-value means the two samples likely come from different distributions, hinting at drift.
from scipy.stats import ks_2samp
stat, pvalue = ks_2samp(reference, current)
drifted = pvalue < 0.05PSI vs KS
PSI gives a single severity score great for dashboards. KS gives a statistical p-value great for yes-or-no decisions. Many teams track both side by side.
Categorical Features
PSI works naturally on categories, treating each value as its own bin. KS assumes ordered numbers, so reach for chi-square on categorical features instead.
Watch the Sample Size
With huge samples the KS test flags tiny, harmless shifts as significant. Pair its p-value with an effect size like PSI before you raise an alarm.
Per-Feature, Then Roll Up
Run these tests per feature, then summarize. A handy signal is the share of features that drifted, which tells you if the shift is broad or narrow. 📊
Quick Check
Pick the right read on these numbers.
Recap
You turned drift into numbers: PSI scores severity with bins, KS measures the biggest distribution gap. Use both, mind sample size, and pick the right test per feature type. 🎯
Domande Frequenti
La lezione «Misurare il drift con PSI e KS» è gratuita?
Sì — il testo completo di «Misurare il drift con PSI e KS» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso MLOps Academy, passa a CoddyKit PRO. Il corso MLOps Academy include 4 lezioni in totale.
Cosa imparerò in «Misurare il drift con PSI e KS»?
Quantifichi lo spostamento della distribuzione per ogni feature. Eserciti MLOps Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare MLOps Academy?
Non è richiesta alcuna esperienza precedente. MLOps Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 2 di 4.
Quanto tempo richiede la lezione «Misurare il drift con PSI e KS»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione MLOps Academy?
Sì. Ogni lezione MLOps Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Data drift e concept drift
- Misurare il drift con PSI e KS
- Generare report sul drift con Evidently
- Impostare soglie e trigger per il drift