We first choose a model
We then decide a metric to evaluate how well the model is performing.
Finally, we search for the parameters
Approach | Pros | Cons |
---|---|---|
Exact solution | Efficient | Solvable equations, overfitting |
Least square | Efficient | Linear model, quadratic error |
Netwon's method | Any model | Suboptimal, can diverge |
Gradient descent | Any model | Suboptimal, slow |
Linear models are those of the forms:
Where
Note: the models are linear in the parameters
Which models are linear ?
Which models are linear ?
Given some dataset, we can first search for a solution that exactly fits the data.
In that case,
This approach is especially useful for interpolation.
Let's start with the simplest example:
What is the model ? How to find
First, let's define the model:
We can then write the equations:
This yields the system of equations:
In matrix form:
The solution is then:
We can also impose derivatives:
What are
We have:
Then:
Now, what if we have 6 data points and want to find a model ?
How do you expect
The output will look like this:
We can notice the following:
Suppose we have many data points and wand to find
If we write the system as seen before:
Problem:
This is because we only have two parameters, but many data points.
Idea: instead of finding
We will call this error
The most common error is the least squares error:
We then want to find
We have a linear model and a square error.
Back to the linear regression problem:
Can you find an equation to find
First, let's write
We can expand it to:
Then, we can compute the derivatives with respect to
To cancel the derivatives, we then have:
Thus:
Substitute in the second equation:
This yields:
We can then write some code to solve the linear regression problem:
Remember that we can write the exact form equation as:
The loss function is then:
This expands to:
The derivatives can then be computed as:
Setting the derivative to zero yields:
And thus:
The least square solution can then be obtained with the exact same approach as the exact solution.
import numpy as np
np.linalg.inv(A) # inverse
np.linalg.pinv(A) # pseudo-inverse
We can then write some code using numpy.linalg.pinv
to solve the linear regression problem. Yielding the same result:
On the next slide, you will find data and models.
Using the linear regression example, you can write some code to find the parameters used!
Hint: to load the data, you can use:
import numpy as np
data = np.loadtxt('data.csv')
Let us consider the following RR robot:
Can you express
We have:
The model is linear in the parameters
Here are some data generated with this script.
They contain noisy measurements of
import numpy as np
data = np.loadtxt('data/rr_data.csv')
alpha, beta, x_e, y_e = data[0] # first data
Write some code to retrieve parameters