0% found this document useful (0 votes)
3 views

message (4)

The document outlines the implementation of Linear Regression and Ridge Regression models in Python using NumPy. It includes methods for fitting the models to training data, making predictions, and evaluating the models using metrics like RMSE and R². The code also demonstrates how to use these regression techniques with sample training and testing datasets.

Uploaded by

jjie9622
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

message (4)

The document outlines the implementation of Linear Regression and Ridge Regression models in Python using NumPy. It includes methods for fitting the models to training data, making predictions, and evaluating the models using metrics like RMSE and R². The code also demonstrates how to use these regression techniques with sample training and testing datasets.

Uploaded by

jjie9622
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

# Linear Regression models

# COMP.4220 Machine Learning

import numpy as np

class Regression(object):
pass

class LinearRegression(Regression):
"""Linear regression"""
def fit(self, x_train: np.ndarray, t_train: np.ndarray):
"""Perform least squares fitting.
Parameters
----------
x_train : np.ndarray
training independent variable (N, D)
t_train : np.ndarray
training dependent variable (N,)
"""
self.w = np.linalg.pinv(x_train) @ t_train
self.var = np.mean(np.square(x_train @ self.w - t_train))

def predict(self, x: np.ndarray, return_std: bool = False):


"""Return prediction given input.
Parameters
----------
x : np.ndarray
samples to predict their output (N, D)
return_std : bool, optional
returns standard deviation of each predition if True

Returns
-------
y : np.ndarray
prediction of each sample (N,)
y_std : np.ndarray
standard deviation of each predition (N,)
"""
y = x @ self.w
if return_std:
y_std = np.sqrt(self.var) + np.zeros_like(y)
return y, y_std
return y

class RidgeRegression(Regression):
"""Ridge regression"""
def __init__(self, lambd: float = 1.):
"""
Parameters
----------
lambda : float, optional
Regularization Coefficient
"""
self.lambd = lambd

def fit(self, x_train: np.ndarray, t_train: np.ndarray):


"""Maximum A Posteriori (MAP) estimation.
Parameters
----------
x_train : np.ndarray
training data independent variable (N, D)
y_train : np.ndarray
training data dependent variable (N,)
"""
# ---- Part (e) ---- #
ridge = RidgeRegression(lambda_=1.0)
ridge.fit(X_train, t_train)

# Make predictions
t_pred = ridge.predict(X_test)

# Evaluate the model using R² and RMSE


rmse = np.sqrt(mean_squared_error(t_test, t_pred))
r2 = r2_score(t_test, t_pred)

print(f'Ridge Regression RMSE: {rmse}')


print(f'Ridge Regression R²: {r2}')
self.w = []

def predict(self, x: np.ndarray):


"""Return prediction.
Parameters
----------
x : np.ndarray
samples to predict their output (N, D)

Returns
-------
np.ndarray
prediction of each input (N,)
"""
# ---- Part (f) ---- #
linreg = LinearRegression()
linreg.fit(X_train, t_train)

# Make predictions
t_pred_linreg = linreg.predict(X_test)

# Evaluate Linear Regression model


rmse_linreg = np.sqrt(mean_squared_error(t_test, t_pred_linreg))
r2_linreg = r2_score(t_test, t_pred_linreg)

print(f'Linear Regression RMSE: {rmse_linreg}')


print(f'Linear Regression R²: {r2_linreg}')
return x

You might also like