代码使用PTHON3.x
题目如下
import numpy as np
import random
import matplotlib.pyplot as plt
F64='float64'
def gen_sin_dot_sample(num_point):
x=np.linspace(0,10,num_point)
y=np.sin(x)+np.random.random(num_point)*0.05
return [x.tolist(),y.tolist()]
def show_point(p_x,p_y):
plt.figure(1)
plt.plot(p_x,p_y,'ob')
plt.xlim(-5.2,15.2)
plt.ylim(-1.8,1.8)
plt.xlabel('x')
plt.ylabel('y')
plt.show()
[x,y]=gen_sin_dot_sample(100)
show_point(x,y)
得到下图样本点
要求用局部加权线性回归拟合该曲线
======================================================
代码如下
import numpy as np
import random
import matplotlib.pyplot as plt
F64='float64'
def gen_sin_dot_sample(num_point):
x = np.linspace(0,10,num_point)
y = np.sin(x)+np.random.random(num_point)*0.5
return [x.tolist(),y.tolist()]
def draw_point(p_x,p_y):
plt.figure(1)
plt.plot(p_x,p_y,'ob')
plt.xlim(-5.2,15.2)
plt.ylim(-2.2,2.2)
plt.xlabel('x')
plt.ylabel('y')
plt.show()
def draw_point_and_line(p_x,p_y,l_x,l_y):
plt.figure(1)
plt.plot(p_x,p_y,'ob')
plt.plot(l_x,l_y,'r')
plt.xlim(-5.2,15.2)
plt.ylim(-2.2,2.2)
plt.xlabel('x')
plt.ylabel('y')
plt.show()
def lwr(x,y,x_calculate,times_iteration,k,study_rate):# x,y,x_calculate is list
num_sample = len(x)
x = np.asarray(x,dtype=F64).reshape((num_sample,1))
y = np.asarray(y,dtype=F64).reshape((num_sample,1))
one_mat = np.ones((num_sample,1),dtype=F64)
x_add_one = np.hstack((one_mat,x))
theta = np.random.random((2))
theta = theta.astype(F64).reshape(2,1)
for i in xrange(times_iteration):
weight = np.exp(-np.square(np.hstack((x,x))-x_calculate)/(k**2))
diff_theta = ((np.dot(x_add_one,theta)-y)*x_add_one*weight).mean(0,dtype=F64,keepdims=True)
theta = theta-diff_theta.transpose()*study_rate
return theta.reshape((2)).tolist()
[x,y] = gen_sin_dot_sample(100)
x_calculate = np.linspace(0,10,50).tolist()
point_list=[]
for i in x_calculate:
theta = lwr(x,y,i,1000,0.3,0.1)
point_y = theta[0]+theta[1]*i
point_list.append(point_y)
draw_point_and_line(x,y,x_calculate,point_list)
结果是,学习率选0.1、k选1.3时,欠拟合,下图
学习率选0.1、k选0.3时,合适,下图
学习率选0.1、k选0.03时,过拟合,下图
============================================
总结,局部加权线性回归计算慢,但效果很好,补一张算法图,免忘
其中