0Pricing
MLOps Academy · 课时

使用 PSI 和 KS 衡量偏移

量化每个特征的分布变化

使用 PSI 和 KS 衡量偏移 是 CoddyKit 上的免费 MLOps Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 MLOps Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 MLOps Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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.05

PSI 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. 🎯

常见问题解答

「使用 PSI 和 KS 衡量偏移」课时是免费的吗?

是的 — 「使用 PSI 和 KS 衡量偏移」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 MLOps Academy 课程的其余内容,请升级到 CoddyKit PRO。 MLOps Academy 课程共包含 4 节课。

「使用 PSI 和 KS 衡量偏移」这节课中我会学到什么?

量化每个特征的分布变化 你通过在浏览器中直接运行的动手代码来练习 MLOps Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 MLOps Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 MLOps Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「使用 PSI 和 KS 衡量偏移」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 MLOps Academy 课中编写并运行代码吗?

能。每节 MLOps Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 数据偏移与概念偏移
  2. 使用 PSI 和 KS 衡量偏移
  3. 使用 Evidently 生成偏移报告
  4. 设置偏移阈值和触发条件
← 返回 MLOps Academy