前言
上一篇的模型的关键性质是它是参数
w
0
w_0
w0,……
w
D
w_D
wD的一个线性函数。这给模型带来了极大的局限性。因此我们将扩展函数的模型为:将输入变量的固定的非线性函数进行线性组合,形式为:
y
(
x
,
w
)
=
∑
j
=
0
M
−
1
w
j
ϕ
j
(
x
)
=
w
T
ϕ
(
x
)
y\left( \mathbf{x,w} \right) =\sum_{j=0}^{M-1}{w_j\phi _j\left( \mathbf{x} \right)}=\mathbf{w}^T\phi \left( \mathbf{x} \right)
y(x,w)=j=0∑M−1wjϕj(x)=wTϕ(x)
在许多模式识别的实际应用中我们会对原始数据进行某种固定形式的预处理或者特征抽取。如果原始变量由向量
x
x
x组成,那么特征可以用基函数$\left{ \varPhi _j\left( x \right) \right} $来表示。
import numpy as np #导入numpy库
import matplotlib.pyplot as plt
train_dataset = np.loadtxt('train.txt')
test_dataset = np.loadtxt('test.txt')
#print(len(train_dataset))
#print(len(test_dataset))
#print(train_dataset)
#print(test_dataset)
基函数为多项式形式
多项式基函数的一个局限性是它们是输入变量的全局函数,因此对于输入空间一个区域的改变将会影响所有其他的区域。这个问题也可以这样解决:把输入空间切分成若干个区域,然后对于每个区域用不同的多项式函数进行拟合。这样的函数叫做样条函数(spline function)
x_test = train_dataset[:,0]
y_test = train_dataset[:,1]
#print(np.shape(X_test)) #此时为数组形式
#q1 = np.matrix([x_test])
#q2 = np.matrix([x_test**2])
#q3 = np.matrix([x_test**3])
#q = np.r_[q1,q2]
#q = np.r_[q,q3]
#print(q.T)
#print(np.shape(q))
x_1 = np.ones(300)
Q = np.matrix([x_1,x_test,x_test**2,x_test**3,x_test**4,x_test**5,x_test**6])
np.shape(Q)
Q = Q.T
print(Q.shape)
# 这多项式取前三项试一试
(300, 7)
W1 = np.dot(Q.T, Q)
W1 = W1.I
print(W1)
W1 = np.dot(W1, Q.T)
print(W1.shape)
y_test = np.matrix(train_dataset[:,1]).T
#y_test
W = np.dot(W1, y_test)
print(W)
print(W[1][0])
x_train_dataset=train_dataset[:,0]
y_train_dataset=train_dataset[:,1]
#画散点图
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
x = np.linspace(0.01,30, 1000)
#y = 1.87473796e+00+ 3.21945066e-01*x+6.21328961e-02*(x**2)+-1.45114519e-03*(x**3)+-9.10121220e-06*(x**4)# + 5.33462435e-03*(x**3)+-1.32274202e-04*(x**4)
#y = 3.11196017e+00 + -1.28738149e+00*x +5.26963857e-01*(x**2)+-5.19707506e-02*(x**3)+2.29640260e-03*(x**4)+-3.73168610e-05*(x**5)
y = 2.59851574e+00+-3.30716199e-01*x+1.31705218e-01*(x**2)+ 1.25428682e-02*(x**3)+-2.61495098e-03*(x**4)+ 1.37686962e-04*(x**5)+-2.35796337e-06*(x**6)
ax.scatter(x_train_dataset,y_train_dataset,color='red',marker='o',s=1)
ax.plot(x, y,color='green',ls='-')
#画直线图
[<matplotlib.lines.Line2D at 0x269608a5e20>]
from numpy import mat
x_test_dataset=test_dataset[:,0]
#print(y_test_dataset)
x_test_dataset=(x_test_dataset).reshape(200,1)
#print(x_test_dataset)
y_test_dataset=test_dataset[:,1]
y_test_dataset=mat(y_test_dataset).reshape(200,1)
#print(y_test_dataset)
import math
y = 1.06025258*x +-0.00403272*(x**2)
y =1.06025258*x_test_dataset + -0.00403272*(x_test_dataset**2)
bias2 = 0
for i in range (0,200):
bias2=bias2+math.pow((1.87473796e+00+ 3.21945066e-01*x_test_dataset[i]+6.21328961e-02*(x_test_dataset[i]**2)+-1.45114519e-03*(x_test_dataset[i]**3)+-9.10121220e-06*(x_test_dataset[i]**4))-y_test_dataset[i],2)
print(bias2)
851.4790466150766
bias2 = 0
for i in range (0,200):
bias2=bias2+math.pow((3.11196017e+00 + -1.28738149e+00*x_test_dataset[i] +5.26963857e-01*(x_test_dataset[i]**2)+-5.19707506e-02*(x_test_dataset[i]**3)+2.29640260e-03*(x_test_dataset[i]**4)+-3.73168610e-05*(x_test_dataset[i]**5))-y_test_dataset[i],2)
print(bias2)
855.1585767908293
bias2 = 0
for i in range (0,200):
bias2=bias2+math.pow((2.59851574e+00+-3.30716199e-01*x_test_dataset[i]+1.31705218e-01*(x_test_dataset[i]**2)+ 1.25428682e-02*(x_test_dataset[i]**3)+-2.61495098e-03*(x_test_dataset[i]**4)+ 1.37686962e-04*(x_test_dataset[i]**5)+-2.35796337e-06*(x_test_dataset[i]**6))-y_test_dataset[i],2)
print(bias2)
865.2514577549032
经过上述拟合发现我们选取N=4的时候方差最小。