How to program the Best Fit Line – Practical Machine Learning Tutorial with Python p.9



Welcome to the 9th part of our machine learning regression tutorial within our Machine Learning with Python tutorial series. We’ve been working on calculating the regression, or best-fit, line for a given dataset in Python. Previously, we wrote a function that will gather the slope, and now we need to calculate the y-intercept.

https://pythonprogramming.net

https://www.facebook.com/pythonprogramming.net/
https://plus.google.com/+sentdex

Related Posts

26 Comments

  1. Hello sentdex, I tried same code whatever u explained in Jupyter but not getting the best fit line due to this error in below syntax
    plt.plot(xs,regression_line) ValueError: setting an array element with a sequence. help me out.

  2. I'm having problems reproducing the best fit slope equation with some values.

    For example, if I use y = 2*x:
    xs = np.array([1,2,3,4,5, 6])
    ys = np.array([2,4,6,8,10,12])

    using the slope equation from the video gives a wrong answer, while adding the value 0 to xs and ys gives the right one.

    I ended up using another approach:
    def best_fit_slope_and_intercept(xs, ys):
    X = xs – xs.mean()
    Y = ys – ys.mean()
    m = X.dot(Y) / (X.dot(X))

    b = ys.mean() – m*xs.mean()

    return m, b

    found here:
    https://math.stackexchange.com/questions/2565098/vector-notation-for-the-slope-of-a-line-using-simple-regression/2642719#2642719

    and to check if it's okay:
    from scipy.stats import linregress
    print(linregress(xs, ys))

  3. just to say that every 6th grader is basically a data scientist/machine learner since one does that stuff in high school 🙂 Seriously, (as you already pointed out) linear regression is not a machine learning technique but a fundamental statistical tool (so are PCA, decision trees etc.) Every scientist uses that stuff every day and might have never heard of machine learning. Great tutorial anyway!!!

  4. Is there any way to do the regression line using numpy. I have read that using for loops or list comp is significantly slower than using numpy due to the vectors in numpy.

  5. great videos Harrison! My data science professor recommended your channel to me and this is like the 6th or 7th series I've been watching! Keep up the good work! You are such a great teacher!

  6. i have tried predicting some more point and i found that it is on the same line..so then it is correct or prediction is done according to regression line..i am not getting so please reply

  7. I really appreciate for the content being taught and the way they are taught.
    I think that the coding tutorials(starting ones) would have been better understood if these(ep 7,8,9) were taught first.

Leave a Reply

Your email address will not be published.

© 2022 Code As Pro - Theme by WPEnjoy · Powered by WordPress