I would like to understand what I am doing wrong here. I am trying to perform polynomial regression by minimizing the least squares, ||Au-y||^2, where y is the given data and A is the matrix where the i-th line holds [1, x_i, (x_i)^2, ... (x_i)^n-1].
If n is larger than the number of data points, the problem is underdetermined, and I expect the numpy.linalg.lstsq() routine to give any of the infinitely possible solutions. But, as you can see, I don't get a solution at all.
import matplotlib.pyplot as plt import numpy as np x = np.array([-6 ,1, 2, 3, 4]) # x data y = np.array([2, -3, 4, 20, -10]) # y data A =  n = 50 # polynomial degree for i in range(0,n): # create A matrix of proper form: i-th line is [1, x_i, x_i**2, ...] A.append(x**i) A=np.array(A) A=A.T u=np.linalg.lstsq(A,y, rcond=None) # solve underdetermined problem x_test=np.linspace(-6, 5, 100) # create more x values for plotting B= for i in range(0,n): # same as before, their power matrix B.append(x_test**i) B=np.array(B) B=B.T plt.plot(x_test, B@u) # plot polynom plt.scatter(x,y) # plot data plt.show() print(A@u-y) # this should be zero vector?? ```