In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to use Python to fit function curves". In daily operation, I believe many people have doubts about how to use Python to fit function curves. Xiaobian consulted all kinds of data and sorted out simple and easy operation methods. I hope to help you answer the doubts about "how to use Python to fit function curves"! Next, please follow the small series to learn together!
Fitting functional curves using Python requires some third-party libraries:
numpy: fundamental library for scientific computation (e.g. matrix)
matplotlib: drawing gallery
scipy: scientific computing library
If these libraries are not installed, you need to enter the following code on the command line to install them:
pip install numpy matplotlib scipy fitting polynomial '' Author: CloudSirDate: 2021 - 08 - 01 13:40:50 LastEditTime: 2021 - 08 - 02 09:41:54 LastEditors: CloudSirDescription: Python fitting polynomials https://github.com/cloudsir '''import matplotlib. pyplot as pltimport numpy as np x =[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] y =[2.83, 9.53, 14.52, 21.57, 38.26, 53.92, 73.15, 101.56, 129.54, 169.75, 207.59] z1 = np. polyfit (x, y, 3)#Fit with a polynomial of degree 3, output coefficients from high to 0 p1 = np.poly1d (z1)#Use a synthetic polynomial of degree y_pre = p1 (x) plt. plot (x, y,'. ')plt.plot(x,y_pre)plt.show()
function description
np.polyfit(x, y, n)
Function: Fitting curve
Parameters:
x, y: raw data of x and y
n: Number of times to fit
Return value:
A list of fitted coefficients in descending order
Example: When n = 3, it will be used
a x 3 + b x 2 + c x + d
Fit the function and return the fitted coefficients [a, b, c, d]
np.poly1d(li, r=False)
Function: Generator polynomial function
Parameters:
li:
When there is no r parameter or r = False, pass in a list of coefficients (from high to low degree), generate polynomial function from this list and return
import numpy as np
f = np.poly1d([2, 3, 4])
""" f ( x ) = 2 x 2 + 3 x + 4
"""
print(f(2)) # 18
When parameter r = True, pass in a root list, use this list to generate polynomial function and return
import numpy as np
f = np.poly1d([2, 3, 4], True)
""" f ( x ) = ( x − 2 ) ∗ ( x − 3 ) ∗ ( x − 4 )
"""
print(f(0)) # -24
Return value:
see above
Fitting Arbitrary Functions '' Author: CloudSirDate: 2021 - 08 - 03 15:01:17 LastEditTime: 2021 - 08 - 03 15:26:05 LastEditors: CloudSirDescription: Python Fitting Arbitrary Functions https://github.com/cloudsir ''#Reference Library Functions import numpy as npimport matplotlib. pyplot as pltfrom scipy import optimize as opplt. rcParams <$'font. sans-serif ']=<$'SimHei']#Used to display Chinese plt. rcParams <$'axes. unicode_minus']= False #Used to display negative signs properly #Function def f_1 (x, A, B, C) to fit: return A * x ** 2 + B * x + C#Data group to be fitted x_group =[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] y_group =[2.83, 9.53, 14.52, 21.57, 38.26, 53.92, 73.15, 101.56, 129.54, 169.75, 207.59]#Get the returned A, B values A, B, C = op. curve_fit (f_1, x_group, y_group)[0]#Plot data points against original plt.scatter (x_group, y_group, marker ='o ', label ='true value') x = np. range (0, 15, 0.01) y = A * x ** 2 + B * x + Cplt. plot (x, y, color ='red ', label ='fitted curve') plt. legend ()#show labelplt.show ()
function description
op.curve_fit(f, x, y)
Function: Fitting arbitrary function
Parameters:
f: Type of function to fit
#Build a quadratic function def f (x, A, B, C): return A * x ** 2 + B * x + Cop. curve_fit (f, x, y)#
x, y: raw data of x and y
Return value: a tuple (popt, pcov)
popt is a one-dimensional array representing the parameters of the resulting fit equation.
pcov is a two-dimensional array of covariances obtained under the popt parameter.
At this point, the study of "how to use Python to fit function curves" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
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.