In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.