Least-Squares Fit to a Straight Line python code
Question: I have a scatter plot composed of X and Y coordinates. I want to use the
Least-Squares Fit to a Straight Line to obtain the line of best fit.

The Least-Squares Fit to a Straight Line refers to: If(x_1,y_1),....(x_n,y_n)
are measured pairs of data, then the best straight line is y = A + Bx.

Here is my code in python:

    
    
     # number of points is 50
     A = (sum(x**2)*sum(y) - sum(x)*sum(x*y)) / (50*sum(x**2) - (sum(x))**2)
     B = (50*sum(x*y) - sum(x)*sum(y)) / (50*sum(x**2) - (sum(x))**2)
     print (A,B)
    

Does this look correct, I'm having issues printing A and B. Thank you!


Answer: Simplest if you just want a line is `scipy.stats.linregress`:

    
    
    >>> from scipy import stats
    >>> slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
    

[Link to
docs](http://docs.scipy.org/doc/scipy-0.13.0/reference/generated/scipy.stats.linregress.html)

