O'Reilly logo

Python Machine Learning By Example - Second Edition by Yuxi Liu

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required