风控B卡行为评分卡lr开发 与 xgb优化

读取数据 、特征衍生、EDA 、划分数据集、特征筛选、自动分箱(这里不使用手动调箱)、woe转化、逐步回归、 建模评估和调参、模型效果不错、psi值较大、调参效果无明显提升。

import pandas as pd
import numpy as np
import pickle
from statsmodels.stats.outliers_influence import variance_inflation_factor
import statsmodels.api as sm
from sklearn import ensemble
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import roc_auc_score
import re
import time
import datetime
from dateutil.relativedelta import relativedelta
from sklearn.model_selection import train_test_split

import toad #蛤蟆包

#################################
#由于数据已经经过一定的清洗了,非一手数据,所以我们忽略了一些步骤,进行变量衍生
#   1, 读取数据,衍生初始变量   #
'''
Loan_Amount:总额度
OS:未还金额
Payment:还款金额
Spend:使用金额
Delq:逾期情况
'''
#################################
folderOfData = 'E:/数据分析/评分卡/行为B卡'
allData = pd.read_csv(folderOfData+'/训练集 (1).csv',header = 0,engine ='python')


#衍生逾期类型的特征的函数
def DelqFeatures(event,window,type):
    '''
    :parms event 数据框
    :parms windows 时间窗口
    :parms type 响应事件类型
    '''
    current = 12
    start = 12 - window + 1
    #delq1、delq2、delq3为了获取window相对应的dataframe范围
    delq1 = [event[a] for a in ['Delq1_' + str(t) for t in range(current, start - 1, -1)]]
    delq2 = [event[a] for a in ['Delq2_' + str(t) for t in range(current, start - 1, -1)]]
    delq3 = [event[a] for a in ['Delq3_' + str(t) for t in range(current, start - 1, -1)]]
    if type == 'max delq':
        if max(delq3) == 1:
            return 3
        elif max(delq2) == 1:
            return 2
        elif max(delq1) == 1:
            return 1
        else:
            return 0
    if type in ['M0 times','M1 times', 'M2 times']:
        if type.find('M0')>-1:
            return sum(delq1)
        elif type.find('M1')>-1:
            return sum(delq2)
        else:
            return sum(delq3)
 
allFeatures = []
 
'''
逾期类型的特征在行为评分卡(预测违约行为)中,一般是非常显著的变量。
通过设定时间窗口,可以衍生以下类型的逾期变量:
'''
# 考虑过去1个月,3个月,6个月,12个月
for t in [1,3,6,12]:
    # 1,过去t时间窗口内的最大逾期状态
    allFeatures.append('maxDelqL'+str(t)+"M")
    allData['maxDelqL'+str(t)+"M"] = allData.apply(lambda x: DelqFeatures(x,t,'max delq'),axis=1)
 
    # 2,过去t时间窗口内的,M0,M1,M2的次数
    allFeatures.append('M0FreqL' + str(t) + "M")
    allData['M0FreqL' + str(t) + "M"] = allData.apply(lambda x: DelqFeatures(x,t,'M0 times'),axis=1)
 
    allFeatures.append('M1FreqL' + str(t) + "M")
    allData['M1FreqL' + str(t) + "M"] = allData.apply(lambda x: DelqFeatures(x, t, 'M1 times'), axis=1)
 
    allFeatures.append('M2FreqL' + str(t) + "M")
    allData['M2FreqL' + str(t) + "M"] = allData.apply(lambda x: DelqFeatures(x, t, 'M2 times'), axis=1)
 
#衍生额度使用率类型特征的函数
def UrateFeatures(event, window, type):
    '''
    :parms event 数据框
    :parms windows 时间窗口
    :parms type 响应事件类型
    '''
    current = 12
    start = 12 - window + 1
    #获取在数据框内有效区域
    monthlySpend = [event[a] for a in ['Spend_' + str(t) for t in range(current, start - 1, -1)]]
    #获取授信总额度
    limit = event['Loan_Amount']
    #月使用率
    monthlyUrate = [x / limit for x in monthlySpend]
    if type == 'mean utilization rate':
        return np.mean(monthlyUrate)
    if type == 'max utilization rate':
        return max(monthlyUrate)
    #月额度使用率增加的月份
    if type == 'increase utilization rate':
        #val[0:-1]表示第一个元素到倒数第二个元素的切片
        currentUrate = monthlyUrate[0:-1]
        #val[1:]
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值