百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术分类 > 正文

python数据分析与挖掘实战—银行分控模型(几种算法模型的比较)

ztj100 2024-12-07 18:52 21 浏览 0 评论

一、神经网络算法:

 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()

结果如下:

综上所述得出,两种算法模型总体上跑出来的准确率还是不错的,但是神经网络准确性更高一点。

相关推荐

Java的SPI机制详解

作者:京东物流杨苇苇1.SPI简介SPI(ServiceProvicerInterface)是Java语言提供的一种接口发现机制,用来实现接口和接口实现的解耦。简单来说,就是系统只需要定义接口规...

90%的Java程序员都忽视的内部类使用不当导致内存泄露!

...

一文读懂 Spring Boot 启动原理,开发效率飙升!

在当今的Java开发领域,SpringBoot无疑是最热门的框架之一。它以其“约定大于配置”的理念,让开发者能够快速搭建和启动应用,极大地提高了开发效率。但是,你是否真正了解Spring...

ServiceLoader

ServiceLoader是Java提供的一种服务发现机制(ServiceProviderInterface,SPI)...

深入探索 Spring Boot3 中的自定义扩展操作

在当今互联网软件开发领域,SpringBoot无疑是最受欢迎的框架之一。随着其版本迭代至SpringBoot3,它为开发者们带来了更多强大的功能和特性,其中自定义扩展操作更是为我们在项目开发中...

Spring Boot启动过程全面解析:从入门到精通

一、SpringBoot概述SpringBoot是一个基于Spring框架的快速开发脚手架,它通过"约定优于配置"的原则简化了Spring应用的初始搭建和开发过程。...

Spring Boot 3.x 自定义 Starter 详解

今天星期六,继续卷springboot3.x。在SpringBoot3.x中,自定义Starter是封装和共享通用功能、实现“约定优于配置”理念的强大机制。通过创建自己的Starte...

Spring Boot 的 3 种动态 Bean 注入技巧

在SpringBoot开发中,动态注入Bean是一种强大的技术,它允许我们根据特定条件或运行时环境灵活地创建和管理Bean。相比于传统的静态Bean定义,动态注入提供了更高的灵活性和可...

大佬用4000字带你彻底理解SpringBoot的运行原理!

SpringBoot的运行原理从前面创建的SpringBoot应用示例中可以看到,启动一个SpringBoot工程都是从SpringApplication.run()方法开始的。这个方法具体完成...

Springboot是如何实现自动配置的

SpringBoot的自动配置功能极大地简化了基于Spring的应用程序的配置过程。它能够根据类路径中的依赖和配置文件中的属性,自动配置应用程序。下面是SpringBoot实现自动配置的...

Spring Boot3.x 应用的生命周期深度解析

SpringBoot应用的生命周期可以清晰地划分为三个主要阶段:启动阶段(Startup)...

Springboot 启动流程及各类事件生命周期那点事

前言本文通过Springboot启动方法分析SpringApplication逻辑。从静态run方法执行到各个阶段发布不同事件完成整个应用启动。...

Spring框架基础知识-常用的接口1

BeanDefinition基本概念BeanDefinition是Spring框架中描述bean配置信息的核心接口,它包含了创建bean实例所需的所有元数据。...

一家拥有 158 年历史的公司遭遇索赔,被迫关闭!

...

Java 技术岗面试全景备战!从基础到架构的系统性通关攻略分享

Java技术岗的面试往往是一项多维度的能力检验。本文将会从核心知识点、项目经验到面试策略,为你梳理一份系统性的备战攻略!...

取消回复欢迎 发表评论: