ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

scorecard用法

2022-01-22 13:31:07  阅读:200  来源: 互联网

标签:None sc creditability 用法 scorecard train test woe


# -*- coding: utf-8 -*-
"""
Created on Fri Oct  9 13:34:59 2020

@author: Administrator
"""

import scorecardpy as sc
dat = sc.germancredit()
#首先,导入germancredit数据。

#1. 筛选变量
#这个函数可以根据指定的条件筛选变量,例如IV值、缺失率、一致性等
dt_s = sc.var_filter(dt=dat
                     , y="creditability"
                     , x=None
                     , iv_limit=0.02
                     , missing_limit=0.95
                     , identical_limit=0.95
                     , var_rm=None #强制删除变量的名称
                     , var_kp=None #强制保留变量的名称
                     , return_rm_reason=False #是否返回每个变量被删除的原因
                     , positive='bad|1' #坏样本的标签
                     )
# 数据划分
train, test = sc.split_df(dt=dt_s
                          , y='creditability'
                          , ratio=0.7 #默认按照7:3对数据集进行分割
                          , seed=186
                          ).values()
#变量分箱 返回字典
bins = sc.woebin(dt_s, y="creditability")

bins = sc.woebin(dt=dt_s #数据
                 , y="creditability" #目标值
                 , x=None #
                 , var_skip=None # 指定不需要分箱的变量。
                 , breaks_list=None #分割点的List。对分箱进行调整的时候用。可以进行自定义分箱
                 , special_values=None #指定单独的箱。
                 , stop_limit=0.1 #当IV值的增加值小于stop_limit或者卡方值小于qchisq(1-stoplimit, 1)时停止分割。
           , count_distr_limit=0.05 #分箱结果中最小占比。默认0.05
           , bin_num_limit=8 #最大分箱数。
           # min_perc_fine_bin=0.02, min_perc_coarse_bin=0.05, max_num_bin=8, 
           , positive="bad|1"
           , no_cores=None
           , print_step=0
           , method="tree"#分箱方法,可以有"tree" or "chimerge"。
           , ignore_const_cols=True#是否忽略常数列。
           , ignore_datetime_cols=True
           , check_cate_num=True#检查分类变量中类别数是否大于50。
           , replace_blank=True#将空值替换为None。
           , save_breaks_list=None
           )


# 变量分箱
sc.woebin_plot(bins)

# 分箱调整
breaks_adj = sc.woebin_adj(dt_s, "creditability", bins) 
bins_adj = sc.woebin(dt_s, y="creditability", breaks_list=breaks_adj)


#woe转换
train_woe = sc.woebin_ply(train, bins_adj)
test_woe = sc.woebin_ply(test, bins_adj)


#模型建立
y_train = train_woe.loc[:,'creditability']
X_train = train_woe.loc[:,train_woe.columns != 'creditability']
y_test = test_woe.loc[:,'creditability']
X_test = test_woe.loc[:,train_woe.columns != 'creditability']

# logistic regression ------
from sklearn.linear_model import LogisticRegression
lr = LogisticRegression(penalty='l1', C=0.9, solver='saga', n_jobs=-1)
lr.fit(X_train, y_train)
# lr.coef_
# lr.intercept_

# predicted proability
train_pred = lr.predict_proba(X_train)[:,1]
test_pred = lr.predict_proba(X_test)[:,1]

#模型评估
train_perf = sc.perf_eva(y_train, train_pred, title = "train")
test_perf = sc.perf_eva(y_test, test_pred, title = "test")

#评分映射
card = sc.scorecard(bins_adj, lr, X_train.columns)

# credit score
train_score = sc.scorecard_ply(train, card, print_step=0)
test_score = sc.scorecard_ply(test, card, print_step=0)

#评分稳定性评估--PSI
sc.perf_psi(
  score = {'train':train_score, 'test':test_score},
  label = {'train':y_train, 'test':y_test}
)

 

标签:None,sc,creditability,用法,scorecard,train,test,woe
来源: https://www.cnblogs.com/andylhc/p/15832912.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有