- Machine Learning Algorithms
- Giuseppe Bonaccorso
- 238字
- 2021-07-02 18:53:32
A bidimensional example
Let's consider a small dataset built by adding some uniform noise to the points belonging to a segment bounded between -6 and 6. The original equation is: y = x + 2 + n, where n is a noise term.
In the following figure, there's a plot with a candidate regression function:

As we're working on a plane, the regressor we're looking for is a function of only two parameters:

In order to fit our model, we must find the best parameters and to do that we choose an ordinary least squares approach. The loss function to minimize is:

With an analytic approach, in order to find the global minimum, we must impose:

So (for simplicity, it accepts a vector containing both variables):
import numpy as np
def loss(v):
e = 0.0
for i in range(nb_samples):
e += np.square(v[0] + v[1]*X[i] - Y[i])
return 0.5 * e
And the gradient can be defined as:
def gradient(v):
g = np.zeros(shape=2)
for i in range(nb_samples):
g[0] += (v[0] + v[1]*X[i] - Y[i])
g[1] += ((v[0] + v[1]*X[i] - Y[i]) * X[i])
return g
The optimization problem can now be solved using SciPy:
from scipy.optimize import minimize
>>> minimize(fun=loss, x0=[0.0, 0.0], jac=gradient, method='L-BFGS-B')
fun: 9.7283268345966025
hess_inv: <2x2 LbfgsInvHessProduct with dtype=float64>
jac: array([ 7.28577538e-06, -2.35647522e-05])
message: 'CONVERGENCE: REL_REDUCTION_OF_F_<=_FACTR*EPSMCH'
nfev: 8
nit: 7
status: 0
success: True
x: array([ 2.00497209, 1.00822552])
As expected, the regression denoised our dataset, rebuilding the original equation: y = x + 2.
- Java語(yǔ)言程序設(shè)計(jì)
- Learn TypeScript 3 by Building Web Applications
- Progressive Web Apps with React
- Intel Galileo Essentials
- JavaScript高效圖形編程
- GeoServer Cookbook
- Cross-platform Desktop Application Development:Electron,Node,NW.js,and React
- R語(yǔ)言游戲數(shù)據(jù)分析與挖掘
- Visual C#.NET程序設(shè)計(jì)
- HTML5權(quán)威指南
- Python數(shù)據(jù)可視化之美:專業(yè)圖表繪制指南(全彩)
- Android高級(jí)開(kāi)發(fā)實(shí)戰(zhàn):UI、NDK與安全
- 零基礎(chǔ)學(xué)編程系列(全5冊(cè))
- Instant Pygame for Python Game Development How-to
- SQL Server 2014數(shù)據(jù)庫(kù)設(shè)計(jì)與開(kāi)發(fā)教程(微課版)