群体稳定度指标PSI
生活随笔
收集整理的这篇文章主要介绍了
群体稳定度指标PSI
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
群体稳定性指标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 variablesuggestion: 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 10and if categorical variable with less than 10 cats, it will be number of categories """# get the number of rows of the sample and holdoutben_len=len(bench);comp_len=len(comp);# sort the valuesbench.sort();comp.sort();psi_cut=[];# calculate sample_size / number_groupsn=int(math.floor(ben_len/group));# from 1 to number_groupsfor i in range(1,group):# as bench has been sorted, we can get the spot of cutting edge values# for example i=1 ⇒ bench[1] will be the first lowercut# and bench[1*n] or bench[-1] will be the first uppercut# count how many values in each benchlowercut=bench[(i-1)*n+1];# when i < groupif i!=group:uppercut=bench[(i*n)];ben_cnt=n;# when i==group else:uppercut=bench[-1];ben_cnt=ben_len-group*(n-1)# count for values in corresponding intervals in holdout datasetcomp_cnt = len([i for i in comp if i > lowercut and i<=uppercut]);# calculate percentage of counts_in_interval / total_valuesben_pct=(ben_cnt+0.0)/ben_len;comp_pct=(comp_cnt+0.0)/comp_len;# if in the corresponding interval, there is value, calculate the psi for this intervalif comp_pct > 0.0:psi_cut.append((ben_pct-comp_pct)*math.log(ben_pct/(comp_pct)));else:psi_cut.append(0);# sum up all the psi, and check the totalpsi=sum(psi_cut);return psi;Run the psi function to every input variable
list_inputs = list() # get all the input variables for var_name in df.columns:if re.search('^i',var_name):list_inputs.append(var_name) # iter the function through them for var_name in list_inputs:psi_value=psi(bench=list(df[var_name]),comp=list(dfo[var_name]),group=max(2,min((len(set(df[var_name]))),10)));print ("psi for ", var_name, " = ", psi_value)总结
以上是生活随笔为你收集整理的群体稳定度指标PSI的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: java short 转char_byt
- 下一篇: 第一类对象