F[ f((x )] = ∫一个bF(x )克(x )dX
G(x )F一世(x ),i = 0 ,… ,M F[ f一世(x )]F[ f(X )] = Δ X [ ˚F0G02+ f1个G1个+ 。。。+ fñ− 1Gñ− 1+ fñGñ2]
F[ f(x )]Δ X= y= f0G02+ f1个G1个+ 。。。+ fñ− 1Gñ− 1+ fñGñ2
F0= 一,˚F 1个= f(x1个),。。。,˚F ñ− 1= f(xñ− 1),˚F ñ= b ,
a < x1个< 。。。< xñ− 1< b ,Δ X = X j + 1− xĴ
中号F一世(x ),i = 1 ,… ,M 一世F[ f一世(x )]Δ X= y一世= f我0G02+ f我1G1个+ 。。。+ f我,Ñ− 1Gñ− 1+ f我ñGñ2
G0,… ,gñX= ⎡⎣⎢⎢⎢⎢F00/ 2F10/ 2…F中号0/ 2F01F11…F中号1个…………F0 ,N− 1F1 ,N− 1…F中号,N− 1F0 N/ 2F1 N/ 2…F中号ñ/ 2⎤⎦⎥⎥⎥⎥
ÿ= [ y0,… ,y中号]
G(x )
import numpy as np
def Gaussian(x, mu, sigma):
return np.exp(-0.5*((x - mu)/sigma)**2)
X ∈ [ 一,b ]
x = np.arange(-1.0, 1.01, 0.01)
dx = x[1] - x[0]
g = Gaussian(x, 0.25, 0.25)
让我们以不同频率的正弦和余弦作为我们的训练功能。计算目标向量:
from math import cos, sin, exp
from scipy.integrate import quad
freq = np.arange(0.25, 15.25, 0.25)
y = []
for k in freq:
y.append(quad(lambda x: cos(k*x)*exp(-0.5*((x-0.25)/0.25)**2), -1, 1)[0])
y.append(quad(lambda x: sin(k*x)*exp(-0.5*((x-0.25)/0.25)**2), -1, 1)[0])
y = np.array(y)/dx
现在,回归矩阵:
X = np.zeros((y.shape[0], x.shape[0]), dtype=float)
print('X',X.shape)
for i in range(len(freq)):
X[2*i,:] = np.cos(freq[i]*x)
X[2*i+1,:] = np.sin(freq[i]*x)
X[:,0] = X[:,0]/2
X[:,-1] = X[:,-1]/2
线性回归:
from sklearn.linear_model import LinearRegression
reg = LinearRegression().fit(X, y)
ghat = reg.coef_
import matplotlib.pyplot as plt
plt.scatter(x, g, s=1, marker="s", label='original g(x)')
plt.scatter(x, ghat, s=1, marker="s", label='learned $\hat{g}$(x)')
plt.legend()
plt.grid()
plt.show()
G(x )
from scipy.signal import savgol_filter
ghat_sg = savgol_filter(ghat, 31, 3) # window size, polynomial order
plt.scatter(x, g, s=1, marker="s", label='original g(x)')
plt.scatter(x, ghat, s=1, marker="s", label='learned $\hat{g}$(x)')
plt.plot(x, ghat_sg, color="red", label='Savitzky-Golay $\hat{g}$(x)')
plt.legend()
plt.grid()
plt.show()
F[ f(x )]F(x )F[ f((x )] = ∫一个b大号(˚F(x )) dX
F0,˚F1个… ,fñXF[ f((x )] = ∫一个b大号(˚F(x ),f′(x )) dX
F′F0,˚F1个… ,fñ大号F0,˚F1个… ,fñ,尽管它可能不像在线性情况下那样容易,但是可以尝试使用非线性方法(例如神经网络或SVM)来学习它。