神经网络贷款风险评估(base on keras and python )

原创 2017年08月18日 14:35:17
  • 标签:
  • python /
  • 神经网络 /
  • keras /
  • 300
  • 编辑
  • 删除

用我儿子的话说,有一天啊,小乌龟遇见小兔子………

有一天,我在网上看到这样一片文章,决策书做贷款决策分析。

贷还是不贷:如何用Python和机器学习帮你决策?

import pandas as pd
df = pd.read_csv('loans.csv')#print(df.head())X = df.drop('safe_loans', axis=1)y = df.safe_loans#change categoricalfrom sklearn.preprocessing import LabelEncoder
from collections import defaultdict
d = defaultdict(LabelEncoder)
X_trans = X.apply(lambda x: d[x.name].fit_transform(x))
X_trans.head()#X_trans.to_excel('X_trans.xls') #random take train and testfrom sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X_trans, y, random_state=1)
#call decision tree
from sklearn import tree
clf = tree.DecisionTreeClassifier(max_depth=8)
clf = clf.fit(X_train, y_train)test_rec = X_test.iloc[1,:]
clf.predict([test_rec])y_test.iloc[1]
from sklearn.metrics import accuracy_score
print(accuracy_score(y_test, clf.predict(X_test)))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

这篇文章写的非常好,从中学到好多,但是计算的正确率不太高,8层的决策树正确率才能达到0.645

0.645480347467
  • 1

我用神经网络重新做了计算

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Aug 17 21:14:08 2017@author: luogan
"""#read data
import pandas as pd
df = pd.read_csv('loans.csv')#print(df.head())#X = df.drop('safe_loans', axis=1)X = df.drop(['safe_loans' ],axis=1)
y = df.safe_loans#change categoricalfrom sklearn.preprocessing import LabelEncoder
from collections import defaultdict
d = defaultdict(LabelEncoder)
X_trans = X.apply(lambda x: d[x.name].fit_transform(x))
X_trans.head()#X_trans.to_excel('X_trans.xls')
##############
data_train=X_trans
data_max = data_train.max()
data_min = data_train.min()
data_mean = data_train.mean()
#
# data_std = data_train.std()
X_train1 = (data_train-data_max)/(data_max-data_min)y=0.5*(y+1)
#random take train and testfrom sklearn.cross_validation import train_test_split
x_train, x_test, y_train, y_test = train_test_split(X_train1, y, random_state=1)#x_train.to_excel('xx_trans.xls') #y_train.to_excel('y_trans.xls') #call decision tree
#from sklearn import tree
#clf = tree.DecisionTreeClassifier(max_depth=10)
#clf = clf.fit(X_train, y_train)from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activationmodel = Sequential() #建立模型
model.add(Dense(input_dim = 12, output_dim = 48)) #添加输入层、隐藏层的连接
model.add(Activation('tanh')) #以Relu函数为激活函数model.add(Dense(input_dim = 48, output_dim = 48)) #添加隐藏层、隐藏层的连接
model.add(Activation('relu')) #以Relu函数为激活函数
model.add(Dropout(0.2))model.add(Dense(input_dim = 48, output_dim = 36)) #添加隐藏层、隐藏层的连接
model.add(Activation('relu')) #以Relu函数为激活函数
model.add(Dropout(0.2))
model.add(Dense(input_dim = 36, output_dim = 36)) #添加隐藏层、隐藏层的连接
model.add(Activation('relu')) #以Relu函数为激活函数model.add(Dense(input_dim = 36, output_dim = 12)) #添加隐藏层、隐藏层的连接
model.add(Activation('relu')) #以Relu函数为激活函数
model.add(Dense(input_dim = 12, output_dim = 12)) #添加隐藏层、隐藏层的连接
model.add(Activation('relu')) #以Relu函数为激活函数model.add(Dense(input_dim = 12, output_dim = 1)) #添加隐藏层、输出层的连接
model.add(Activation('sigmoid')) #以sigmoid函数为激活函数
#编译模型,损失函数为binary_crossentropy,用adam法求解
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(x_train.values, y_train.values, nb_epoch = 70, batch_size = 2000) #训练模型r = pd.DataFrame(model.predict_classes(x_test.values))
'''
r = pd.DataFrame(model.predict(x_test.values))
rr=r.values
tr=rr.flatten()for i in range(tr.shape[0]):if tr[i]>0.5:tr[i]=1else:tr[i]=0
'''
from sklearn.metrics import accuracy_score
print(accuracy_score(y_test, r))        
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
0.650640749978
  • 1

我对神经网络进行了各种优化,正确率一直上不去,计算结果还不如我的神经网络股票预测代码

我不高兴,非常不高兴,65%的正确率是我无法容忍的,让我郁闷的是,我一直深爱的神经网络居然也这么无力
不能这样下去,下回我们将采用传说中的xgboost试一下

百度云代码下载

提取码      jc5x

神经网络贷款风险评估(base on keras and python ) 原创 2017年08月18日 14:35:17 标签: python / 神经网络 / keras 300 用我相关推荐

  1. python pandas dataframe 行列选择,切片操作 原创 2017年02月15日 21:43:18 标签: python 30760 python pandas dataframe

    python pandas dataframe 行列选择,切片操作 原创 2017年02月15日 21:43:18 标签: python / 30760 编辑 删除 python pandas dat ...

  2. 从任务到可视化,如何理解LSTM网络中的神经元 By 机器之心2017年7月03日 14:29 对人类而言,转写是一件相对容易并且可解释的任务,所以它比较适合用来解释神经网络做了哪些事情,以及神经网

    从任务到可视化,如何理解LSTM网络中的神经元 By 机器之心2017年7月03日 14:29 对人类而言,转写是一件相对容易并且可解释的任务,所以它比较适合用来解释神经网络做了哪些事情,以及神经网络 ...

  3. 2017年11月23日学习笔记_用python解决杨辉三角函数,以及理解

    今天学习了廖雪峰老师的python教程,学到杨辉三角函数的时候很迷茫, 他的基本格式如下: [1],[1, 1],[1, 2, 1],[1, 3, 3, 1],[1, 4, 6, 4, 1],[1, ...

  4. 从CNN视角看在自然语言处理上的应用 原创 2017年10月24日 00:00:00 1339 作者 | 卞书青 卷积神经网络(Convolutional Neural Network)最早是应用在

    从CNN视角看在自然语言处理上的应用 原创 2017年10月24日 00:00:00 标签: 1339

  5. python机器学习2021年6月19日09:35:06

    垂直和并array arr1 = np.array([1,2,3]) arr2 = np.array([4,5,6]) arr3 = np.vstack((arr1,arr2))#垂直合并 print ...

  6. PANDAS 数据合并与重塑(concat篇) 原创 2016年09月13日 19:26:30 47784 pandas作者Wes McKinney 在【PYTHON FOR DATA ANALYS

    PANDAS 数据合并与重塑(concat篇) 原创 2016年09月13日 19:26:30 标签: 47784 编辑 删除 pandas作者Wes McKinney 在[PYTHON FOR DA ...

  7. LSTM入门必读:从入门基础到工作方式详解 By 机器之心2017年7月24日 12:57 长短期记忆(LSTM)是一种非常重要的神经网络技术,其在语音识别和自然语言处理等许多领域都得到了广泛的应用

    LSTM入门必读:从入门基础到工作方式详解 By 机器之心2017年7月24日 12:57 长短期记忆(LSTM)是一种非常重要的神经网络技术,其在语音识别和自然语言处理等许多领域都得到了广泛的应用. ...

  8. python行业中性_用Python分析指数: 11月16日热门指数Z值表

    衡量市场,指数高低是一个难题! 价值投资者很难知道,现在是高估,还是低估? 买的是便宜还是,贵了? 应该现在买/卖,还是再等等? 针对这个问题,我在网上看到了一些量化的处理方法.例如:平均数法,中位数 ...

  9. 派森学python_2019 年 1月 26 日 随笔档案 - 派森学python - 博客园

    摘要:环境准备 python2.7 凑合的linux 差不多的无线网卡 "pywifi模块" 弱口令字典 清除系统中的任何wifi连接记录(非常重要!!!) 首先,这个模块在win ...

最新文章

  1. jupyter Nodebook如何查看函数帮助
  2. 一次上机试题(面向对象)
  3. error MSB8008: 指定的平台工具集(v110)未安装或无效。请确保选择受支持的 PlatformToolset 值
  4. Windows下挂载iscsi存储及多路径功能配置
  5. 某大佬的20+公司面试题总结和自己的补充
  6. 16.unix网络编程一卷 unp.h
  7. python时间差怎么转换为数字_pandas进行时间数据的转换和计算时间差并提取年月日...
  8. 数学中的一朵“奇葩”——四元数
  9. 25个关键技术点,带你熟悉Python
  10. 终端中用命令成功修改linux~Ubuntu PATH环境变量
  11. java栈空异常_Java如何处理空堆栈异常?
  12. Spark重要概念提出时间戳和原因
  13. Mac快速备忘录开启与关闭设置方法
  14. 004 Leaflet 第四个demo 使用自己的图标替换marker图标
  15. 几款用于防破解的软件
  16. MindMaster Pro 7.2中文版 — 亿图思维导图
  17. 一招教你解决Rational rose画时序图(Sequence diagram)时找不到参与者(Actor)的尴尬现场
  18. 说说基因组的垃圾DNA-Transposable elements
  19. html 指针图表,HTML5 canvas 指针时钟
  20. 股票市场行情走势图绘制

热门文章

  1. 访问者模式 php,18php访问者模式
  2. linux:exec族函数, exec族函数配合fork使用,system 函数,popen 函数
  3. android 之RadioButton单选控件
  4. 使用隐式Intent打开系统内置拨号界面
  5. CCF 2020年题目题解 - Python
  6. pyspark pipline
  7. mysql bin日志备份_mysql之binlog日志备份还原
  8. flask html 得到文本框 input的内容_【笔记7】HTML及其常见标签
  9. Leetcode 剑指 Offer 53 - I. 在排序数组中查找数字 I (每日一题 20210928)
  10. Leetcode 103. 二叉树的锯齿形层序遍历 (每日一题 20210924)