Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Exercise 2: Regression Using Least Squares

Write a program in python (ipython-notebook) to implement the least squares solution for the polynomial curve tting problem . Follow the instructions below:

1 . Generate 50 2D-data points using the following function:

2 . Add Gaussian random noise to the data . Look at the sample code here:

 

3 . Fit the generated noisy data using the least squares method based on a polynomial function . You must write two versions of the least squares solution:

(1) 

(2)  or using numpy's pinv function

4 . Show the original curve line, the noisy data, and the curve line estimated from the noisy data, like a figure below:

 

The red line is the original curve based on the .equation . The green dots are the noisy data . The blue line is the estimatedfitting line, by setting M = 4.

5 . Compute and display the error value, E(w)

6 . Display the estimated values of w

7 . Experiment with your code by changing M to various values . Plot the values of M against those of E(w), meaning: project them onto a 2D space, where the x-axis is M and the y-axis is E(w) . Try to understand the cases of underfitting and overfitting, and how they relate to the error value, E(w)

 

An example of the overtting problem .

8 . Change the noise levels, and apply the the same operations above

9 . Change the function for generating the data to:  , and apply the same operations as above

 

In your solutions, aside from the code, you need to include or show the results (as instructed above) in the ipython-notebook format.