• 基于数据挖掘算法建立银行风控模型


    一、BP神经网络算法

    代码:

    import pandas as pd
    import numpy as np
    #导入划分数据集函数
    from sklearn.model_selection import train_test_split
    #读取数据
    datafile = './data/bankloan.xls'#文件路径
    data = pd.read_excel(datafile)
    x = data.iloc[:,:8]
    y = data.iloc[:,8]
    #划分数据集
    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=100)
    #导入模型和函数
    from keras.models import Sequential
    from keras.layers import Dense,Dropout
    #导入指标
    from keras.metrics import BinaryAccuracy
    #导入时间库计时
    import time
    start_time = time.time()
    #-------------------------------------------------------#
    model = Sequential()
    model.add(Dense(input_dim=8,units=800,activation='relu'))#激活函数relu
    model.add(Dropout(0.5))#防止过拟合的掉落函数
    model.add(Dense(input_dim=800,units=400,activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(input_dim=400,units=1,activation='sigmoid'))
     
    model.compile(loss='binary_crossentropy', optimizer='adam',metrics=[BinaryAccuracy()])
    model.fit(x_train,y_train,epochs=100,batch_size=128)
    loss,binary_accuracy = model.evaluate(x,y,batch_size=128)
    #--------------------------------------------------------#
    end_time = time.time()
    run_time = end_time-start_time#运行时间
     
    print('模型运行时间:{}'.format(run_time))
    print('模型损失值:{}'.format(loss))
    print('模型精度:{}'.format(binary_accuracy))
     
    yp = model.predict(x).reshape(len(y))
    yp = np.around(yp,0).astype(int) #转换为整型
    from cm_plot import *  # 导入自行编写的混淆矩阵可视化函数
    cm_plot(y,yp).show()  # 显示混淆矩阵可视化结果
    编写的混淆矩阵可视化函数:
     
    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

    运行结果:

     

    二、决策树算法

    代码:

    import pandas as pd
    import seaborn as sns
    import matplotlib.pyplot as plt
    from sklearn.model_selection import train_test_split
    from sklearn.tree import DecisionTreeClassifier as DTC
    from sklearn.metrics import confusion_matrix
    from sklearn.metrics import accuracy_score
    import time
    start_time = time.time()
    
    filePath = './data/bankloan.xls'
    data = pd.read_excel(filePath)
    x = data.iloc[:,:8]
    y = data.iloc[:,8]
    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=100)
    #模型
    dtc_clf = DTC(criterion='entropy')#决策树
    #训练
    dtc_clf.fit(x_train,y_train)
    #模型评价
    dtc_yp = dtc_clf.predict(x)
    dtc_score = accuracy_score(y, dtc_yp)
    score = {"决策树得分":dtc_score}
    
    score = sorted(score.items(),key = lambda score:score[0],reverse=True)
    print(pd.DataFrame(score)) 
    #中文标签、负号正常显示
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.rcParams['axes.unicode_minus'] = False 
    #绘制混淆矩阵
    figure = plt.subplots(figsize=(12,10))
    plt.title('决策树')
    dtc_cm = confusion_matrix(y, dtc_yp)
    heatmap = sns.heatmap(dtc_cm, annot=True, fmt='d')
    heatmap.yaxis.set_ticklabels(heatmap.yaxis.get_ticklabels(), rotation=0, ha='right')
    heatmap.xaxis.set_ticklabels(heatmap.xaxis.get_ticklabels(), rotation=45, ha='right')
    plt.ylabel("true label")
    plt.xlabel("predict label")
    plt.show()
    end_time = time.time()
    run_time = end_time-start_time#运行时间
    print('模型运行时间:{}'.format(run_time))
    print('模型损失值:{}'.format(loss))
    print('模型精度:{}'.format(binary_accuracy))

    决策树算法运行结果:

     

     

     再对两者的模型运行时间和模型损失值、模型精度进行比较,表现为两种算法(模型)都是对的。

  • 相关阅读:
    Android中AsyncTask与handler
    AndroidTimer使用(三)补充篇
    Android的消息机制(一)
    Android消息处理机制(二)
    java操作Excel文件(二)
    java操作excel
    Android APK反编译详解(附图)
    Android中Handler的使用方法——在子线程中更新界面
    Android线程间通信的Message机制
    AndroidTimer使用(二)
  • 原文地址:https://www.cnblogs.com/Adaran/p/16072470.html
Copyright © 2020-2023  润新知