Tensorflow线性回归模型搭建

# import tensorflow as tf
# import numpy as np
# greeting = tf.constant('Hello Google Tensorflow!')
# #启动一个会话
# sess = tf.Session()
# #使用会话执行greeting计算模块
# result = sess.run(greeting)
# print(result)
# sess.close()
#
# #使用TensorFlow完成一次线性函数计算
# matrix1 = tf.constant([[3.,3.]]) #行向量
# matrix2 = tf.constant([[2.],[2.]]) #列向量
# product = tf.matmul(matrix1,matrix2)
# linear = tf.add(product,tf.constant(2.0))
#
# with tf.Session() as sess:
#     result = sess.run(linear)
#     print(result)

import tensorflow as tf
import numpy as np
import pandas as pd
train = pd.read_csv('breast-cancer-train.csv')
test = pd.read_csv('breast-cancer-test.csv')
X_train = np.float32(train[['Clump Thickness','Cell Size']].T)
y_train = np.float32(train['Type'].T)
X_test = np.float32(test[['Clump Thickness','Cell Size']])
y_test = np.float32(test['Type'].T)
b = tf.Variable(tf.zeros([1]))
W = tf.Variable(tf.random_uniform([1,2],-1.0,1.0))

#显示线性函数
y = tf.matmul(W,X_train) + b
#获得训练集上的均方误差
loss = tf.reduce_mean(tf.square(y-y_train))

#使用梯度下降估计参数W,b,设置步长为0.01
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)

init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)

for step in range(0,1000):
    sess.run(train)
    if step % 200 == 0:
        print(step,sess.run(W),sess.run(b))

#测试样本
test_nagative = test.loc[test['Type']==0][['Clump Thickness','Cell Size']]
test_positive = test.loc[test['Type']==1][['Clump Thickness','Cell Size']]

import matplotlib.pyplot as plt
plt.scatter(test_nagative['Clump Thickness'],test_nagative['Cell Size'],marker='o',s=200,c='red')
plt.scatter(test_positive['Clump Thickness'],test_positive['Cell Size'],marker='x',s=150,c='black')
plt.xlabel('Clump Thickness')
plt.ylabel('Cell Size')
lx = np.arange(0,12)
ly = (0.5 - sess.run(b) -lx * sess.run(W)[0][0])/ sess.run(W)[0][1]
plt.plot(lx,ly,color='green')
plt.show()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值