We have constructed a way to find the “best” b and m values using gradient descent! Let’s try this on the set of baseball players’ heights and weights that we saw at the beginning of the lesson.



Run the code in script.py.

This is a scatterplot of weight vs height.


We have imported your gradient_descent() function. Call it with parameters:

  • X
  • y
  • num_iterations of 1000
  • learning_rate of 0.0001

Store the result in variables called b and m.


Create a list called y_predictions. Set it to be every element of X multiplied by m and added to b.

The easiest way to do this would be a list comprehension:

new_y = [element*slope + intercept for element in y]

Plot X vs y_predictions on the same plot as the scatterplot.

Does the line look right?

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?