Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of least Squares method realized by Python data fitting

Shulou Source: shulou.com Published: 2022-06-03 02:50:43 10月01日 Update

Today, I will talk to you about the example analysis of Python data fitting to achieve the least square method. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Linear fitting

This expression is still very simple.

For some cases, we often choose the natural sequence as the independent variable. At this time, we can use some elementary mathematical inferences when finding the value of the independent variable. For the natural sequence of x ∈ [m, n], there are

# filename core.pyimport numpy as npdef leastSquare (x Y): if len (x) = = 2: # then x is the natural sequence sx = 0.5* (x [1]-x [0] + 1) * (x [1] + x [0]) ex = sx/ (x [1]-x [0] + 1) sx2 = ((x [1] * (x [1] + 1) * (2x1 [1] + 1)-(x [0]) * (x [0]-1) * (2x [0]-1)) / 6 x = np.array (range (x [0]) X [1] + 1)) else: sx = sum (x) ex = sx/len (x) sx2 = sum (Xeroy) sxy = sum (Xeroy) ey = np.mean (y) a = (sxy-ey*sx) / (sx2-ex*sx) b = (ey*sx2-sxy*ex) / (sx2-ex*sx) return

Test it

> x = np.arange (25) > y = x*15+20+np.random.randn (len (x)) * 5 # randn generates normal distribution noise > a core.leastSquare b = core.leastSquare (xpeny) > plt.scatter (xmagin y) # Raw data scatter plot > plt.plot (x A*x+b) # fit Line [] > plt.show ()

Get

Higher order polynomial

As before, the agreement

The code is as follows

# the format of the input parameters is np.array,n as order def leastSquareMulti (xjinyjinn): X = [np.sum (xmagedi) for i in range (2*n+1)] Y = np.array ([[np.sum (y*x**i)] for i in range (nasty 1)]) S = np.array ([X [I: i+n+1] for i in range (nasty 1)]) return np.linalg.solve (SMagy) #

The test results are as follows:

> x = np.arange (25) > y = x**3+3*x**2+2*x+12 > import core > core.leastSquareMulti ([[12.], # this is a constant term [2.], [3.], [1.]) Multiple independent variables

For samples

Then the corresponding error equations can be expressed as

Exponential function

Its code is

Def expFit (XMague y): Y0 = y [0:-3] y1 = y [1:-2] y2 = y [2:-1] BMague C = leastSquare (y2/y0 Y1/y0) b1 = np.log ((B-np.sqrt (Background2Benz4C)) / 2) b2 = np.log ((B+np.sqrt (Bathy4C) / 2) X = np.exp (b1-b2) * x = y/np.exp (b2yogx) A1 Magi a2 = leastSquare (XMY) return a1Magi a2menb1Personb2

After reading the above, do you have any further understanding of the example analysis of Python data fitting to realize the least square method? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Tags: Data content sequence independent variables natural minimum multiplication examples analysis code testing primitive exponential functions functions elementary mathematics parameters noise polynomials constants cases Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Technology OPPO Reno Microsoft MariaDB