EECS 445 Introduction to Maching Learning
HW1 Q6 Linear Regression Optimization Methods)
~~~~~~
Follow the instructions in the homework to complete the
assignment.
"""
import numpy as np
import math
import matplotlib.pyplot as plt
from helper import load_data
def calculate_squared_loss(X, y, theta):
loss = 0.0
for i in range(y.size):
loss = loss + ((y[i]np.dot(theta, X[i]))**2)/2
loss = loss/y.size
return loss
def calculate_RMS_Error(X, y, theta):
"""
Args:
X: np.array, shape (n, d)
y: np.array, shape (n,)
theta: np.array, shape (d,). Specifies an (d1)^th degree
polynomial
Returns:
E_rms: float. The root mean square error as defined in the
assignment.
"""
X = generate_polynomial_features(X, len(X[0]))
theta = 0.0
for i in range(len(y)):
theta += (np.dot(theta, X[i])y[i])**2
E_rms = math.sqrt(theta / len(y))
return E_rms
def generate_polynomial_features(X, M):
"""
Create a polynomial feature mapping from input examples. Each
element x
in X is mapped to an (M+1)dimensional polynomial feature
This study source was downloaded by 100000850872992 from CourseHero.com on 02-15-2023 06:36:05 GMT -06:00
https://www.coursehero.com/file/36332137/q6-linear-regressionpy/
, vector
i.e. [1, x, x^2, ...,x^M].
Args:
X: np.array, shape (n, 1). Each row is one instance.
M: a nonnegative integer
Returns:
Phi: np.array, shape (n, M+1)
"""
Phi = np.zeros((X.shape[0],M+1))
for i in range(len(X)):
for j in range(M+1):
Phi[i][j] = pow(X[i], j)
return Phi
def ls_gradient_descent(X, y, learning_rate=0):
"""
Implements the Gradient Descent (GD) algorithm for least
squares regression.
Note:
Please use the stopping criteria: number of iterations
>= 1e6 or |new_loss 10
Args:
X: np.array, shape (n, d)
y: np.array, shape (n,)
Returns:
theta: np.array, shape (d,)
"""
d = X.shape[1]
theta = np.zeros(d+1)
X = generate_polynomial_features(X, 1)
k = 0
new_loss = calculate_squared_loss(X, y, theta)
prev_loss = 0
while k < 1e6 and abs(new_lossprev_loss) >1e10:
prev_loss = new_loss
update = np.zeros(X[0].size)
This study source was downloaded by 100000850872992 from CourseHero.com on 02-15-2023 06:36:05 GMT -06:00
https://www.coursehero.com/file/36332137/q6-linear-regressionpy/