使用tensorflow实现逻辑回归算法

本文参考《tensorflow实战机器学习指南》

 

 

 
# -*- coding: utf-8 -*- """ Created on Sat Feb 24 20:55:02 2018 @author: www """ #用tensorflow实现逻辑回归算法 #使用鸢尾花数据集 import pandas as pd import numpy as np import tensorflow as tf import matplotlib.pyplot as plt import requests from tensorflow.python.framework import ops ops.reset_default_graph() birthdata_url = 'https://www.umass.edu/statdata/statdata/data/lowbwt.dat' birth_file = requests.get(birthdata_url) birth_data = birth_file.text.split('\r\n')[5:] birth_header = [x for x in birth_data[0].split( '') if len(x) >= 1] birth_data = [[float(x) for x in y.split(' ') if len(x) >= 1] for y in birth_data[1:] if len(y) >= 1] y_vals = np.array([x[1] for x in birth_data]) x_vals = np.array([x[2:9] for x in birth_data]) train_indices = np.random.choice(len(x_vals), round(len(x_vals) * 0.8), replace=False) test_indices = np.array(list(set(range(len(x_vals))) - set(train_indices))) X_train = x_vals[train_indices] X_test = x_vals[test_indices] y_train = y_vals[train_indices] y_test = y_vals[test_indices] #特征缩放 def normalize_cols(m): col_max = m.max(axis=0) col_min = m.min(axis=0) return (m - col_min) / (col_max - m) X_train = normalize_cols(X_train) X_test = normalize_cols(X_test) #声明批量大小,占位符,变量和逻辑模型,这步不需要用sigmoid函数封装输出结果,因为sigmoid操作是包含在 #内建损失函数之中的 batch_size = 25 x_data = tf.placeholder(shape = [None, 7], dtype=tf.float32) y_target = tf.placeholder(shape = [None,1], dtype=tf.float32) A = tf.Variable(tf.random_normal(shape=[7,1])) b = tf.Variable(tf.random_normal(shape=[1,1])) mdel_output = tf.add(tf.matmul(x_data, A), b) #声明损失函数,其包括sigmoid函数,初始化变量,声明优化器, loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=y_target, logits=mdel_output)) init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) my_opt = tf.train.GradientDescentOptimizer(0.01) train_step = my_opt.minimize(loss) #除记录损失函数外,也需要记录分类器在训练集和测试集的准确度,所以创建一个返回准确度的预测函数 prediction = tf.round(tf.sigmoid(mdel_output)) prediction_correct = tf.cast(tf.equal(prediction, y_target), tf.float32) accuracy = tf.reduce_mean(prediction_correct) #开始遍历迭代训练,记录损失值和准确度, loss_vec = [] train_acc = [] test_acc = [] for i in range(1500): rand_index = np.random.choice(len(X_train), size=batch_size) rand_x = X_train[rand_index] rand_y = np.transpose([y_train[rand_index]]) sess.run(train_step, feed_dict={x_data:rand_x, y_target:rand_y}) temp_loss = sess.run(loss, feed_dict={x_data:rand_x, y_target:rand_y}) loss_vec.append(temp_loss) temp_acc_train = sess.run(accuracy, feed_dict={x_data:X_train, y_target:np.transpose([y_train])}) train_acc.append(temp_acc_train) #============================================================================== # temp_acc_test = sess.run(accuracy, feed_dict={x_data:X_test, y_target:np.transpose([y_test])}) # test_acc.append(temp_acc_test) #============================================================================== #绘制损失和准确度 plt.plot(loss_vec, 'k-') plt.title('Cross Entropy Loss per Generation') plt.xlabel('Generation') plt.ylabel('Cross Entropy Loss') plt.show() plt.plot(train_acc, 'k-', label='Train Set Accuracy') plt.plot(test_acc, 'k-', label='Test Set Accuracy') plt.title('Train and Test Accuracy') plt.xlabel('Generation') plt.ylabel('Accuracy') plt.legend(loc='lower right') plt.show() 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值