EN240120201439
Generates fictitious data
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()
You need to know in outline what equation a given graph can have. We define the equation of the curve.
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
import scipy
from scipy.optimize import curve_fit
parametr, parametr_cov = curve_fit(test, x, y)
parametr
print("Sine funcion coefficients:", parametr)
print("Covariance of coefficients:", parametr_cov)
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).
print('a: ',parametr[0])
print('b: ',parametr[1])
We substitute the formula for the sine function: a e.g. sin (b x)
kot = (parametr[0]*(np.sin(parametr[1]*x)))
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
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.
def kot(x, a, b):
return a*np.exp(b*x)
import scipy
from scipy.optimize import curve_fit
pa, pa_cov = curve_fit(kot, x, y)
print("Sine funcion coefficients:", pa)
print("Covariance of coefficients:")
print(pa_cov)
Podstawiamy do wzoru: a np.exp(b x)
print('a: print('b:
PZU = pa[0]*np.exp(pa[1]*x)
#PZU = kot(x, pa[0], pa[1])
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
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()
def linear(x, a, b):
return a*x + b
import scipy
from scipy.optimize import curve_fit
par, par_cov = scipy.optimize.curve_fit(linear, x, y)
print("Sine funcion coefficients:", par)
print("Covariance of coefficients:")
print(par_cov)
We substitute for the formula:
print('a: print('b:
#PKP = par[0]*x + par[1]
PKP = linear(x, par[0], par[1])
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
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.
def foka(x, a, b):
return a * np.cos(b * x)
import scipy
from scipy.optimize import curve_fit
pac, pac_cov = scipy.optimize.curve_fit(foka, x, y)
print("Sine funcion coefficients:", pac)
print("Covariance of coefficients:")
print(pac_cov)
print('a: print('b:
PKS = foka(x, pac[0], pac[1])
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
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.
def vok(x, a, b):
return a * np.cosh(x * b)
import scipy
from scipy.optimize import curve_fit
pan, pan_cov = scipy.optimize.curve_fit(vok, x, y)
print("Sine funcion coefficients:", pan)
print("Covariance of coefficients:")
print(pan_cov)
print('a: print('b:
POS = vok(x, pan[0], pan[1])
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
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
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¶
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()
Inverse hyperbolic cosine¶
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()
np.cosh
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¶
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¶
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¶
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¶
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()
np.arcsinh¶
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¶
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¶
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()
rad2deg¶
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¶
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¶
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¶
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()
np.log10¶
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()
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()
PIERWIASTEK KWADRATOWY¶
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()
PIERWIASTEK PRZESTRZENNY¶
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.¶
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¶
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()
# Generate sample data
x = np.sort(5 * np.random.rand(30, 1), axis=0)
y = np.sin(x).ravel()
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()
# Generate sample data
x = np.sort(10 * np.random.rand(70, 1), axis=0)
y = np.sin(x).ravel()
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.