With a thorough understanding of the gradient descent based linear regression, we'll now implement it from scratch.
We start with defining the function computing the prediction with the current weights:
>>> def compute_prediction(X, weights):... """ Compute the prediction y_hat based on current weights... Args:... X (numpy.ndarray)... weights (numpy.ndarray)... Returns:... numpy.ndarray, y_hat of X under weights... """... predictions = np.dot(X, weights)... return predictions
Then, we can continue with the function updating the weight w by one step in a gradient descent manner, as follows:
>>> def update_weights_gd(X_train, ...