课本上线性回归算法
#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()
结果图