Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the leastsq function in python

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to use the leastsq function in python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Leastsq action: minimizes the sum of squares of a set of equations.

Parameter settings:

Func error function

Parameters initialized by X0

Other additional parameters of args

For example:

First create a sample point

Import numpy as npimport scipy as spfrom scipy.optimize import leastsqimport matplotlib.pyplot as pltplt.rcParams ['font.sans-serif'] = [' SimHei'] plt.rcParams ['axes.unicode_minus'] = Falsex= [1meme 2meme 3meme 4] y = [2pjong 3meme 4meme 5]

Fitting line

Def y_pre (pmaine x): f=np.poly1d (p) return f (x)

Among them, np.polyld

F=np.poly1d ([1Jing 2jue 3]) # x ^ 2 + 2x+3f (1) "" 6 "

Error function

Def error (pmaine x ~ (y)): return y-y_pre (p ~ ~ () x)

And then it's easy.

P = [1Magne2] # write the value casually # y=w1*x+w2res=leastsq (error,p,args= (XPenagy)) w1gramres [0] # res [0] is the parameter list of wi "at this point w1 and w2 have been found. Here is a drawing to take a look at the" x_=np.linspace (1Min 10100) # isometric sequence. The fitting curve obtained by y_p=w1*x_+w2 # plt.scatter (XMagne y) # sample point plt.plot (Xerogramme) # draw the fitting curve

Can be directly encapsulated into a function

X=np.linspace (0jing2jing10) y=np.sin (np.pi*x) # original sample yearly = [y + np.random.normal (0Power0.1) for y in y] # np.random.normal (loc,scale,size): the mean of the normal distribution, the standard deviation of the normal distribution, the shape # np.random.randn () # the standard normal distribution is the normal distribution with 0 as the mean and 1 as the standard deviation, marked as N (0) 1) def fit (multiple 1): p=np.random.rand (multiple 1) # returns a random sample value or a set of random sample values that obey the uniform distribution of "zero one". The range of random sample values is [0Magne1) res=leastsq (error,p,args= (XMagany)) # wi value x_point=np.linspace (0Magne2100) # increase the amount of data to smooth y_point=np.sin (np.pi*x_point) # increase the amount of data in order to smooth plt.plot (x_point) Y_pre (res [0], x_point), 'baked mahogany labelings' fit') plt.scatter (XMagne yy) plt.legend () fit (3)

You can also output the middle result:

X=np.linspace (0jing2jing10) y=np.sin (np.pi*x) # original sample yearly = [y + np.random.normal (0Power0.1) for y in y] # np.random.normal (loc,scale,size): the mean of the normal distribution, the standard deviation of the normal distribution, the shape # np.random.randn () # the standard normal distribution is the normal distribution with 0 as the mean and 1 as the standard deviation, marked as N (0) 1) def fit (multiple 1): p=np.random.rand (multiple 1) # returns a random sample value or a set of random sample values that obey the uniform distribution of "zero one". The range of random sample values is [0Power1) res=leastsq (error,p,args= (xpeny)) # wi's value x_point=np.linspace (0meme2100) y_point=np.sin (np.pi*x_point) plt.plot (xpenalty pointline) plt.plot (res [0], x_point),'b' Label=' fitting') print (res [0]) plt.scatter (xQuery y _) plt.legend () fit (3)

The fitting straight line is:

This is the end of the content of "how to use the leastsq function in python". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report