polyreg py import numpy as np from numpy random import rand randn from numpy linalg import norm solve import matplotlib pyplot as plt def generate_data (beta sig ) np random rand( ( *np arange ( )beta sig np random randn ( return uy np random seed ( beta np array ([[ - - ]] sig uy generate_data (beta sig nxx np arange (np min( )np max( )+ - - yy np polyval (np flip(beta)xxplt plot(uy'markersize = plt plot(xx yy '--',linewidth = plt xlabel ( '$ 'plt ylabel ( '$ ^*( )$'plt legend (['data points ','true ']plt show (the following codewhich imports the code abovefits polynomial models with parameters to the training data and plots selection of fitted curvesas shown in figure polyreg py from polyreg import max_p p_range np arange ( max_p np ones (( )betahat trainloss {}{for in p_range is the number of parameters if np hstack ((xu**( - ))add column to matrix betahat [psolve ( xx ytrainloss [ (norm( betahat [ ]** /np [ select three curves replot the points and true line and store in the list plots plots [plt plot(uy' 'markersize = [ plt plot(xx yy ' --',linewidth = [ ]add the three curves for in pyy np polyval (np flipbetahat [ ]xxplots append (plt plot(xx yy)[ ]