Fit curve to data_ scipy.optimize.curve_fit

EN240120201439

EXAMPLE

Generates fictitious data

In [1]:
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
from pylab import *

x = np.linspace(0, 10, num = 40) 
y = 3.45 * np.sin(1.2 * x) + np.random.normal(size = 40)   

plt.figure()
plt.plot(x, y, 'ko', color ='blue', label="Original Noised Data")
plt.legend()
plt.show()  
<Figure size 640x480 with 1 Axes>

You need to know in outline what equation a given graph can have. We define the equation of the curve.

In [2]:
def test(x, a, b): 
    return a * np.sin(b * x) 

The scipy function “scipy.optimize.curve_fit” adopts the type of curve to which you want to fit the data (linear),
– x axis data (x table),
– y axis data (y table),
– guessing parameters (p0).

The function then returns two information:
popt – Sine function coefficients:
pcov – estimated parameter covariance

In [3]:
import scipy
from scipy.optimize import curve_fit

parametr, parametr_cov = curve_fit(test, x, y)
In [4]:
parametr
Out[4]:
array([3.40022034, 1.18368923])
In [5]:
print("Sine funcion coefficients:",  parametr) 
print("Covariance of coefficients:", parametr_cov) 
Sine funcion coefficients: [3.40022034 1.18368923]
Covariance of coefficients: [[0.06204388 0.00011354]
 [0.00011354 0.00018356]]

We defined functions as:
a * np.sin (b * x)
Variables a and b are coefficients of a sinusoidal function:

  • parameter [0] = a
  • parameter [1] = b

which contain actual match parameters (popt_linear) and covariance of match parameters (pcov_linear).

In [6]:
print('a: ',parametr[0])
print('b: ',parametr[1])
a:  3.400220342120811
b:  1.1836892329574118

We substitute the formula for the sine function: a e.g. sin (b x)

In [7]:
kot = (parametr[0]*(np.sin(parametr[1]*x))) 
In [8]:
plt.plot(x, y, 'o', color ='blue', label ="data") 
plt.plot(x, kot, '--', color ='red', label ="Fitted Curve: 3.6*sin(1.3*x)") 
plt.legend() 
plt.show() 

EXAMPLE

Generates fictitious data

In [9]:
x = np.linspace(0, 1, num = 40)  
y = 3.45 * np.exp(2.334 * x) + 2*np.random.normal(size = 40) 

plt.figure()
plt.plot(x, y, 'ko', color ='blue', label="Original Noised Data")
plt.legend()
plt.show()  

Trzeba znać w zaryzie jakie równanie może mieć dany wykres. Definiujemy równanie krzywej.

In [10]:
def kot(x, a, b): 
    return a*np.exp(b*x) 
In [11]:
import scipy
from scipy.optimize import curve_fit

pa, pa_cov = curve_fit(kot, x, y) 
In [12]:
print("Sine funcion coefficients:",  pa) 
print("Covariance of coefficients:")
print(pa_cov)
Sine funcion coefficients: [3.6797288  2.25070778]
Covariance of coefficients:
[[ 0.08865591 -0.02837859]
 [-0.02837859  0.0096322 ]]

Podstawiamy do wzoru: a np.exp(b x)

