How to program the Best Fit Slope – Practical Machine Learning Tutorial with Python p.8

Python

Video is ready, Click Here to View ×


Welcome to the 8th part of our machine learning regression tutorial within our Machine Learning with Python tutorial series. Where we left off, we had just realized that we needed to replicate some non-trivial algorithms into Python code in an attempt to calculate a best-fit line for a given dataset.

Before we embark on that, why are we going to bother with all of this? Linear Regression is basically the brick to the machine learning building. It is used in almost every single major machine…

20 thoughts on “How to program the Best Fit Slope – Practical Machine Learning Tutorial with Python p.8

  1. Children, when you see sentdex struggle in 9:11, always remember… Writing too complicated math expression is no no.
    Use the following breaks:
    def best_fit_slope(xs, ys):
    numerator = (np.mean(xs)*np.mean(ys)) – np.mean(xs*ys)
    denominator = np.mean(xs)**2 – np.mean(xs**2)
    return numerator/denominator

    Love you sentdex! keep up the good work!

  2. Hey sentdex. So I tried deriving the equation for a slope of best fit, but I got the wrong answer because instead of squaring all the residuals, I would simply take their absolute value. Why is it that we square the residuals? Wouldn't that make the line of best fit more susceptible to outliers? Why would we want that?

    Thanks so much for your tutorials, I've been following the series and they're awesome!

  3. Hello, my question is: given your definition of best_fit_slope(xs,ys) at 10:12. How many times the value of mean(xs) gets computed? Three times? Wouldn't it worth it to introduce a temporary variable?

  4. Would you please point out where is the mistake on the stock prediction that you said was fixed (the dates are not in the future but it predicts or take the prices of the existing dates).
    Kind regards.

  5. I know how to get coefficients in simple linear regression but I do not know how to get the coefficients or weights without using sklearn or other libraries! I cant find resources too! How can I write my own functions to do this?

  6. hey guys I am pretty new to coding. But I wrote this out myself before he did to see how i would do. I did it like this:

    def best_fit_slope (xs,ys):

    x_bar = np.mean(xs)
    y_bar = np.mean(ys)
    xy_bar = np.mean(xs*ys)

    m = (((x_bar*y_bar)-(xy_bar))/(
    (x_bar**2)-(mean(xs**2))))

    return m

    So I split up the means into their own variables then plugged them into the equation so it is easier to read. is there a reason this is bad? like is this slower than having it all in a mess of one line?

  7. I like your tutorials but kinda feel you wasted a lot of time on this one explaining how to manage parentheses and stuff, while you rushed through much more complicated things in the previous ones.

Leave a Reply

Your email address will not be published. Required fields are marked *