Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use python to do data fitting

Shulou Source: shulou.com Published: 2022-06-01 08:30:55 09月30日 Update

This article will explain in detail how to use python to do data fitting. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

1. Example: fit a function Func, here is an exponential function.

Provenance:

SciPy v1.1.0 Reference Guide

# Headerimport numpy as npimport matplotlib.pyplot as pltfrom scipy.optimize import curve_fit#Define a function (here an exponential function is used) def func (x, a, b, c): return a * np.exp (- b * x) + c#Create the data to be fit with some noisexdata = np.linspace (0,4,50) y = func (xdata, 2.5,1.3,0.5) np.random.seed (1729) y_noise = 0.2 * np.random.normal (size=xdata.size) ydata = y + y_noiseplt.plot (xdata Ydata, 'bo', label='data') # Fit for the parameters aforme b, c of the function func:popt, pcov = curve_fit (func, xdata, ydata) popt # output: array ([2.55423706, 1.35190947, 0.47450618]) plt.plot (xdata, func (xdata, * popt),' r Mustang, label='fit: asides% 5.3f, baked% 5.3f, cations% 5.3f'% tuple (popt)) # In the case of parameters aforb C need be constrainted#Constrain the optimization to the region of # 0

Tags: Examples functions data code provenance articles files more middle students running good original practical exponential functions next content oneness commands addresses instructions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Information NVidia MySQL Shulou Technology