In [13]:
print('a: print('b: 
a: 3.68
b: 2.25
In [14]:
 PZU = pa[0]*np.exp(pa[1]*x) 
#PZU = kot(x, pa[0], pa[1])
In [15]:
plt.plot(x, y, 'o', color ='blue', label ="data") 
plt.plot(x, PZU, '--', color ='red', label ="Fitted Curve") 
plt.legend() 
plt.show() 

EXAMPLE

Generates fictitious data

In [16]:
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(1,10,100)
y = np.linspace(5,200,100)
y_noise = 30*(np.random.ranf(100))
y += y_noise

plt.figure()
plt.plot(x, y, 'ko',color ='blue', label="Original Noised Data")
plt.legend()
plt.show()
In [17]:
def linear(x, a, b):
    return a*x + b
In [18]:
import scipy
from scipy.optimize import curve_fit

par, par_cov = scipy.optimize.curve_fit(linear, x, y)
In [19]:
print("Sine funcion coefficients:",  par) 
print("Covariance of coefficients:")
print(par_cov)
Sine funcion coefficients: [21.97503641 -3.98345159]
Covariance of coefficients:
[[ 0.09584559 -0.52715077]
 [-0.52715077  3.55935696]]

We substitute for the formula:

In [20]:
print('a: print('b: 
a: 21.98
b: -3.98
In [21]:
#PKP = par[0]*x + par[1]
PKP = linear(x, par[0], par[1])
In [22]:
plt.plot(x, y, 'o', color ='blue', label ="data") 
plt.plot(x, PKP, '--', color ='red', label ="Fitted Curve") 
plt.legend() 
plt.show()

EXAMPLE

Generates fictitious data

In [23]:
x = np.linspace(0, 10, num = 160) 
y = 1.45 * np.sin(1.2 * x) + np.random.normal(size = 160)   

plt.figure()
plt.plot(x, y, 'ko', color ='blue', label="Original Noised Data")
plt.legend()
plt.show()  

You need to know in outline what equation a given graph can have. We define the equation of the curve.

In [24]:
def foka(x, a, b): 
    return a * np.cos(b * x) 
In [25]:
import scipy
from scipy.optimize import curve_fit

pac, pac_cov = scipy.optimize.curve_fit(foka, x, y)
In [26]:
print("Sine funcion coefficients:",  pac) 
print("Covariance of coefficients:")
print(pac_cov)
Sine funcion coefficients: [1.07953123 0.96862312]
Covariance of coefficients:
[[ 0.01835107 -0.00023385]
 [-0.00023385  0.00053849]]
In [27]:
print('a: print('b: 
a: 1.08
b: 0.97
In [28]:
PKS = foka(x, pac[0], pac[1])
In [29]:
plt.plot(x, y, 'o', color ='blue', label ="data") 
plt.plot(x, PKS, '--', color ='red', label ="Fitted Curve") 
plt.legend() 
plt.show()

EXAMPLE

Generates fictitious data

In [30]:
x = np.linspace(-10, 10, num = 300) 
y = 1.75 * np.cosh(0.168 * x) + np.random.normal(size = 300)   

plt.figure()
plt.plot(x, y, 'ko', color ='blue', label="Original Noised Data")
plt.legend()
plt.axhline(y=0, color='black', linestyle='--', lw=0.5)
plt.axvline(x= 0, color = 'black', linestyle='--', lw=0.5)

plt.show() 

Defines the matching function.

In [31]:
def vok(x, a, b):
    return a * np.cosh(x * b)
In [32]:
import scipy
from scipy.optimize import curve_fit

pan, pan_cov = scipy.optimize.curve_fit(vok, x, y)
In [33]:
print("Sine funcion coefficients:",  pan) 
print("Covariance of coefficients:")
print(pan_cov)
Sine funcion coefficients: [1.67013896 0.1780296 ]
Covariance of coefficients:
[[ 7.28891469e-03 -5.93352295e-04]
 [-5.93352295e-04  5.89205781e-05]]
In [34]:
print('a: print('b: 
a: 1.67
b: 0.18
In [35]:
POS = vok(x, pan[0], pan[1])
In [36]:
plt.plot(x, y, 'o', color ='blue', label ="data") 
plt.plot(x, POS, '--', color ='orange', linewidth=5.5, label ="Fitted Curve") 
plt.legend() 
plt.axhline(y=0, color='black', linestyle='--', lw=0.5)
plt.axvline(x= 0, color = 'black', linestyle='--', lw=0.5)
plt.show()

Compute tangent

In [37]:
x = np.linspace(-10, 10, num = 60) 
y = 10.45 * np.tan(10.2 * x) + np.random.normal(size = 60)   

plt.figure()
plt.plot(x, y, 'ko', color ='blue', label="Original Noised Data")
plt.legend()
plt.axhline(y=0, color='black', linestyle='--', lw=0.5)
plt.axvline(x= 0, color = 'black', linestyle='--', lw=0.5)
plt.show()  

Compute hyperbolic tangent

In [38]:
x = np.linspace(-10, 10, num = 60) 
y = 15.45 * np.tanh(1.2 * x) + np.random.normal(size = 60)   

plt.figure()
plt.plot(x, y, 'ko', color ='blue', label="Original Noised Data")
plt.legend()
plt.axhline(y=0, color='black', linestyle='--', lw=0.5)
plt.axvline(x= 0, color = 'black', linestyle='--', lw=0.5)

plt.show()  

Inverse sine

In [39]:
x = np.linspace(-10, 10, num = 260) 
y = 9.45 * np.arcsin(0.7 * x) + np.random.normal(size = 260)   

plt.figure()
plt.plot(x, y, 'ko', color ='blue', label="Original Noised Data")
plt.legend()
plt.axhline(y=0, color='black', linestyle='--', lw=0.5)
plt.axvline(x= 0, color = 'black', linestyle='--', lw=0.5)

plt.show() 
C:ProgramDataAnaconda3libsite-packagesipykernel_launcher.py:2: RuntimeWarning: invalid value encountered in arcsin
  

Inverse hyperbolic cosine

In [40]:
x = np.linspace(-10, 10, num = 260) 
y = 9.45 * np.arccosh(3.7 * x) + np.random.normal(size = 260)   

plt.figure()
plt.plot(x, y, 'ko', color ='blue', label="Original Noised Data")
plt.legend()
plt.axhline(y=0, color='black', linestyle='--', lw=0.5)
plt.axvline(x= 0, color = 'black', linestyle='--', lw=0.5)
plt.show() 
C:ProgramDataAnaconda3libsite-packagesipykernel_launcher.py:2: RuntimeWarning: invalid value encountered in arccosh
  

np.cosh

In [41]:
x = np.linspace(-10, 10, num = 260) 
y = 1.45 * np.cosh(0.3 * x) + np.random.normal(size = 260)   

plt.figure()
plt.plot(x, y, 'ko', color ='blue', label="Original Noised Data")
plt.legend()
plt.axhline(y=0, color='black', linestyle='--', lw=0.5)
plt.axvline(x= 0, color = 'black', linestyle='--', lw=0.5)
plt.show() 

np.sinh

In [42]:
x = np.linspace(-10, 10, num = 260) 
y = 1.45 * np.sinh(0.3 * x) + np.random.normal(size = 260)   

plt.figure()
plt.plot(x, y, 'ko', color ='blue', label="Original Noised Data")
plt.legend()
plt.axhline(y=0, color='black', linestyle='--', lw=0.5)
plt.axvline(x= 0, color = 'black', linestyle='--', lw=0.5)
plt.show() 

np.tanh

In [43]:
x = np.linspace(-10, 10, num = 260) 
y = 7.45 * np.tanh(0.3 * x) + np.random.normal(size = 260)   

plt.figure()
plt.plot(x, y, 'ko', color ='blue', label="Original Noised Data")
plt.legend()
plt.axhline(y=0, color='black', linestyle='--', lw=0.5)
plt.axvline(x= 0, color = 'black', linestyle='--', lw=0.5)
plt.show() 

np.arcsinh

In [44]:
x = np.linspace(-10, 10, num = 260) 
y = 7.45 * np.arcsinh(0.3 * x) + np.random.normal(size = 260)   

plt.figure()
plt.plot(x, y, 'ko', color ='blue', label="Original Noised Data")
plt.legend()
plt.axhline(y=0, color='black', linestyle='--', lw=0.5)
plt.axvline(x= 0, color = 'black', linestyle='--', lw=0.5)
plt.show() 

np.arccosh

In [45]:
x = np.linspace(-10, 10, num = 260) 
y = 17.45 * np.arccosh(1.3 * x) + np.random.normal(size = 260)   

plt.figure()
plt.plot(x, y, 'ko', color ='blue', label="Original Noised Data")
plt.legend()
plt.axhline(y=0, color='black', linestyle='--', lw=0.5)
plt.axvline(x= 0, color = 'black', linestyle='--', lw=0.5)
plt.show()
C:ProgramDataAnaconda3libsite-packagesipykernel_launcher.py:2: RuntimeWarning: invalid value encountered in arccosh
  

np.arcsinh

In [46]:
x = np.linspace(-10, 10, num = 260) 
y = 9.45 * np.arcsinh(3.7 * x) + np.random.normal(size = 260)   

plt.figure()
plt.plot(x, y, 'ko', color ='blue', label="Original Noised Data")
plt.legend()
plt.axhline(y=0, color='black', linestyle='--', lw=0.5)
plt.axvline(x= 0, color = 'black', linestyle='--', lw=0.5)
plt.show() 

np.arctan

In [47]:
x = np.linspace(-10, 10, num = 260) 
y = 9.45 * np.arctan(3.7 * x) + np.random.normal(size = 260)   

plt.figure()
plt.plot(x, y, 'ko', color ='blue', label="Original Noised Data")
plt.legend()
plt.axhline(y=0, color='black', linestyle='--', lw=0.5)
plt.axvline(x= 0, color = 'black', linestyle='--', lw=0.5)
plt.show() 

Inverse hyperbolic tangent

In [48]:
x = np.linspace(-10, 10, num = 260) 
y = 59.45 * np.arctanh(0.7 * x) + np.random.normal(size = 260)   

plt.figure()
plt.plot(x, y, 'ko', color ='blue', label="Original Noised Data")
plt.legend()
plt.axhline(y=0, color='black', linestyle='--', lw=0.5)
plt.axvline(x= 0, color = 'black', linestyle='--', lw=0.5)
plt.show() 
C:ProgramDataAnaconda3libsite-packagesipykernel_launcher.py:2: RuntimeWarning: invalid value encountered in arctanh
  

rad2deg

In [49]:
x = np.linspace(-10, 10, num = 260) 
y = 100.45 * np.rad2deg(2.7 * x) + np.random.normal(size = 260)   

plt.figure()
plt.plot(x, y, 'ko', color ='blue', label="Original Noised Data")
plt.legend()
plt.axhline(y=0, color='black', linestyle='--', lw=0.5)
plt.axvline(x= 0, color = 'black', linestyle='--', lw=0.5)
plt.show() 

Unwrap

In [50]:
x = np.linspace(-10, 10, num = 260) 
y = 0.15 * np.unwrap(2.7 * x) + np.random.normal(size = 260)   

plt.figure()
plt.plot(x, y, 'ko', color ='blue', label="Original Noised Data")
plt.legend()
plt.axhline(y=0, color='black', linestyle='--', lw=0.5)
plt.axvline(x= 0, color = 'black', linestyle='--', lw=0.5)
plt.show() 

np.exp

In [51]:
x = np.linspace(-10, 10, num = 260) 
y = 7.45 * np.exp(0.17 * x) + np.random.normal(size = 260)   

plt.figure()
plt.plot(x, y, 'ko', color ='blue', label="Original Noised Data")
plt.legend()
plt.axhline(y=0, color='black', linestyle='--', lw=0.5)
plt.axvline(x= 0, color = 'black', linestyle='--', lw=0.5)
plt.show() 

np.log

In [52]:
x = np.linspace(-10, 20, num = 260) 
y = 7.45 * np.log(0.17 * x) + np.random.normal(size = 260)   

plt.figure()
plt.plot(x, y, 'ko', color ='blue', label="Original Noised Data")
plt.legend()
plt.axhline(y=0, color='black', linestyle='--', lw=0.5)
plt.axvline(x= 0, color = 'black', linestyle='--', lw=0.5)
plt.show() 
C:ProgramDataAnaconda3libsite-packagesipykernel_launcher.py:2: RuntimeWarning: invalid value encountered in log
  

np.log10

In [53]:
x = np.linspace(-10, 30, num = 260) 
y = 7.45 * np.log10(0.17 * x) + np.random.normal(size = 260)   

plt.figure()
plt.plot(x, y, 'ko', color ='blue', label="Original Noised Data")
plt.legend()
plt.axhline(y=0, color='black', linestyle='--', lw=0.5)
plt.axvline(x= 0, color = 'black', linestyle='--', lw=0.5)
plt.show() 
C:ProgramDataAnaconda3libsite-packagesipykernel_launcher.py:2: RuntimeWarning: invalid value encountered in log10
  
In [54]:
x = np.linspace(-10,30, num = 260) 
y = 7.45 * np.log10(0.17 * x) + np.random.normal(size = 260)   

plt.figure()
plt.plot(x, y, 'ko', color ='blue', label="Original Noised Data")
plt.legend()
plt.axhline(y=0, color='black', linestyle='--', lw=0.5)
plt.axvline(x= 0, color = 'black', linestyle='--', lw=0.5)
plt.show() 
C:ProgramDataAnaconda3libsite-packagesipykernel_launcher.py:2: RuntimeWarning: invalid value encountered in log10
  

PIERWIASTEK KWADRATOWY

In [55]:
x = np.linspace(-10, 10, num = 260) 
y = 3.45 * np.sqrt(3.17 * x) + np.random.normal(size = 260)   

plt.figure()
plt.plot(x, y, 'ko', color ='blue', label="Original Noised Data")
plt.legend()
plt.axhline(y=0, color='black', linestyle='--', lw=0.5)
plt.axvline(x= 0, color = 'black', linestyle='--', lw=0.5)
plt.show() 
C:ProgramDataAnaconda3libsite-packagesipykernel_launcher.py:2: RuntimeWarning: invalid value encountered in sqrt
  

PIERWIASTEK PRZESTRZENNY

In [56]:
x = np.linspace(-10, 10, num = 260) 
y = 3.45 * np.cbrt(7.17 * x) + np.random.normal(size = 260)   

plt.figure()
plt.plot(x, y, 'ko', color ='blue', label="Original Noised Data")
plt.legend()
plt.axhline(y=0, color='black', linestyle='--', lw=0.5)
plt.axvline(x= 0, color = 'black', linestyle='--', lw=0.5)
plt.show() 

Return the element-wise square of the input.

In [57]:
x = np.linspace(-10, 10, num = 260) 
y = 5.45 * np.square(0.17 * x) + np.random.normal(size = 260)   

plt.figure()
plt.plot(x, y, 'ko', color ='blue', label="Original Noised Data")
plt.legend()
plt.axhline(y=0, color='black', linestyle='--', lw=0.5)
plt.axvline(x= 0, color = 'black', linestyle='--', lw=0.5)
plt.show() 

Returns an element-wise indication of the sign of a number

In [58]:
x = np.linspace(-10, 10, num = 260) 
y = 2.30 * np.sign(44.17 * x) + np.random.normal(size = 260)   

plt.figure()
plt.plot(x, y, 'ko', color ='blue', label="Original Noised Data")
plt.legend()
plt.axhline(y=0, color='black', linestyle='--', lw=0.5)
plt.axvline(x= 0, color = 'black', linestyle='--', lw=0.5)
plt.show() 
In [78]:
# Generate sample data
x = np.sort(5 * np.random.rand(30, 1), axis=0)
y = np.sin(x).ravel()
In [79]:
plt.figure()
plt.plot(x, y, 'ko', color ='blue', label="Original Noised Data")
plt.legend()
plt.axhline(y=0, color='black', linestyle='--', lw=0.5)
plt.axvline(x= 0, color = 'black', linestyle='--', lw=0.5)
plt.show() 
In [76]:
# Generate sample data
x = np.sort(10 * np.random.rand(70, 1), axis=0)
y = np.sin(x).ravel()
In [77]:
plt.figure()
plt.plot(x, y, 'ko', color ='blue', label="Original Noised Data")
plt.legend()
plt.axhline(y=0, color='black', linestyle='--', lw=0.5)
plt.axvline(x= 0, color = 'black', linestyle='--', lw=0.5)
plt.show() 

In the next part we will use the menus Support Vector Regression (SVR) using linear and non-linear kernels.