This tutorial shows how simple it is to implement a linear regression in python.
So a regression is about fitting a function (in the linear case a line) to given data points. Therefore the reconstruction cost is minimized such that for all datapoints x the squared distanze between its target value y and the function value f(x) of the fitted curve is minimal.
1) We need some data so create an array x with shape (100,1) that contains the numbers from -5 to 5.
2) Initialize the random number generator of numpy to 42.
3) Now add Gaussian random noise to x with a standard deviation of 1 and store the result in an array y.
4) Plot the datapoints as yellow dots in the range $x\in[-10,10]$ and $y\in[-10,10]$
A commen way to integrate a bias value for many machine learning methods is to add a dimension which is constant one for all datapoints!
1) Modify the code by adding a second constant dimension to x and add 10 to y to shift the datapoints verctically.
2) Now plot the result in the range $x\in[-10,10]$ and $y\in[0,20]$, notice that you have to select the first dimension of x in order not to plot the constant dimension!
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
By using a polynomial expansion of x we can fit a polynome to the data.
Fit a ploynome of degree 5 to the data
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
Now perform the same using the linear regression function np.polyfit(x,y,5) of numpy. Notice that x,y are 1D arrays here!