TODO: Lienar Regression with least Mean Squares (LMS) Optimize the model through gradient descent. *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 LeastMeanSquares(LinearModel):     """         Performs regression using least mean squares (gradient descent)         attributes:             w (np.ndarray): weight matrix             alpha (float): learning rate or step size             epochs (int): Number of epochs to run for mini-batch                 gradient descent             seed (int): Seed to be used for NumPy's RandomState class                 or universal seed np.random.seed() function.     """     def __init__(self, alpha: float, epochs: int, seed: int = None):         super().__init__()         self.w = None         self.alpha = alpha         self.epochs = epochs         self.seed = seed     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 LMS in order to learn the                 weights `self.w`.         """         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:                 Finish this method by adding code to make a prediction given the learned                 weights `self.w`.         """         # 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: Lienar Regression with least Mean Squares (LMS)

Optimize the model through gradient descent. *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 LeastMeanSquares(LinearModel):
    """
        Performs regression using least mean squares (gradient descent)

        attributes:
            w (np.ndarray): weight matrix

            alpha (float): learning rate or step size

            epochs (int): Number of epochs to run for mini-batch
                gradient descent

            seed (int): Seed to be used for NumPy's RandomState class
                or universal seed np.random.seed() function.
    """

    def __init__(self, alpha: float, epochs: int, seed: int = None):
        super().__init__()
        self.w = None
        self.alpha = alpha
        self.epochs = epochs
        self.seed = seed

    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 LMS in order to learn the
                weights `self.w`.
        """
        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:
                Finish this method by adding code to make a prediction given the learned
                weights `self.w`.
        """
        # 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
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Temporal Difference Learning
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
  • SEE MORE 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