TensorFlow学习记录(三)---梯度下降法之一元线性回归

 

 

 

 

 

 

 

import numpy as np
import matplotlib.pyplot as plt
x = np.array([137.97,104.50,100.00,124.32,79.20,99.00,124.00,114.00,106.69,138.05,53.75,46.91,
              68.00,63.02,81.26,86.21])
y = np.array([145.00,110.00,93.00,116.00,65.32,104.00,118.00,91.00,62.00,133.00,51.00,
              45.00,78.50,69.65,75.69,95.30])
#加载数据集
learn_rate = 0.00001
iter = 100
display_step = 10
#设置超参数
np.random.seed(612)
w = np.random.randn()#返回正太分布的随机数
b = np.random.randn()
#设置模型参数值
mse = []
for i in range(0,iter + 1):
    dl_dw = np.mean(x*(w*x+b-y))
    dl_db = np.mean(w*x+b-y)
    w = w- learn_rate*dl_dw
    b = b- learn_rate*dl_db

    pred = w*x+b
    Loss = np.mean(np.square(y-pred))/2
    mse.append(Loss)

    if i % display_step == 0:
        print("i: %i,Loss: %f,w: %f,b:%f" %(i,mse[i],w,b))
#结果可视化

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.figure(figsize=(20,4))

plt.subplot(1,3,1)
plt.scatter(x,y,color = "red",label="销售记录")
plt.scatter(x,pred,color = "blue",label = "梯度下降法")
plt.plot(x,pred,color="blue")
#加入解析解
plt.plot(x,0.89*x+5.41,color="green",label="解析法")

plt.legend(loc = "upper left")
plt.xlabel("Area",fontsize = 14)
plt.ylabel("Price",fontsize = 14)
#损失可视化
plt.subplot(1,3,2)
plt.plot(range(20,100),mse[20:100],color = "yellow",label = "损失函数的值")
plt.xlabel("Iteration",fontsize = 14)
plt.ylabel("Loss",fontsize = 14)

plt.subplot(1,3,3)
plt.plot(y,color= "red",marker = "o",label = "销售记录")
plt.plot(pred,color = "blue",marker = "o",label = "预测房价")
plt.legend()
plt.xlabel("Sample ",fontsize = 14)
plt.ylabel("PRICE   ",fontsize = 14)
plt.legend(loc = "upper left")
plt.show()

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前额皮质

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值