银行风控模型的建立
作者:互联网
代码1
import pandas as pd from sklearn.model_selection import train_test_split from keras.models import Sequential from keras.layers.core import Dense, Activation from keras.layers import Activation,Dense,Dropout import numpy as np inputfile = './bankloan.xls' data = pd.read_excel(inputfile) X = data.drop(columns='违约') y = data['违约'] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1) model = Sequential() model.add(Dense(64,input_dim=8,activation='relu')) # Drop防止过拟合的数据处理方式 model.add(Dropout(0.5)) model.add(Dense(64,activation='relu')) model.add(Dropout(0.5)) model.add(Dense(1,activation='sigmoid')) # 编译模型,定义损失函数,优化函数,绩效评估函数 model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy']) # 导入数据进行训练 model.fit(X_train,y_train,epochs=200,batch_size=128) # 模型评估 score = model.evaluate(X_test,y_test,batch_size=128) print(score) predict_x=model.predict(X_test) yp=np.argmax(predict_x,axis=1) def cm_plot(y, y_pred): from sklearn.metrics import confusion_matrix #导入混淆矩阵函数 cm = confusion_matrix(y, y_pred) #混淆矩阵 import matplotlib.pyplot as plt #导入作图库 plt.matshow(cm, cmap=plt.cm.Greens) #画混淆矩阵图,配色风格使用cm.Greens,更多风格请参考官网。 plt.colorbar() #颜色标签 for x in range(len(cm)): #数据标签 for y in range(len(cm)): plt.annotate(cm[x,y], xy=(x, y), horizontalalignment='center', verticalalignment='center') plt.ylabel('True label') #坐标轴标签 plt.xlabel('Predicted label') #坐标轴标签 plt.show() return plt cm_plot(y_test, yp)
代码2
import pandas as pd from sklearn.model_selection import train_test_split from sklearn.linear_model import LogisticRegression from sklearn.metrics import accuracy_score inputfile = './bankloan.xls' data = pd.read_excel(inputfile) X = data.drop(columns='违约') y = data['违约'] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1) model = LogisticRegression() model.fit(X_train, y_train) y_pred = model.predict(X_test) score = accuracy_score(y_pred, y_test) print(score) def cm_plot(y, y_pred): from sklearn.metrics import confusion_matrix #导入混淆矩阵函数 cm = confusion_matrix(y, y_pred) #混淆矩阵 import matplotlib.pyplot as plt #导入作图库 plt.matshow(cm, cmap=plt.cm.Greens) #画混淆矩阵图,配色风格使用cm.Greens,更多风格请参考官网。 plt.colorbar() #颜色标签 for x in range(len(cm)): #数据标签 for y in range(len(cm)): plt.annotate(cm[x,y], xy=(x, y), horizontalalignment='center', verticalalignment='center') plt.ylabel('True label') #坐标轴标签 plt.xlabel('Predicted label') #坐标轴标签 plt.show() return plt cm_plot(y_test, y_pred)
结果1:
[0.3884179890155792, 0.800000011920929]
结果2:
0.8285714285714286
标签:plt,cm,模型,银行,风控,train,test,import,model 来源: https://www.cnblogs.com/yanjiahao/p/16076488.html