信用评分预测模型(八)--多层感知机模型

Author:LieDra
https://www.cnblogs.com/LieDra/

前言

下面将对数据利用多层感知机模型得到结果。

多层感知机模型介绍

多层感知器(MLP)是一种前馈的人工神经网络,映射一组输入向量到一 组输出向量,具有显著的学习和推理 能力,适合应用于复杂的分类问题。
一个简单的MLP模型如下所示(实际隐藏层可以有多层):
多层感知机.png
MLP 涉及输入层、输出层 以及隐藏层三层结构。最底层为输入层,中间层为隐藏层,最后为输出层。

最简单的 MLP 除输入层、输出层外, 需要有一层隐藏层。

MLP 的每一层的节点都全连接到下一层,即上一层的任何一个神经元与下一层的所有神经元都有连接,同时每个节点都带有非线性激活函数(sigmoid函数、Relu函数等),通过改变线性规则,克服感知器不能对线性不可分数据进行识别的弱点,使得神经网络可以应用到更多的非线性模型中。

下面是分别对原始数据应用MLP,对标准化数据应用MLP,对标准化数据应用调参MLP。

代码示例

#Multi-layer Perceptron 多层感知机
from sklearn.neural_network import MLPClassifier
#标准化数据,否则神经网络结果不准确,和SVM类似
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
# import mglearn
import matplotlib.pyplot as plt
# import numpy as np
import pandas as pd

#读取文件
readFileName="D:/study/5/code/python/python Data analysis and mining/class/dataset/german-全标准化.xls"

#读取excel
df=pd.read_excel(readFileName)
list_columns=list(df.columns[:-1])
x=df.ix[:,:-1]
y=df.ix[:,-1]
names=x.columns

l1,l2,l3 = [],[],[]

#random_state 相当于随机数种子
def train_test():
    # for i in range(0,1000):
    #     x_train,x_test,y_train,y_test=train_test_split(x,y,stratify=y,random_state=i)
    #     x_train,x_test,y_train,y_test=train_test_split(x,y,stratify=y)
    #     mlp=MLPClassifier(random_state=42)
    #     print('第',i+1,'次test:')
        x_train,x_test,y_train,y_test=train_test_split(x,y,stratify=y,train_size=0.6,random_state=492)
        x_test2,x_check,y_test2,y_check=train_test_split(x_test,y_test,stratify=y_test,train_size=0.25,random_state=492)
        mlp=MLPClassifier()
        mlp.fit(x_train,y_train)
        print("neural network:")   
        print("accuracy on the training subset:{:.3f}".format(mlp.score(x_train,y_train)))
        print("accuracy on the test subset:{:.3f}".format(mlp.score(x_check,y_check)))
        l1.append(mlp.score(x_check,y_check))

        print('***'*50)
        scaler=StandardScaler()
        x_train_scaled=scaler.fit(x_train).transform(x_train)
        x_check_scaled=scaler.fit(x_check).transform(x_check)
        
        # mlp_scaled=MLPClassifier(max_iter=1000,random_state=42)
        mlp_scaled=MLPClassifier(max_iter=100)
        mlp_scaled.fit(x_train_scaled,y_train)
        print("neural network after scaled:")   
        print("accuracy on the training subset:{:.3f}".format(mlp_scaled.score(x_train_scaled,y_train)))
        print("accuracy on the test subset:{:.3f}".format(mlp_scaled.score(x_check_scaled,y_check)))
        l2.append(mlp.score(x_check_scaled,y_check))
    
    
        print('***'*50)
        print('***'*50)

        # mlp_scaled2=MLPClassifier(max_iter=1000,alpha=1,random_state=42)
        mlp_scaled2=MLPClassifier(max_iter=100,alpha=0.0001)
        mlp_scaled2.fit(x_train_scaled,y_train)
        print("neural network after scaled and alpha change to 1:")   
        print("accuracy on the training subset:{:.3f}".format(mlp_scaled2.score(x_train_scaled,y_train)))
        print("accuracy on the test subset:{:.3f}".format(mlp_scaled2.score(x_check_scaled,y_check)))
        l3.append(mlp.score(x_check_scaled,y_check))
        print('***'*50)
        print('***'*50)
        print('***'*50)

        # print(len(mlp_scaled2.coefs_[1])) #100
        # print(len(mlp_scaled2.coefs_[0])) #20 系数矩阵

        #绘制颜色图,热图
        # plt.figure(figsize=(20,5))
        # plt.imshow(mlp_scaled2.coefs_[0],interpolation="None",cmap="GnBu")
        # plt.yticks(range(20),names)
        # plt.xlabel("coefs")
        # plt.ylabel("input feature")
        # plt.colorbar()
        # plt.show()

train_test()
print('max acc1',l1.index(max(l1))+1)
print('max acc2',l2.index(max(l2))+1)
print('max acc3',l3.index(max(l3))+1)


 
# #绘制颜色图,热图
# plt.figure(figsize=(20,5))
# plt.imshow(mlp_scaled.coefs_[0],interpolation="None",cmap="GnBu")
# plt.yticks(range(30),names)
# plt.xlabel("columns in weight matrix")
# plt.ylabel("input feature")
# plt.colorbar()
# plt.show()

# ******************************************************************************************************************************************************
# max acc1 51
# max acc2 78
# max acc3 78

结果

部分结果

neural network after scaled and alpha change to 1:
accuracy on the training subset:0.835
accuracy on the test subset:0.790
******************************************************************************************************************************************************
******************************************************************************************************************************************************
******************************************************************************************************************************************************
max acc1 2
max acc2 10
max acc3 10

coefs系数矩阵热力图如下(。。。随便看看)
热力图.png

posted @ 2020-01-08 20:02  LieDra  阅读(1141)  评论(0)    收藏  举报