%%! pip install --upgrade ipython # download the software import urllib urllib.urlretrieve('https://github.com/sods/ods/archive/master.zip', 'master.zip') # unzip the software import zipfile zip = zipfile.ZipFile('./master.zip', 'r') for name in zip.namelist(): zip.extract(name, '.') # add the module location to the python path. import sys sys.path.append("./ods-master/") import numpy as np # import numpy for the arrays. def quadratic(x): """Take in a vector of input values and return the design matrix associated with the basis functions.""" return np.hstack([np.ones((n, 1)), x, x**2]) # ensure plots appear in the notebook. %matplotlib inline import pylab as plt # first let's generate some inputs n = 100 x = np.zeros((n, 1)) # create a data set of zeros x[:, 0] = np.linspace(-1, 1, n) # fill it with values between -1 and 1 Phi = quadratic(x) fig, ax = plt.subplots(figsize=(12,4)) ax.set_ylim([-1.2, 1.2]) # set y limits to ensure basis functions show. ax.plot(x[:,0], Phi[:, 0], 'r-', label = '$\phi=1$') ax.plot(x[:,0], Phi[:, 1], 'g-', label = '$\phi = x$') ax.plot(x[:,0], Phi[:, 2], 'b-', label = '$\phi = x^2$') ax.legend(loc='lower right') ax.set_title('Quadratic Basis Functions') def polynomial(x, num_basis=4, data_limits=[-1., 1.]): Phi = np.zeros((x.shape[0], num_basis)) for i in xrange(num_basis): Phi[:, i:i+1] = x**i return Phi import pods pods.notebook.display_prediction(basis=polynomial, num_basis=4) fig, ax = plt.subplots(figsize=(12,4)) plt.close(fig) from IPython.display import display display(fig) def rbf(x, num_basis=4, data_limits=[-1., 1.]): centres=np.linspace(data_limits[0], data_limits[1], num_basis) width = (centres[1]-centres[0])/2. Phi = np.zeros((x.shape[0], num_basis)) for i in xrange(num_basis): Phi[:, i:i+1] = np.exp(-0.5*((x-centres[i])/width)**2) return Phi pods.notebook.display_prediction(basis=rbf, num_basis=4) def fourier(x, num_basis=4, data_limits=[-1., 1.]): tau = 2*np.pi span = float(data_limits[1]-data_limits[0]) Phi = np.zeros((x.shape[0], num_basis)) for i in xrange(num_basis): count = float((i+1)/2) frequency = count/span if i % 2: Phi[:, i:i+1] = np.sin(tau*frequency*x) else: Phi[:, i:i+1] = np.cos(tau*frequency*x) return Phi pods.notebook.display_prediction(basis=fourier, num_basis=4) data = pods.datasets.olympic_marathon_men() y = data['Y'] x = data['X'] y -= y.mean() y /= y.std() def polynomial(x, num_basis=4, data_limits=[-1., 1.]): centre = data_limits[0]/2. + data_limits[1]/2. span = data_limits[1] - data_limits[0] z = x - centre z = 2*z/span Phi = np.zeros((x.shape[0], num_basis)) for i in xrange(num_basis): Phi[:, i:i+1] = z**i return Phi import pods #x[:, 0] = np.linspace(1888, 2020, 1000) fig, ax = plt.subplots(figsize=(12,4)) ax.plot(x, y, 'rx') pods.notebook.display_prediction(basis=dict(rbf=rbf, polynomial=polynomial, fourier=fourier), data_limits=(1888, 2020), fig=fig, ax=ax, offset=0., wlim = (-4., 4., 0.001), num_basis=4) # Question 3 Answer Code # provide the answers so that the code runs correctly otherwise you will loose marks! # (a) polynomial ###### Edit these lines ##### w_0 = w_1 = w_2 = w_3 = ############################## w_polynomial = np.asarray([[w_0], [w_1], [w_2], [w_3]]) # (b) rbf ###### Edit these lines ##### w_0 = w_1 = w_2 = w_3 = ############################## w_rbf = np.asarray([[w_0], [w_1], [w_2], [w_3]]) # (c) fourier ###### Edit these lines ##### w_0 = w_1 = w_2 = w_3 = ############################## w_fourier = np.asarray([[w_0], [w_1], [w_2], [w_3]]) np.asarray([[1, 2, 3, 4]]).shape # Question 5 Answer Code # Write code for you answer to this question in this box # Do not delete these comments, otherwise you will get zero for this answer. # Make sure your code has run and the answer is correct *before* submitting your notebook for marking. from IPython.display import YouTubeVideo YouTubeVideo('PoNbOnUnOao')