一、神经网络算法: 1 import pandas as pd 2 from keras.models import Sequential 3 from keras.layers.core import Dense, Activation 4 import numpy as np 5 # 参数初始化 6 inputfile = ' C:/Users/76319/Desktop/bankloan.xls ' 7 data
一、神经网络算法:
1 import pandas as pd 2 from keras.models import Sequential 3 from keras.layers.core import Dense, Activation 4 import numpy as np 5 # 参数初始化 6 inputfile = 'C:/Users/76319/Desktop/bankloan.xls' 7 data = pd.read_excel(inputfile) 8 x_test = data.iloc[:,:8].values 9 y_test = data.iloc[:,8].values 10 inputfile = 'C:/Users/76319/Desktop/bankloan.xls' 11 data = pd.read_excel(inputfile) 12 x_test = data.iloc[:,:8].values 13 y_test = data.iloc[:,8].values 14 15 model = Sequential() # 建立模型 16 model.add(Dense(input_dim = 8, units = 8)) 17 model.add(Activation('relu')) # 用relu函数作为激活函数,能够大幅提供准确度 18 model.add(Dense(input_dim = 8, units = 1)) 19 model.add(Activation('sigmoid')) # 由于是0-1输出,用sigmoid函数作为激活函数 20 model.compile(loss = 'mean_squared_error', optimizer = 'adam') 21 # 编译模型。由于我们做的是二元分类,所以我们指定损失函数为binary_crossentropy,以及模式为binary 22 # 另外常见的损失函数还有mean_squared_error、categorical_crossentropy等,请阅读帮助文件。 23 # 求解方法我们指定用adam,还有sgd、rmsprop等可选 24 model.fit(x_test, y_test, epochs = 1000, batch_size = 10) 25 predict_x=model.predict(x_test) 26 classes_x=np.argmax(predict_x,axis=1) 27 yp = classes_x.reshape(len(y_test)) 28 29 def cm_plot(y, yp): 30 from sklearn.metrics import confusion_matrix 31 cm = confusion_matrix(y, yp) 32 import matplotlib.pyplot as plt 33 plt.matshow(cm, cmap=plt.cm.Greens) 34 plt.colorbar() 35 for x in range(len(cm)): 36 for y in range(len(cm)): 37 plt.annotate(cm[x,y], xy=(x, y), horizontalalignment='center', verticalalignment='center') 38 plt.ylabel('True label') 39 plt.xlabel('Predicted label') 40 return plt 41 cm_plot(y_test,yp).show()# 显示混淆矩阵可视化结果 42 score = model.evaluate(x_test,y_test,batch_size=128) # 模型评估 43 print(score)
结果以及混淆矩阵可视化如下:
二、然后我们使用逻辑回归模型进行分析和预测:
import pandas as pd inputfile = 'C:/Users/76319/Desktop/bankloan.xls' data = pd.read_excel(inputfile) print (data.head()) X = data.drop(columns='违约') y = data['违约'] from sklearn.model_selection import train_test_split from sklearn.linear_model import LogisticRegression 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) print(y_pred) from sklearn.metrics import accuracy_score 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') #坐标轴标签 return plt cm_plot(y_test, y_pred).show()
结果如下:
综上所述得出,两种算法模型总体上跑出来的准确率还是不错的,但是神经网络准确性更高一点。
努力地向月光下的影子——骇客靠拢!!! 黎明之花,待时绽放