Anonymous Anonymous - 1 month ago
131 0

No description

Python

LR Q3 p1

def __init__(self, X, y):
        """ Initialize the linear regression model by computing the estimate of the weights parameter
            Args: 
                X (array-like) : feature matrix of training data where each row corresponds to an example
                y (array like) : vector of training data outputs 
            """
        
        self.beta = la.cho_solve(la.cho_factor(np.add(X.T.dot(X), np.identity(X.shape[1])*(10**-4))), np.dot(X.T,y))
        #you have to do this in one line otherwise youg et memory errors
        
        
    def predict(self, X_p): 
        """ Predict the output of X_p using this linear model. 
            Args: 
                X_p (array_like) feature matrix of predictive data where each row corresponds to an example
            Return: 
                (array_like) vector of predicted outputs for the X_p
            """
  
        return X_p.dot(self.beta)