The SciPy library is a collection of mathematical algorithms and convenience functions built on top of the NumPy library. It is done by the same people but the aim is slightly different:
Because SciPy is a very large compilation of numerical routines, there are many sub-modules, which are all loaded separately (just avoid too long import time). To allow faster development, some developer made scikits which are focusing on a specific subject rather then staying generic.
The default behavour of SciPy is sometimes different from Numpy:
In [1]:
import numpy
numpy.sqrt(-1)
Out[1]:
In [2]:
import scipy
scipy.sqrt(-1)
Out[2]:
In [3]:
%pylab inline
In [5]:
#The exect function
f = lambda x:(x-1.)*(x-0.5)*(x+0.5)
#My sampling:
x_sparse = scipy.linspace(-1,1,num=5)
x_dense = scipy.linspace(-1 , 1 , num=40)
from scipy.interpolate import interp1d
f0 = interp1d(x_sparse, f(x_sparse), kind='zero')
f1 = interp1d(x_sparse, f(x_sparse), kind='linear')
f2 = interp1d(x_sparse, f(x_sparse), kind='quadratic')
f3 = interp1d(x_sparse, f(x_sparse), kind='cubic')
f4 = interp1d(x_sparse, f(x_sparse), kind='nearest')
plot(x_sparse, f(x_sparse) , 'D', label='data')
plot(x_dense, f0(x_dense), ':', label='zero')
plot(x_dense, f1(x_dense), '-.', label='linear')
plot(x_dense, f2(x_dense), '-.', label='quadratic')
plot(x_dense, f3(x_dense), 's--', label='cubic')
plot(x_dense, f4(x_dense), '-' , label='nearest')
plot(x_dense, f(x_dense), linewidth = 2, label='exact')
legend(loc='lower right')
axis([x_dense.min(), x_dense.max(), f(x_dense).min(), f(x_dense).max()+0.1])
Out[5]:
scipy.integrate provides integration and differential equation solvers. Those functions are wrappers from the QUADPACK ForTran library. It provides 3 kinds of functions:
In [6]:
from scipy import integrate
f = lambda x: 2 / (1 + x**2)
integrate.quad(f, 0, inf)
Out[6]:
In [7]:
f = lambda x: exp(-x**2)
x = linspace(-10, 10 , 1001)
integrate.quad(f,-10,10)[0] - integrate.trapz(f(x),x)
Out[7]:
The scipy.optimize package provides several commonly used optimization algorithms:
scipy.fftpack contains Python bindings to the FFTPACK ForTran library. Like NumPy 1d, 2d and nd transform at available in forward and reverse directions. In addition to NumPy, SciPy offers convolution in Fourier space, Discrete Sine and Cosine Transforms (used in JPEG encoding) and some caching of the prime factorization of length of the array.
In [8]:
from scipy.fftpack import fft
# Number of samplepoints
N = 600
# sample spacing
T = 1.0 / 800.0
x = linspace(0.0, N*T, N)
y = sin(50.0 * 2.0*pi*x) + 0.5*sin(80.0 * 2.0*pi*x)
yf = fft(y)
xf = linspace(0.0, 1.0/(2.0*T), N/2)
figure()
subplot(1,2,1)
plot(x,y,label="direct space")
subplot(1,2,2)
plot(xf, 2.0/N * abs(yf[0:N/2]),label="frequency space")
grid()
legend()
Out[8]:
The signal processing toolbox, scipy.signal, contains some filtering functions which can be classified in:
In [9]:
from scipy import misc, signal
image = misc.lena()
w = signal.gaussian(50, 5.0)
image_new = signal.sepfir2d(image, w, w)
figure()
subplot(1,2,1)
imshow(image, cmap="gray")
title("Original image")
subplot(1,2,2)
imshow(image_new, cmap="gray")
title("Blured image")
Out[9]:
Scipy.linalg contains the linear algebra routines from LAPACK wrapped in Python. Unlike Numpy, it need this library to be installed on your computer. One of the most common optimized version is ATLAS but OpenBLAS is also popular and the MKL (from Intel) is the fastest. So linalg from Scipy can be 20x faster than the default implementation provided by NumPy.
Sparse matrix representation and diagonalization are also handled in scipy using the ForTran ARPACK library.
Scipy is a huge compilation of numerical libraries. It is too large to fit into a single brain, this is why there is an excellent documentation.
More advanced toolkits are available, those are often research subjects of the respective authors.
In [ ]: