其他分享
首页 > 其他分享> > 风控预测

风控预测

作者:互联网

神经网络测试

import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
#载入数据集
mnist = input_data.read_data_sets('C:/Users/CHANYING/Desktop/huadabing/data/bankloan.xls', one_hot=True)
#每个批次的大小
batch_size=100
#计算一共有多少批次
n_batch = mnist.train.num_examples // batch_size
#定义两个placehold
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])

#创建一个简单的神经网络
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
prediction = tf.nn.softmax(tf.matmul(x,W)+b)
#二次代价函数
loss = tf.reduce_mean(tf.square(y-prediction))
#使用梯度下降法
train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)

#初始化变量
init = tf.global_variables_initializer()
#存放结果到一个布尔型变量中
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))
#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

with tf.Session() as sess:
sess.run(init)
for epoch in range(20):
for batch in range(n_batch):
batch_xs,batch_ys = mnist.train.next_batch(batch_size)
sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})

acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
print("Iter "+str(epoch) + ",Testing Accuracy "+str(acc))

 

 

决策树

import matplotlib.pyplot as plt
import pandas as pd
filename = 'C:/Users/CHANYING/Desktop/huadabing/data/bankloan.xls'
data = pd.read_excel(filename) # 导入数据

x = data.iloc[:,:8].astype(int)
y = data.iloc[:,8].astype(int)

classes = list(set(fact))
classes.sort(reverse=True)
r1=[[509,10],[181,0]]

plt.figure(figsize=(12,10)) #设置plt窗口的大小
confusion =r1
print("confusion",confusion)
plt.imshow(confusion, cmap=plt.cm.Blues)
indices = range(len(confusion))
indices2 = range(3)
plt.xticks(indices, classes,rotation=40,fontsize=18)
plt.yticks([0.00,1.00], classes,fontsize=18)
plt.ylim(1.5 , -0.5) #设置y的纵坐标的上下限

plt.title("Confusion matrix",fontdict={'weight':'normal','size': 18})
#设置color bar的标签大小
cb=plt.colorbar()
cb.ax.tick_params(labelsize=18)
plt.xlabel('Predict label',fontsize=18)
plt.ylabel('True label',fontsize=18)

print("len(confusion)",len(confusion))
for first_index in range(len(confusion)):
for second_index in range(len(confusion[first_index])):

if confusion[first_index][second_index]>200:
color="w"
else:
color="black"
plt.text(first_index, second_index, confusion[first_index][second_index],fontsize=18, color = color,verticalalignment='center',horizontalalignment='center',)
plt.show()

 

标签:index,plt,预测,confusion,batch,风控,tf,data
来源: https://www.cnblogs.com/ChanyTong/p/16076484.html