# file: lecture_13/p01.py # # revision history: # # 20230928 (JP): initial version # # Solution to the first exam problem. # # To run this script, do this: # # python p01.py # #------------------------------------------------------------------------------ # import required system modules # import os import sys import math import numpy as np #------------------------------------------------------------------------------ # # Let's try a 2x2 where the solution is exact # #------------------------------------------------------------------------------ # declare a matrix X # X = np.array([[1, 0], [0, 2]]) print(X) Y = np.array([[1], [3]]) print(Y) # find a = (X^TX)^-1 X^T Y # X_t = np.transpose(X) a = (np.linalg.inv(X_t @ X) @ X_t) @ Y print(a) # check the result # y_est = X @ a print(y_est) # how would you compute the mean-square error? # MSE = np.square(np.subtract(Y, y_est)).mean() print("MSE = %f" % (MSE)) print("----") #------------------------------------------------------------------------------ # # Let's try the exam problem # #------------------------------------------------------------------------------ # declare a matrix X # X = np.array([[1, 0], [0, 1], [1, 1]]) print(X) Y = np.array([[1], [1], [1]]) print(Y) # find a = (X^TX)^-1 X^T Y # X_t = np.transpose(X) a = (np.linalg.inv(X_t @ X) @ X_t) @ Y print(a) # check the result # y_est = X @ a # how would you compute the mean-square error? # MSE = np.square(np.subtract(Y, y_est)).mean() print("MSE = %f" % (MSE)) print("----") # # end of file