Implementing linear regression

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 =, 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, ...

Get Python Machine Learning By Example - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.