Experiment data fitting (Python) being added ...

Overview

Here, we aim to determine the parameters by fitting the experimental data from the state obtained in the txt file or dat file. In order to use the function regardless of whether it is linear or non-linear, we use a module called curve_fit, which is a part of the scipy.optimize module included in Python's scipy package.

Linear function fitting

First, load the required modules.

#Required for fitting
from scipy.optimize import curve_fit
import numpy as np
#Required for illustration
import matplotlib.pyplot as plt

Try fitting the following data.

x = np.asarray([1,2,4,6,7])
y = np.asarray([1,3,3,5,4])
plt.scatter(x, y, c='k')
plt.show()

fit1.png Define the function form to be fitted and actually perform the fitting.

#Define the function expression you want to fit. Here is a linear function.
def linear_fit(x, a, b):
    return a*x + b
#Perform fitting.
#prameter_initial = np.array([1,2])← Set the initial value here, but skip it here.
popt, pcov = curve_fit( linear_fit, x, y)#, p0= prameter_initial)

Fitting is done so far, and the results of parameters a and b are stored in the first return value popt. This time, the two parameters of the slope a and the intercept b of the linear function are included in popt as [a, b] = [popt [0], popt [1]]. Check what the value is.

print (popt[0],popt[1])

Maybe you can find a = 0.4999 ..., b = 1.2000 ... Plot overlaid to see if this value is valid. It doesn't look good.

c=popt[0]
d=popt[1]
f = c*x + d #f = linear_fit(x,popt[0],popt[1])Or f= linear_fit(x,*popt)But OK.
plt.scatter(x, y, c='k')
plt.plot(x, f, c='r')
plt.show()

fit2.png

Quadratic function fitting

Similarly, load the required modules.

#Required for fitting
from scipy.optimize import curve_fit
import numpy as np
#Required for illustration
import matplotlib.pyplot as plt

Try fitting the following data.

x = np.asarray([1,2,2.5,4,5,6,7,8,9,10])
y = np.asarray([10,8,5,2,1,1.2,2.5,5,7,10])
plt.scatter(x, y, c='k')
plt.show()

fit3.png Define the function form to be fitted and actually perform the fitting.

#Define the function expression you want to fit. Here is a quadratic function.
def two_fit(x,a,b):
    return  a*(x-b)**2

#Perform fitting. The initial value is also set.
prameter_initial = np.array([1,5])
popt, pcov = curve_fit(two_fit, x, y, p0= prameter_initial)

Fitting is done so far, and the results of parameters a and b are stored in the first return value popt. This time, the two parameters of the slope a and the intercept b of the linear function are included in popt as [a, b] = [popt [0], popt [1]]. Check what the value is.

print (popt[0],popt[1])

Maybe you can find a = 0.5404046977156955, b = 5.48472528172034. Plot overlaid to see if this value is valid. Although not explained in the case of the linear function, there are two plotting methods at this time. First, try the same method as for a linear function.

c=popt[0]
d=popt[1]
f = c*(x-d)**2 #f = two_fit(x,popt[0],popt[1])#You can write like this, f= two_fit(x,*popt)But OK.
plt.scatter(x, y, c='k')
plt.plot(x, f, c='r')
plt.show()

fit4.png I didn't know when it was a linear function, but you can see that it's jerky. If there is a lot of data, this doesn't bother me, but I don't like the way it looks, so I'll write it smoothly as the second method.

#Specifying the drawing range
# x1 = np.arange(x-axis minimum,Maximum x-axis,Notched)
x1 = np.arange(1, 10, 0.000001)
c=popt[0]
d=popt[1]
f = c*(x1-d)**2
plt.scatter(x, y, c='k')
plt.plot(x1, f, c='r')
plt.show()

fit5.png It feels nice and smooth. Create x1 as the drawing range and make the steps finer. Now that we know the basic fitting method and drawing method, we will finally read the experimental data of the txt file and dat file and perform fitting.

Fitting of experimental data

Finally, based on the above, read the data from the txt file and dat file, perform fitting, and determine the parameters. I'll put the whole code at the very end.

Recommended Posts

Experiment data fitting (Python) being added ...
Data analysis python
[python] Read data
Data analysis with python 2
Python Data Visualization Libraries
Data analysis using Python 0
Data analysis overview python
Data cleaning using Python
Not being aware of the contents of the data in python
Python data analysis template
[Python tutorial] Data structure
[Python] Sorting Numpy data
Data analysis with Python
Grammar features added from Python3.6
Tuning experiment of Tensorflow data
Display UTM-30LX data in Python
Data Science Cheat Sheet (Python)
[Python] Notes on data analysis
My python data analytics environment
Python application: data visualization # 2: matplotlib
Python data analysis learning notes
Python data type summary memo
[Python] Curve fitting with polynomial
[Python] Plot time series data
Python for Data Analysis Chapter 2
Image data type conversion [Python]
Data analysis using python pandas
python> tuple> data, address = s.recvfrom (10000)
Python for Data Analysis Chapter 3
Read json data with python