regression_basis function-GAUSS

前言

上一篇的模型的关键性质是它是参数 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=0M1wjϕj(x)=wTϕ(x)
在许多模式识别的实际应用中我们会对原始数据进行某种固定形式的预处理或者特征抽取。如果原始变量由向量 x x x组成,那么特征可以用基函数$\left{ \varPhi _j\left( x \right) \right} $来表示。

import numpy as np #导入numpy库
import matplotlib.pyplot as plt 
import math
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)

基函数为高斯基函数形式

对于基函数,有许多其他的选择这里我们采用高斯基函数: ϕ j ( x ) = e − ( x − μ j ) 2 2 σ 2 \phi _j\left( x \right) =e^{-\frac{\left( x-\mu _j \right) ^2}{2\sigma ^2}} ϕj(x)=e2σ2(xμj)2
其中 μ j \mu_j μj控制了基函数在输入空间中的位置,参数 σ \sigma σ控制了基函数的空间大小。这种基函数通常被称作“高斯基函数”,但是应该注意它们未必是一个概率表达式。特别的,归一化系数不重要,因为这些基函数会与一个调节参数 w j w_j wj相乘。

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)
# 这里我们需要调节参数u和s,这里我假设如下参数
u_0 = 0.4
u_1 = 8.4
u_2 = 14.9
u_3 = 21
u_4 = 27.8
s2  = 2.22

x_0 = np.exp(-((x_test-u_0)**2/(2*s2**2) ))
x_1 = np.exp(-((x_test-u_1)**2/(2*s2**2)))
x_2 = np.exp(-((x_test-u_2)**2/(2*s2**2)))
x_3 = np.exp(-((x_test-u_3)**2/(2*s2**2)))
x_4 = np.exp(-((x_test-u_4)**2/(2*s2**2)))
Q = np.matrix([x_0,x_1,x_2,x_3,x_4])
np.shape(Q)
Q = Q.T
print(Q.shape)
# 这多项式取前三项试一试


(300, 5)
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)
w = W.getA()
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)
y =  w[0]*np.exp(-((x-u_0)**2/(2*s2**2)))+w[1]*np.exp(-((x-u_1)**2/(2*s2**2)))+w[2]*np.exp(-((x-u_2)**2/(2*s2**2)))+w[3]*np.exp(-((x-u_3)**2/(2*s2**2)))+w[4]*np.exp(-((x-u_4)**2/(2*s2**2)))
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 0x23656e73100>]

在这里插入图片描述

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
bias2 = 0
#测试
#bias2=bias2+math.pow((x_test_dataset[1]*0.94732599+0.62680273)-y_test_dataset[1],2)
#print(bias2)
for i in range (0,200):
    bias2=bias2+math.pow((w[0]*np.exp(-((x_test_dataset[i]-u_0)**2/(2*s2**2)))+w[1]*np.exp(-((x_test_dataset[i]-u_1)**2/(2*s2**2)))+w[2]*np.exp(-((x_test_dataset[i]-u_2)**2/(2*s2**2)))+w[3]*np.exp(-((x_test_dataset[i]-u_3)**2/(2*s2**2)))+w[4]*np.exp(-((x_test_dataset[i]-u_4)**2/(2*s2**2))))-y_test_dataset[i],2)
print(bias2)
190.09069632822656

这里我的几个参数调节了很长时间,这里再给出训练集的时候,我们根据训练集的散点图来大致判断的上述的u和s参数选取

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值