当前位置: 首页 > 工具软件 > Scorecards > 使用案例 >

二期:Combined Scorecards

景成和
2023-12-01
from sklearn.neural_network import MLPClassifier



'''
串行结构的组合模型
'''

#随机从所有的变量种选取一部分构建神经网络模型
trainData_trial = trainData.copy()
randomSelectedFeatures = random.sample(WOE_IV_dict.keys(),20)
randomSelectedWOE = []
for var in randomSelectedFeatures:
    newVar = var+"_WOE"
    randomSelectedWOE.append(newVar)
    trainData_trial[newVar] = trainData_trial[var].map(lambda x: WOE_IV_dict[var]['WOE'][x])


X_train = np.matrix(trainData_trial[randomSelectedWOE])
y_train = np.array(trainData_trial['label'])
clf = MLPClassifier(solver='lbfgs', alpha=1e-5, hidden_layer_sizes=(5, 2), random_state=1)
clf.fit(X_train, y_train)
pred_prob = clf.predict_proba(X_train)[:,1]

# 神经网络的输出作为评分卡模型的输入
X_scorecard = X[featureSelected + ['intercept']]
X_scorecard['ANN'] = pred_prob

featuresEnsembled = ['ANN']
n = 0
while(n<len(finalFeatures)):
    nextVar = finalFeatures[n]
    featuresEnsembled = featuresEnsembled + [nextVar]
    X_train = X_scorecard[featuresEnsembled+['intercept']]
    logit = sm.Logit(y, X_train)
    logit_result = logit.fit()
    params = logit_result.params
    nonAnnParams = params[params.index != 'ANN']
    if max(nonAnnParams) < 0:
        n += 1
        continue
    else:
        featuresEnsembled.remove(nextVar)
        n += 1

trainData['log_odds_ensembled'] = logit_result.predict()
perf_model_combined = KS_AR(trainData, 'log_odds_ensembled', 'label')
# {'KS': 0.64722808021589284, 'AR': 0.68410884676888761}


'''
异构并行模型
'''
#根据上一步的结果,未调优的神经网络模型的结果是 pred_prob
trainData['prob_ANN'] = pred_prob
trainData['log_odds_ANN'] = trainData['prob_ANN'].apply(lambda x: np.log(x/(1-x)))
w_list = [i/100.0 for i in range(1,100)]
perf_list = []
for w in w_list:
    trainData['prob_LR'] = trainData['log_odds'].apply(lambda x: 1.0/(1+np.exp(-x)))
    trainData['log_odds_combined_1'] = trainData[['log_odds_ANN','log_odds']].apply(lambda x: w*x.log_odds+(1-w)*x.log_odds_ANN,axis=1)
    perf_model_combined_2 = KS_AR(trainData, 'log_odds_combined_1', 'label')
    perf_list.append((w,perf_model_combined_2))
#{'KS': 0.64502685026357542, 'AR': 0.67523323096890819}




'''
同构并行模型。采用神经网络
'''
#Bagging
train, test = train_test_split(trainData_trial, train_size=0.7)
total_pred = np.zeros(test.shape[0])
numberOfBagging = 10
for i in range(numberOfBagging):
    train2 = train.sample(frac = 0.6, replace = True)
    X_train = np.matrix(train2[randomSelectedWOE])
    y_train = np.array(train2['label'])
    clf = MLPClassifier(solver='lbfgs', alpha=1e-5, hidden_layer_sizes=(5, 2), random_state=1)
    clf.fit(X_train, y_train)
    X_test = np.matrix(test[randomSelectedWOE])
    pred_prob = clf.predict_proba(X_test)[:, 1]
    pred_log_odds = np.log(pred_prob/(1-pred_prob))
    total_pred = total_pred + pred_log_odds
total_pred = total_pred/numberOfBagging
test['log_odds_bagging'] = total_pred
perf_model_combined_bagging = KS_AR(test, 'log_odds_bagging', 'label')
#{'KS': 0.6499313111659355, 'AR': 0.67769316320816442}


X_train = np.matrix(train[randomSelectedWOE])
y_train = np.array(train['label'])
clf = MLPClassifier(solver='lbfgs', alpha=1e-5, hidden_layer_sizes=(5, 2), random_state=1)
clf.fit(X_train, y_train)
X_test = np.matrix(test[randomSelectedWOE])
pred_prob = clf.predict_proba(X_test)[:,1]
test['log_odds_single_ANN'] = pred_prob
perf_model_single_ANN = KS_AR(test, 'log_odds_single_ANN', 'label')
#{'KS': 0.64591434769055633, 'AR': 0.6705601450281512}

 类似资料: