csomag | |
---|---|
NumPy | Hatékony N-dimenziós tömb |
SciPy | Numerikus számÃtások |
Matplotlib | Grafikonok és rajzok |
IPython (Jupyter) | InteraktÃv notebook |
SymPy | Szimbolikus számÃtások |
Pandas | Adatbányászat |
import sympy as sym
import math
import numpy as np
import matplotlib.pyplot as plt
import scipy
A numerikus számÃtások célja, hogy a matematikai számÃtásokat végezzünk hatékonyan.
Előnyök:
Hátrányok:
Részletes tutorial: https://docs.scipy.org/doc/scipy/reference/tutorial/
A háttérben sokszor Fortran fut.
Alcsomagok:
scipy.cluster | Vector quantization / Kmeans |
scipy.constants | Physical and mathematical constants |
scipy.fftpack | Fourier transform |
scipy.integrate | Integration routines |
scipy.interpolate | Interpolation |
scipy.io | Data input and output |
scipy.linalg | Linear algebra routines |
scipy.ndimage | n-dimensional image package |
scipy.odr | Orthogonal distance regression |
scipy.optimize | Optimization |
scipy.signal | Signal processing |
scipy.sparse | Sparse matrices |
scipy.spatial | Spatial data structures and algorithms |
scipy.special | Any special mathematical functions |
scipy.stats | Statistics |
from scipy import linalg, optimize
LU felbontás
from scipy.linalg import lu
A = np.array([[2, 5, 8, 7], [5, 2, 2, 8], [7, 5, 6, 6], [5, 4, 4, 8]])
p, l, u = lu(A)
print(A,p,l,u, sep="\n")
np.allclose(A - p @ l @ u, np.zeros((4, 4)))
QR felbontás Q ortogonális, R felső háromszög
A = np.random.randn(4, 4)
print(A)
q, r = linalg.qr(A)
print(q,r,sep='\n')
q @ q.T
A scipy.integrate.quad(f, a, b)
integrálja a-tól b-ig az f függvényt. Két értékkel tér vissza, az inregrálás eredményével és egy hibahatárral.
import scipy.integrate
f= lambda x:2*x
i = scipy.integrate.quad(f, 0, 1)
print(i)
f= lambda x:np.exp(-x**2)
i = scipy.integrate.quad(f, 0, 1)
print(i)
## szimbolikus számÃtással
xval = sym.Symbol('x')
sym.integrate(sym.exp(-xval ** 2), xval)
((sym.erf(1)-sym.erf(0))*np.pi**(1/2)/2).evalf()
#sym.integrate(sym.exp(sym.sin(-sym.log(xval ** 2+xval)-xval)), xval)
f= lambda x:np.exp(np.sin(-np.log(x ** 2+x)-x))
i = scipy.integrate.quad(f, 0, 1)
print(i)
Több változóban is tudunk integrálni a scipy.integrate.dblquad(func, a, b, gfun, hfun)
paranccsal. A gfun és hfun parancsok adják meg a belső integrál határait.
f = lambda x, y : 16*x*y
g = lambda x : 0
h = lambda y : math.sqrt(1-4*y**2)
i = scipy.integrate.dblquad(f, 0, 0.5, g, h)
print(i)
from scipy import interpolate
x = np.linspace(0, 4, 12)
y = np.cos(x**2/3+4)
print (x,y)
figure, axis = plt.subplots(1, 1,figsize=(5,5))
axis.plot(x, y,'o')
plt.show()
f1 = interpolate.interp1d(x, y,kind = 'linear')
f2 = interpolate.interp1d(x, y, kind = 'cubic')
xnew = np.linspace(0, 4,30)
#plt.plot(x, y, 'o', xnew, f(xnew), '-', xnew, f2(xnew), '--')
figure, axis = plt.subplots(1, 1,figsize=(5,5))
axis.plot(x, y, 'o')
axis.plot( xnew, f1(xnew), '-')
axis.plot( xnew, f2(xnew), '--')
axis.legend(['data', 'linear', 'cubic'], loc = 'best')
plt.show()
from scipy.interpolate import UnivariateSpline
x = np.linspace(-3, 3, 50)
y = np.exp(-x**2) + 0.1 * np.random.randn(50)
figure, axis = plt.subplots(1, 1,figsize=(5,5))
axis.plot(x, y, 'ro', ms = 5)
plt.show()
f2 = interpolate.interp1d(x, y, kind = 'cubic')
xnew = np.linspace(-3, 3,30)
#plt.plot(x, y, 'o', xnew, f(xnew), '-', xnew, f2(xnew), '--')
figure, axis = plt.subplots(1, 1,figsize=(5,5))
axis.plot(x, y, 'o')
axis.plot( xnew, f2(xnew), '--')
axis.legend(['data', 'cubic' ], loc = 'best')
plt.show()
spl = UnivariateSpline(x, y)
xs = np.linspace(-3, 3, 1000)
figure, axis = plt.subplots(1, 1,figsize=(5,5))
axis.plot(xs, spl(xs), 'g', lw = 3)
axis.plot(x, y, 'ro', ms = 5)
plt.show()
spl.set_smoothing_factor(0.5)
figure, axis = plt.subplots(1, 1,figsize=(5,5))
axis.plot(x, y, 'ro')
axis.plot(xs, spl(xs))
axis.plot(x,np.exp(-x**2), ms = 5)
axis.legend(['data', 'spline', 'original'], loc = 'best')
plt.show()
B-spline reprezentáció
t=np.linspace(0,1.75*2*np.pi,100)
x = np.sin(t)
y = np.cos(t)
z = t
# add noise
x+= np.random.normal(scale=0.1, size=x.shape)
y+= np.random.normal(scale=0.1, size=y.shape)
z+= np.random.normal(scale=0.1, size=z.shape)
# spline parameters
s=3.0 # smoothness parameter
k=2 # spline order
nest=-1 # estimate of number of knots needed (-1 = maximal)
# find the knot points
tckp,u = scipy.interpolate.splprep([x,y,z],s=s,k=k,nest=-1)
# evaluate spline, including interpolated points
xnew,ynew,znew = scipy.interpolate.splev(np.linspace(0,1,400),tckp)
figure, axis = plt.subplots(2, 2,figsize=(6,6))
axis[0,0].plot(x,y,'bo-',label='data')
axis[0,0].plot(xnew,ynew,'r-',label='fit')
axis[0,1].plot(x,z,'bo-',label='data')
axis[0,1].plot(xnew,znew,'r-',label='fit')
axis[1,0].plot(y,z,'bo-',label='data')
axis[1,0].plot(ynew,znew,'r-',label='fit')
plt.show()
f = np.poly1d([5, 1])
x = np.linspace(0, 10, 30)
y = f(x) + 6*np.random.normal(size=len(x))
xn = np.linspace(0, 10, 200)
figure, axis = plt.subplots(1, 1,figsize=(6,6))
axis.plot(x, y, 'or')
plt.show()
a = np.vstack([x, np.ones(len(x))]).T
np.dot(np.linalg.inv(np.dot(a.T, a)), np.dot(a.T, y))
np.linalg.lstsq(a, y,rcond=None)[0]
np.polyfit(x, y, 1)
m, c = np.polyfit(x, y, 1)
yn = np.polyval([m, c], xn)
figure, axis = plt.subplots(1, 1,figsize=(6,6))
axis.plot(x, y, 'or')
axis.plot(xn, yn)
plt.show()
A scipy.optimize.root(fn,x0)
függvényt használhatjuk. A hátterben különbözÅ‘ iteratÃv módszerek futnak, amik folyamatosan javÃtanak a megoldáson. A második paraméter a kezdeti tipp a gyökre.
from scipy.optimize import root
def func(x):
return x*2 + 2 * np.cos(x)
sol = root(func,0)
print(sol)
func(-0.73908513)
sym.solveset( xval*2 + 2 * sym.cos(xval), xval) # a szimolikus megoldó csak széttárja a kezét.
from scipy.spatial import Delaunay
points = np.random.rand(30, 2) # 30 random points in 2-D
tri = Delaunay(points)
import matplotlib.pyplot as plt
plt.triplot(points[:,0], points[:,1], tri.simplices.copy())
plt.plot(points[:,0], points[:,1], 'o')
plt.show()
from scipy.spatial import ConvexHull
points = np.random.rand(30, 2) # 30 random points in 2-D
hull = ConvexHull(points)
plt.plot(points[:,0], points[:,1], 'o')
for simplex in hull.simplices:
plt.plot(points[simplex,0], points[simplex,1], 'k-')
plt.show()
# How many time points are needed i,e., Sampling Frequency
samplingFrequency = 100;
# At what intervals time points are sampled
samplingInterval = 1 / samplingFrequency;
# Begin time period of the signals
beginTime = 0;
# End time period of the signals
endTime = 10;
# Frequency of the signals
signal1Frequency = 11;
signal2Frequency = 7;
# Time points
time = np.arange(beginTime, endTime, samplingInterval);
# Create two sine waves
amplitude1 = np.sin(2*np.pi*signal1Frequency*time)
amplitude2 = np.sin(2*np.pi*signal2Frequency*time)
# Create subplot
figure, axis = plt.subplots(4, 1,figsize=(10,10))
plt.subplots_adjust(hspace=1)
# Time domain representation for sine wave 1
axis[0].set_title('Sine wave with a frequency of 4 Hz')
axis[0].plot(time, amplitude1)
axis[0].set_xlabel('Time')
axis[0].set_ylabel('Amplitude')
# Time domain representation for sine wave 2
axis[1].set_title('Sine wave with a frequency of 7 Hz')
axis[1].plot(time, amplitude2)
axis[1].set_xlabel('Time')
axis[1].set_ylabel('Amplitude')
# Add the sine waves
amplitude = amplitude1 + amplitude2
# Time domain representation of the resultant sine wave
axis[2].set_title('Sine wave with multiple frequencies')
axis[2].plot(time, amplitude)
axis[2].set_xlabel('Time')
axis[2].set_ylabel('Amplitude')
# Frequency domain representation
fourierTransform = np.fft.fft(amplitude)/len(amplitude) # Normalize amplitude
fourierTransform = fourierTransform[range(int(len(amplitude)/2))] # Exclude sampling frequency
tpCount = len(amplitude)
values = np.arange(int(tpCount/2))
timePeriod = tpCount/samplingFrequency
frequencies = values/timePeriod
# Frequency domain representation
axis[3].set_title('Fourier transform depicting the frequency components')
axis[3].plot(frequencies, abs(fourierTransform))
axis[3].set_xlabel('Frequency')
axis[3].set_ylabel('Amplitude')
plt.show()