简体   繁体   中英

Least-Squares Fit to a Straight Line python code

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!

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

If I understand your question correctly, you have two datasets x and y where you want to perform a least square fit.

You don't have to write the 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

popt, pcov = curve_fit(f, x, y) # your data x, y to fit

where popt[0] , popt[1] would be the slope and intercept of the 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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM