您的位置:首页 > 理论基础 > 计算机网络

金融风控-->客户流失预警模型-->神经网络建模

2017-07-13 11:13 351 查看
上一篇博文中,我们对金融数据建立了GBDT模型。这篇博文将利用深度学习框架pytorch对金融数据建立一个人工神经网络模型。

有关人工神经网络的详细介绍请看我的另外一篇博文人工神经网络

人工神经网络数据预处理:

不能有缺失值

移除常量型特征(即这个特征的最大值和最小值相等)

不能接受非数值形式的输入,字符型变量需要编码:

One hot编码

Dummy编码

浓度编码

变量归一化/标准化





人工神经网络中的参数设置:

输入层节点个数

隐藏层层数

隐藏层节点个数

隐藏层联接状态

激活函数

损失函数

学习速率

迭代次数

模型的效果

AUC score,超过0.7为佳,越大说明效果越好

调参时,可以选择AUC最高的一组参数



下面是代码部分,这里我们只用一个隐藏层,只调了隐藏层神经元个数,dropOut这两个参数。其他参数调优类似。

#coding:utf-8
import pandas as pd
import numpy
import torch
from torch.autograd import Variable
import torch.nn.functional as F
import torch.nn as nn
from sklearn import metrics
import torch.utils.data as Data
from sklearn.model_selection import train_test_split
import operator

modelData = pd.read_csv('modelData.csv', header = 0)
allFeatures = list(modelData.columns)
#remove the class label and cust id from features
allFeatures.remove('CUST_ID')
allFeatures.remove('CHURN_CUST_IND')
modelData2 = modelData.copy()

#normalize the features using max-min to convert the values into [0,1] interval
def MaxMinNorm(df,col):
ma, mi = max(df[col]), min(df[col])
rangeVal = ma - mi
if rangeVal == 0:
print col
df[col] = df[col].map(lambda x:(x-mi)*1.0/rangeVal)

for numFeatures in allFeatures:
MaxMinNorm(modelData2, numFeatures)

#we need to remove columns of 'TELEBANK_ALL_TX_NUM', 'RATIO_21' since they are constants
allFeatures.remove('TELEBANK_ALL_TX_NUM')
allFeatures.remove('RATIO_21')

x_train, x_test, y_train, y_test = train_test_split(modelData2[allFeatures],modelData2['CHURN_CUST_IND'], test_size=0.4,random_state=9)

train_x=x_train.values
train_y=y_train.values

tensor_x=torch.from_numpy(train_x)
tensor_y=torch.from_numpy(train_y)

torch_dataset=torch.utils.data.TensorDataset(data_tensor=tensor_x,target_tensor=tensor_y)

Batch_size=1500
train_loader=torch.utils.data.DataLoader(
dataset=torch_dataset,
batch_size=Batch_size,
shuffle=True,
num_workers=2,
)

x=Variable(torch.from_numpy(train_x)).type(torch.FloatTensor)
y=Variable(torch.from_numpy(train_y)).type(torch.LongTensor)

test_x=x_test.values
V_test_x=Variable(torch.from_numpy(test_x)).type(torch.FloatTensor)
test_y=y_test.values

class Net(torch.nn.Module):
def __init__(self,n_feature,n_hidden,n_output,drop_out):
super(Net,self).__init__()
self.hidden=torch.nn.Linear(n_feature,n_hidden)
torch.nn.Dropout(drop_out)
self.predict=torch.nn.Linear(n_hidden,n_output)

def forward(self, x):
x=F.relu(self.hidden(x))
x=self.predict(x)
return x

## 只有一个隐藏层,这里挑选一个在auc上效果最好的隐藏层的神经元个数
hidden_units_selection={}

for hidden_units in range(10,101,10):

net=Net(n_feature=174,n_hidden=800,n_output=2,drop_out=0.5)
optimizer=torch.optim.Adam(net.parameters(),lr=0.01)
##loss_func=nn.CrossEntropyLoss(weight=torch.FloatTensor([1,35]))
loss_func=nn.CrossEntropyLoss()
for epoch in range(200):
for step,(b_x,b_y) in enumerate(train_loader):
batch_x=Variable(b_x).type(torch.FloatTensor)
batch_y=Variable(b_y).type(torch.LongTensor)
out=net(batch_x)
loss=loss_func(out,batch_y)

optimizer.zero_grad()
loss.backward()
optimizer.step()

if epoch%5==0:
##F.softmax(out)得出二维的概率值,表示每个样本在0类,1类上概率值,是个Variabel类型
# 1表示在列这个维度上得出每行的最大值,返回最大值和在这个行上的位置index local
pred = net(V_test_x)
## 将Variabel 类型转成numpy类型
clf_pred_proba=F.softmax(pred).data.numpy()
## 取每个样本为类别1的概率
pred_proba=[i[1] for i in clf_pred_proba]
auc_score=metrics.roc_auc_score(test_y,pred_proba)
print('Epoch: ', epoch, '| roc_auc_score: %.4f' % auc_score)

pred = net(V_test_x)
clf_pred_proba=F.softmax(pred).data.numpy()
pred_proba=[i[1] for i in clf_pred_proba]
auc_score=metrics.roc_auc_score(test_y,pred_proba)
hidden_units_selection[hidden_units]=auc_score

best_hidden_units=max(hidden_units_selection.iteritems(),key=operator.itemgetter(1))[0]

## 只有一个隐藏层,这里挑选一个在auc上效果最好的dropOut

drop_out_selection={}

for drop_out in numpy.linspace(0,0.99,100):
net=Net(n_feature=174,n_hidden=best_hidden_units,n_output=2,drop_out=drop_out)
optimizer=torch.optim.Adam(net.parameters(),lr=0.01)
##loss_func=nn.CrossEntropyLoss(weight=torch.FloatTensor([1,35]))
loss_func=nn.CrossEntropyLoss()
for epoch in range(200):
for step,(b_x,b_y) in enumerate(train_loader):
batch_x=Variable(b_x).type(torch.FloatTensor)
batch_y=Variable(b_y).type(torch.LongTensor)
out=net(batch_x)
loss=loss_func(out,batch_y)

optimizer.zero_grad()
loss.backward()
optimizer.step()

if epoch%5==0:
pred = net(V_test_x)
clf_pred_proba=F.softmax(pred).data.numpy()
pred_proba=[i[1] for i in clf_pred_proba]
auc_score=metrics.roc_auc_score(test_y,pred_proba)
print('Epoch: ', epoch, '| roc_auc_score: %.4f' % auc_score)

pred = net(V_test_x)
clf_pred_proba=F.softmax(pred).data.numpy()
pred_proba=[i[1] for i in clf_pred_proba]
auc_score=metrics.roc_auc_score(test_y,pred_proba)
drop_out_selection[drop_out]=auc_score

best_drop_out=max(drop_out_selection.iteritems(),key=operator.itemgetter(1))[0]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: