课本上线性回归算法

课本上线性回归算法

#coding=utf-8
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
#构造训练数据
x = np.arange(0.,10.,0.2)
m = len(x)
x0 = np.full(m,1.0)
input_data = np.vstack([x0,x]).T#将偏置b作为权向量的一个分量
target_data = 2 * x + 5 + np.random.randn(m)

#两种终止条件
loop_max = 10000#最大迭代次数防止死循环
epsilon = 1e-3#能接受的最小误差

#初始化权值
np.random.seed(0)
theta = np.random.randn(2)
alpha = 0.001 #步长
diff = 0.
error = np.zeros(2)
count = 0
finish = 0

while count < loop_max:
    count += 1
    sum_m = np.zeros(2)
    for i in range(m):
        dif = (np.dot(theta,input_data[i]) - target_data[i]) * input_data[i]
        sum_m = sum_m + dif

    theta = theta - alpha * sum_m

    if np.linalg.norm(theta - error) <epsilon:
        finsh = 1
        break
    else:
        error = theta
    print('loop count = %d' % count,'\tw',theta)
print('loop count = %d' % count,'\tw',theta)

#用scipy线性回归检查
slope,intercept,r_value,p_value,slope_std_error = stats.linregress((x,target_data))
print('intercept = %s slope = %s' % (intercept,slope))
plt.plot(x,target_data,'g*')
plt.plot(x,theta[1] * x + theta[0],'r')
plt.xlabel('x')
plt.ylabel('y')
plt.show()

结果图
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

henu-于笨笨

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

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

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

打赏作者

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

抵扣说明:

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

余额充值