TODO: Polynomial Regression with Ordinary Least Squares (OLS) and Regularization *Please complete the TODOs. * !pip install wget import os import random import traceback from pdb import set_trace import sys import numpy as np from abc import ABC, abstractmethod import traceback from util.timer import Timer from util.data import split_data, feature_label_split, Standardization from util.metrics import mse from datasets.HousingDataset import HousingDataset class BaseModel(ABC):     """ Super class for ITCS Machine Learning Class"""     @abstractmethod     def fit(self, X, y):         pass     @abstractmethod     def predict(self, X):         pass class LinearModel(BaseModel):     """         Abstract class for a linear model         Attributes         ==========         w       ndarray                 weight vector/matrix     """     def __init__(self):         """             weight vector w is initialized as None         """         self.w = None     # check if the matrix is 2-dimensional. if not, raise an exception     def _check_matrix(self, mat, name):         if len(mat.shape) != 2:             raise ValueError(f"Your matrix {name} shape is not 2D! Matrix {name} has the shape {mat.shape}")     # add a biases     def add_ones(self, X):         """             add a column basis to X input matrix         """         self._check_matrix(X, 'X')         return np.hstack((np.ones((X.shape[0], 1)), X))     ####################################################     #### abstract funcitons ############################     @abstractmethod     def fit(self, X: np.ndarray, y: np.ndarray):         """             train linear model             Args:                 X:  Input data                 y:  targets/labels         """         pass     @abstractmethod     def predict(self, X: np.ndarray):         """             apply the learned model to input X             parameters             ----------             X     2d array                   input data         """         pass class PolynomialRegressionRegularized(PolynomialRegression):     """         Performs polynomial regression with l2 regularization using the ordinary least squares algorithm              attributes:             w (np.ndarray): weight matrix that is inherited from OrdinaryLeastSquares                          degree (int): the number of polynomial degrees to include when adding                 polynomial features.     """     def __init__(self, degree: int, lamb: float):         super().__init__(degree)         self.lamb = lamb              def fit(self, X: np.ndarray, y: np.ndarray) -> None:         """ Used to train our model to learn optimal weights.                      TODO:                 Finish this method by adding code to perform polynomial regression using                 the closed form solution OLS with L2 regularization to learn                  the weights `self.w`.                              Hint:                 Add the bias after computing polynomial features. Typically we don't want                 to include the bias when computing polynomial features.         """         pass # TODO replace this line with your code          def predict(self, X: np.ndarray) -> np.ndarray:         """ Used to make a prediction using the learned weights.             TODO:                 This predict() method is exactly the same as the predict() method in the above class `PolynomialRegression`,                  so you can just simply copy them here.          """         # TODO (REQUIRED) Add code below         # TODO (REQUIRED) Store predictions below by replacing np.ones()         y_hat = np.ones([len(X), 1])         return y_hat

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

TODO: Polynomial Regression with Ordinary Least Squares (OLS) and Regularization

*Please complete the TODOs. *

!pip install wget

import os
import random
import traceback
from pdb import set_trace
import sys
import numpy as np
from abc import ABC, abstractmethod
import traceback

from util.timer import Timer
from util.data import split_data, feature_label_split, Standardization
from util.metrics import mse
from datasets.HousingDataset import HousingDataset

class BaseModel(ABC):
    """ Super class for ITCS Machine Learning Class"""

    @abstractmethod
    def fit(self, X, y):
        pass

    @abstractmethod
    def predict(self, X):
        pass

class LinearModel(BaseModel):
    """
        Abstract class for a linear model

        Attributes
        ==========
        w       ndarray
                weight vector/matrix
    """

    def __init__(self):
        """
            weight vector w is initialized as None
        """
        self.w = None

    # check if the matrix is 2-dimensional. if not, raise an exception
    def _check_matrix(self, mat, name):
        if len(mat.shape) != 2:
            raise ValueError(f"Your matrix {name} shape is not 2D! Matrix {name} has the shape {mat.shape}")

    # add a biases
    def add_ones(self, X):
        """
            add a column basis to X input matrix
        """
        self._check_matrix(X, 'X')
        return np.hstack((np.ones((X.shape[0], 1)), X))

    ####################################################
    #### abstract funcitons ############################
    @abstractmethod
    def fit(self, X: np.ndarray, y: np.ndarray):
        """
            train linear model

            Args:
                X:  Input data

                y:  targets/labels
        """
        pass

    @abstractmethod
    def predict(self, X: np.ndarray):
        """
            apply the learned model to input X

            parameters
            ----------
            X     2d array
                  input data

        """
        pass

class PolynomialRegressionRegularized(PolynomialRegression):
    """
        Performs polynomial regression with l2 regularization using the ordinary least squares algorithm
    
        attributes:
            w (np.ndarray): weight matrix that is inherited from OrdinaryLeastSquares
            
            degree (int): the number of polynomial degrees to include when adding
                polynomial features.
    """

    def __init__(self, degree: int, lamb: float):
        super().__init__(degree)
        self.lamb = lamb
        
    def fit(self, X: np.ndarray, y: np.ndarray) -> None:
        """ Used to train our model to learn optimal weights.
        
            TODO:
                Finish this method by adding code to perform polynomial regression using
                the closed form solution OLS with L2 regularization to learn 
                the weights `self.w`.
                
            Hint:
                Add the bias after computing polynomial features. Typically we don't want
                to include the bias when computing polynomial features.
        """
        pass # TODO replace this line with your code
    
    def predict(self, X: np.ndarray) -> np.ndarray:
        """ Used to make a prediction using the learned weights.

            TODO:
                This predict() method is exactly the same as the predict() method in the above class `PolynomialRegression`, 
                so you can just simply copy them here. 
        """
        # TODO (REQUIRED) Add code below

        # TODO (REQUIRED) Store predictions below by replacing np.ones()
        y_hat = np.ones([len(X), 1])

        return y_hat

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Mergesort
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education