python ks值计算_请问spss logistic回归建模后怎么计算ks值?

本文介绍了如何使用Python计算KS值(Kolmogorov-Smirnov检验统计量)以及在逻辑回归建模后评估提升度(Lift)。提供了一个名为`decilestats`的函数,该函数根据预测值和实际目标值生成十等分统计信息,包括KS值、增益和提升度。函数通过将预测概率值分为指定组数并计算每个组内的阳性与阴性分布比例来计算KS值。同时,它还计算了每组和总体的提升度。
摘要由CSDN通过智能技术生成

谢邀,用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)

希望有所帮助~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值