银行分控模型建立

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 = 'E:\JAVA_5/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)

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) #画混淆矩阵

 

 

 

 

# -*- coding: utf-8 -*-
"""
Created on Sun Mar 27 00:04:18 2022

@author: dd
"""
import matplotlib as plt
import pandas as pd
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers.core import Dense, Activation


# 参数初始化
inputfile = 'E:/JAVA_5/bankloan.xls'
data = pd.read_excel(inputfile)
x_test = data.iloc[:,:8].values
y_test = data.iloc[:,8].values

model = Sequential() # 建立模型
model.add(Dense(input_dim = 8, units = 8))
model.add(Activation('relu')) # 用relu函数作为激活函数,能够大幅提供准确度
model.add(Dense(input_dim = 8, units = 1))
model.add(Activation('sigmoid')) # 由于是0-1输出,用sigmoid函数作为激活函数

model.compile(loss = 'mean_squared_error', optimizer = 'adam')
# 编译模型。由于我们做的是二元分类,所以我们指定损失函数为binary_crossentropy,以及模式为binary
# 另外常见的损失函数还有mean_squared_error、categorical_crossentropy等,请阅读帮助文件。
# 求解方法我们指定用adam,还有sgd、rmsprop等可选

model.fit(x_test, y_test, epochs = 10, batch_size = 10) # 训练模型,学习一千次
import numpy as np
predict_x=model.predict(x_test)
classes_x=np.argmax(predict_x,axis=1)
yp = classes_x.reshape(len(y_test))

score = model.evaluate(x_test, y_test, batch_size=128) #分类预测精确度
print(score)

from cm_plot import * # 导入自行编写的混淆矩阵可视化函数
cm_plot(y_test,yp).show()
#-*- coding: utf-8 -*-
def cm_plot(y, yp):

from sklearn.metrics import confusion_matrix

cm = confusion_matrix(y, yp)

import matplotlib.pyplot as plt
plt.matshow(cm, cmap=plt.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')
return plt

 

 

 

 

posted @ 2022-03-27 22:47  兴X  阅读(31)  评论(0编辑  收藏  举报