# -*- coding: utf-8 -*-
import tensorflow as tf
import time
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn import svm
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt
import numpy as np
class read_data():
def __init__(self,):
path='D://code//dataanaylze//credit_default-master//feature.csv'
self._CSV_FILE_PATH = path
self._data = pd.read_csv(self._CSV_FILE_PATH)
# print(self._data.shape)#查看数据维度
# print(self._data.describe())#数据集描述
self._sum_num = self._data.shape[0] # 数据总行数
self._indicator = 0
def read(self):
cardiac_output= self._data['CO'].value_counts()#查询心输出量的的情况
# print(cardiac_output)
df = pd.DataFrame({'ID': cardiac_output.index,'values': cardiac_output.values})
# print(df)
# plt.rcParams['font.sans-serif']=['SimHei']#设置字体为SimHei显示中文
# plt.figure(figsize = (6,6))
# plt.title('心输出量分布情况\n ')
# sns.set_color_codes("pastel")
# sns.barplot(x = 'ID', y="values", data=df)
# locs, labels = plt.xticks()
# plt.show()
self._data.drop(['ID'], inplace=True, axis =1) #去掉ID
target = self._data['CO'].values
# print(type(target))
# print(target.shape)
columns = self._data.columns.tolist()
columns.remove('CO')
# print(type(columns))
features = self._data[columns].values
# print(type(features))
# print(features.shape)
train_x,train_y ,test_x, test_y = train_test_split(features, target, test_size=0.30, random_state =1)#选择30%作为测试集
# # 采用Z-Score规范化数据,保证每个特征维度的数据均值为0,方差为1
# ss = StandardScaler()
# train_X = ss.fit_transform(train_x)
# test_X = ss.transform(test_x)
return train_x,test_x,train_y,test_y
def next_batch(self,train_X, train_y, test_X,test_y,batch_size):
end_indicator = self._indicator + batch_size
if end_indicator > self._sum_num:
raise Exception("batch size is larger than all examples")
batch_data = train_X[self._indicator: end_indicator]
batch_target = train_y[self._indicator: end_indicator]
batch_data1 = test_X[self._indicator: end_indicator]
batch_target2 = test_y[self._indicator: end_indicator]
self._indicator = end_indicator
return batch_data, batch_target,batch_data1,batch_target2
#定义模型
x = tf.placeholder(tf.float32,[None,4])
y = tf.placeholder(tf.float32,[None])##输入的真实值的占位符
keep_prob =tf.placeholder(tf.float32)
# n_input = 4#样本特征数
# n_epoch = 200 # 迭代数
# lr = 0.05#学习率
# eps = 0.5#支持边界到决策边界的函数距离
# lam = 0.05#L2正则化函数
W1 = tf.Variable(tf.truncated_normal([4,2000],stddev=0.1))#初始化时一个非常重要的环节
b1 = tf.Variable(tf.zeros([2000])+0.1)
L1 = tf.nn.tanh(tf.matmul(x,W1)+b1)#使用双曲正切
L1_drop = tf.nn.dropout(L1,keep_prob) #tensorflow封装好的dropout函数,L1是我们需要控制的神经元,keep_prob是工作的神经元的百分比
#注意使用dropout后会使模型的训练速度下降
W2 = tf.Variable(tf.truncated_normal([2000,1000],stddev=0.1))#增加隐藏层设置2000个神经元,这里是故意定义一个复杂的神经网络
b2 = tf.Variable(tf.zeros([1000])+0.1)#期望其出项过度拟合的情况
L2 = tf.nn.tanh(tf.matmul(L1_drop,W2)+b2)
L2_drop = tf.nn.dropout(L2,keep_prob)
W3 = tf.Variable(tf.truncated_normal([1000,1],stddev=0.1))
b3 = tf.Variable(tf.zeros([1])+0.1)
prediction = tf.nn.dropout(tf.matmul(L2_drop,W3)+b3,0.8)
#注意这里的知识点是tensorflow中矩阵相乘的规则
# 创建一个简单的神经网络
# 创建SVM分类器
# model = svm.SVC()
# 用训练集做训练
# model.fit(train_X,train_y)
# # 用测试集做预测
# prediction=model.predict(test_X)
y_reshaped=tf.reshape(y,(-1,1))
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_reshaped,logits=prediction))#损失函数
#第一个参数logits:就是神经网络最后一层的输出,未经过soft_max
#第二个参数labels:实际的标签,需要是one-hot格式
train_step = tf.train.GradientDescentOptimizer(1e-3).minimize(loss)
#评估训练好的模型
correct_prediction = tf.equal(tf.argmax(y_reshaped),tf.argmax(prediction))#计算预测值和真实值
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))#f.cast作用:布尔型转化为浮点数,并且取平均值,得到准确率
init = tf.global_variables_initializer()
start = time.clock()
with tf.Session() as sess:
sess.run(init)
for i in range(3000):
all_test_value=[]
train_x, train_y,test_x,test_y=read_data().read()
bath_xs,bath_ys,bath_xs1,bath_ys1 = read_data().next_batch( train_x, train_y,test_x,test_y ,10)
sess.run(train_step, feed_dict={x:bath_xs,y:bath_ys,keep_prob:0.8})
if i % 500 == 0:
acc = sess.run(accuracy, feed_dict={x:bath_xs, y:bath_ys,keep_prob:0.8})
print("Iter " + str(i) + ",Training Accuracy " + str(acc))
if (i+1) % 10 == 0:
value=sess.run(train_step, feed_dict={x: bath_xs1, y: bath_ys1, keep_prob: 0.8})
print("Iter " + str(i) + ",Testing Accuracy " + str(value))
# prediction_value = sess.run(prediction, feed_dict={x: bath_xs})
# plt.figure()
# x就是输入数据
# plt.xlabel('心输出量真实值')
# plt.ylabel('心输出量的预测值')
# plt.scatter(train_x, train_y)
# plt.title('心输出量对比图')
# plt.plot(prediction_value , bath_ys, label='心输出量')
# plt.show()
end = time.clock()
print('Running time: %s Seconds'%int(end-start))
print('训练完成')
简单的非线性回归,基于tensorflow实现
最新推荐文章于 2021-10-12 19:04:31 发布