Question: Please complete the following code: ---- mport numpy as np from abc import ABC, abstractmethod # Super class for machine learning models class BaseModel(ABC):
Please complete the following code:
----
mport numpy as np from abc import ABC, abstractmethod # Super class for machine learning models class BaseModel(ABC): """ Super class for ITCS Machine Learning Class""" @abstractmethod def train(self, X, T): pass @abstractmethod def use(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(''.join(["Wrong matrix ", name])) # add a basis 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 train(self, X, T): """ train linear model parameters ----------- X 2d array input data T 2d array target labels """ pass @abstractmethod def use(self, X): """ apply the learned model to input X parameters ---------- X 2d array input data """ pass class LinearRegress(LinearModel): """ LinearRegress class attributes =========== w nd.array (column vector/matrix) weights """ def __init__(self): LinearModel.__init__(self) # train lease-squares model def train(self, X, T): pass ## TODO: replace this # apply the learned model to data X def use(self, X): pass ## TODO: replace this
class LMS(LinearModel): """ Lease Mean Squares. online learning algorithm attributes ========== w nd.array weight matrix alpha float learning rate """ def __init__(self, alpha): LinearModel.__init__(self) self.alpha = alpha # batch training by using train_step function def train(self, X, T): pass ## TODO: replace this # train LMS model one step # here the x is 1d vector def train_step(self, x, t): pass ## TODO: replace this # apply the current model to data X def use(self, X): pass ## TODO: replace this
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
