Toad 是专为工业界模型开发设计的Python工具包,特别针对评分卡的开发。Toad 的功能覆盖了建模全流程,从 EDA、特征工程、特征筛选到模型验证和评分卡转化。Toad 的主要功能极大简化了建模中最重要最费时的流程,即特征筛选和分箱。
导入模型包
import pandas as pd
import numpy as np
import toad
import matplotlib.pyplot as plt
%matplotlib inline
from toad.plot import bin_plot
from sklearn.model_selection import train_test_split
读取数据并查看数据基本内容
df = pd.read_csv('germancredit.csv')
df.replace({'good':0,'bad':1}, inplace=True)
print(df.info())
df.head()
使用toad进行分箱,并输出分箱的内容
combiner = toad.transform.Combiner()
combiner.fit(df, y='creditability', method='chi', min_samples=0.05)
bins = combiner.export()
print('duration in month:',bins['duration.in.month'])
分为7组并绘制图形
c2 = toad.transform.Combiner()
c2.fit(df[['duration.in.month','creditability']], y='creditability', method='chi', n_bins=7)
transformed = c2.transform(df[['duration.in.month','creditability']], labels=True)
bin_plot(transformed, x='duration.in.month', target='creditability')
一般分箱的折线图要实现单调的趋势,所以我们要调整分箱的箱数。
c2 = toad.transform.Combiner()
c2.fit(df[['duration.in.month','creditability']], y='creditability', method='chi', n_bins=5)
transformed = c2.transform(df[['duration.in.month','creditability']], labels=True)
bin_plot(transformed, x='duration.in.month', target='creditability')
使用toad计算woe
train_X, test_X, train_y, test_y = train_test_split(df.drop('creditability', axis=1), df['creditability'], test_size=0.25, random_state=450)
# 合并数据,并增加列区分训练和测试的特征
data_train = pd.concat([train_X, train_y], axis=1)
data_train['type'] = 'train'
data_test = pd.concat([test_X, test_y], axis=1)
data_test['type'] = 'test'
# 设置分箱的边界
a_bin = {'duration.in.month':[9,12,18,33]}
c2 = toad.transform.Combiner()
c2.set_rules(a_bin)
data = pd.concat([data_train,data_test], axis=0)
# 进行分箱
temp_data = c2.transform(data[['duration.in.month','creditability','type']])
# 绘制图形
from toad.plot import badrate_plot, proportion_plot
badrate_plot(temp_data, target='creditability', x='type', by='duration.in.month')
# 每一个箱的占比情况
proportion_plot(temp_data['duration.in.month'])
0和1组存在倒挂,即badrate不单调,0和1进行合并。
# 设置分箱的边界
a_bin = {'duration.in.month':[9,18,33]}
c2 = toad.transform.Combiner()
c2.set_rules(a_bin)
# 进行分箱
temp_data = c2.transform(data[['duration.in.month','creditability','type']])
# 绘制图形
badrate_plot(temp_data, target='creditability', x='type', by='duration.in.month')
# 每一个箱的占比情况
proportion_plot(temp_data['duration.in.month'])
# 将特征的值转化为分箱的箱号
binned_data = c2.transform(data_train)
# 计算woe
transer = toad.transform.WOETransformer()
# 对woe的值进行转化,映射到数据集上,则训练集用fit_transform,测试集用transform
data_tr_woe = transer.fit_transform(binned_data, binned_data['creditability'], exclude=['creditability','type'])
data_tr_woe.head()