Least squares are suitable for direct python code

I have a scatter plot composed of X and Y coordinates. I want to use the minimum squares for a straight line to get the best fit line.

The smallest squares that fit the straight line are: If (x_1, y_1), .... (x_n, y_n) are measured data pairs, then the best line is y = A + Bx.

Here is my python code:

# 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) 

It looks right, I have problems printing A and B. Thank you!

+6
source share
3 answers

If I understand your question correctly, you have two sets of data x and y that you want to execute with the least square.

You do not need to write an algorithm yourself, curve_fit from scipy.optimize should do what you want, try:

 from scipy.optimize import curve_fit def f(x, A, B): # this is your 'straight line' y=f(x) return A*x + B A,B = curve_fit(f, x, y)[0] # your data x, y to fit 

A , B will be tilt and intercept for a straight line

for more details and examples see http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html#scipy.optimize.curve_fit

+10
source

The easiest way is if you just want the line to be scipy.stats.linregress :

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

Link to documents

+11
source

You are trying to propagate two lists x and y (or separately), but it is not defined in Python. You need to either write your own function to do the multiplication by the elements of the list, or use numpy.multiply . For example, if you want to do a multiple multiplication of x and y,

 import numpy as np xy = np.multiply(x,y) 
+1
source

All Articles