谢邀,用python写过,ks和lift都在里面,spss没有写过,逻辑上类似,FYI.
def decilestats(true, pred, group=10):
'''
to generate the decile Stats(Ks, Gain, Lift@decile, Lift@total).
Parameters:
true: actual target values
pred: prediction values
group: number of deciles
return:
prob_min: minimum probablity
prob_max: maximum probablity
#pop: number of cases in its group
#num1: number of positive(events/responses..)
#num0: number of negative(non events/responses..)
%pop: percentage of cases
%num1: percentage of positive
%num0: percentage of negative
cum%pop: cumulative percentage of cases
cum%num1: cumulative percentage of positive
cum%num0: cumulative percentage of negative
ks: the degree of separation between the positive and negative distributions
lift@decile: lift in each group, (%num1/%pop)
lift@total: lift total, (cum%num1/cum%pop)
return: decile stats result
#example:
dstats = decilestats(y_true, y_pred)
'''
# dataframe:
pdict = pd.DataFrame({"true": true, "pred": pred})
pdict['decile'] = pd.qcut(pdict['pred'], group, duplicates='drop', labels=False)
report = pdict.groupby(['decile']).agg({'true': {'#pop': 'count', '#num1': 'sum'},
'pred': {'prob_min': 'min', 'prob_max': 'max'}})
report.columns = report.columns.droplevel(0) # drop level0('true', 'pred')
report.sort_index(ascending=False, inplace=True) # sort index
# counts
report['#num0'] = report['#pop'] - report['#num1']
# percent
report['%pop'] = report['#pop'] / sum(report['#pop'])
report['%num1'] = report['#num1'] / sum(report['#num1'])
report['%num0'] = report['#num0'] / sum(report['#num0'])
# cumulative
report['cum%pop'] = report['%pop'].cumsum()
report['cum%num1'] = report['%num1'].cumsum()
report['cum%num0'] = report['%num0'].cumsum()
# result
report['ks'] = report['cum%num1'] - report['cum%num0']
report['lift@decile'] = report['%num1'] / report['%pop']
report['lift@total'] = report['cum%num1'] / report['cum%pop']
return report.round(3)
希望有所帮助~