群体稳定性指标PSI(Population Stability Index)是衡量模型的预测值与实际值偏差大小的指标。
PSI = sum((实际占比-预期占比)* ln(实际占比/预期占比))
举例:
比如训练一个logistic回归模型,预测时候会有个概率输出p。
测试集上的输出设定为p1吧,将它从小到大排序后10等分,如0-0.1,0.1-0.2,…。
现在用这个模型去对新的样本进行预测,预测结果叫p2,按p1的区间也划分为10等分。
实际占比就是p2上在各区间的用户占比,预期占比就是p1上各区间的用户占比。
意义就是如果模型跟稳定,那么p1和p2上各区间的用户应该是相近的,占比不会变动很大,也就是预测出来的概率不会差距很大。
一般认为PSI小于0.1时候模型稳定性很高,0.1-0.25一般,大于0.25模型稳定性差,建议重做。
PS:除了按概率值大小等距十等分外,还可以对概率排序后按数量十等分,两种方法计算得到的psi可能有所区别但数值相差不大。
以上转自:https://blog.csdn.net/Rango_lhl/article/details/81388051
以下用自己最近做的一个project的代码做个例子:
Load libraries and data
import pandas as pd
import numpy as np
import math
import re
# sample data
df = pd.read_csv('dev.csv')
# holdout data(without target variable)
dfo = pd.read_csv('oot0.csv')
Define PSI function
def psi(bench, comp, group):
"""
bench: sample[variable]
comp: holdout[variable]
group: how many groups with in the variable
suggestion: group=max(2,min((len(set(df[var_name]))),10))
at least 2,
at max 10,
so if continuous variable, it will be maximum at 10
and if categorical variable with less than 10 cats, it will be number of categories