scipy: High-end scientific computing
Author: Adrien Chauve, Andre Espaze, Emmanuelle Gouillart, Gaël Varoquaux, Ralf gommers
Translated from: scipy Lecture Notes
Turn from: http://reverland.org/python/2012/10/22/scipy/#fnref: 4
SciPy
The SCIPY package contains the various toolkits that are devoted to common problems in scientific computing. Its different sub modules are corresponding to different applications. Like interpolation, integration, optimization, image processing, special functions, and so on.
SciPy can be compared to other standard scientific computing libraries, such as the GSL (GNU C or C + + Scientific Computing Library), or the MATLAB toolbox. SCIPY is the core package of the scientific computing program in Python, which is used to efficiently compute the numpy matrix to allow NumPy and scipy to work together.
Before implementing a program, it is worth checking if the required data processing method is already in scipy. As a professional programmer, scientists always like to reinvent the wheel _, resulting in a loophole, not optimized, difficult to share and maintain the code. Instead, the SCIPY program is optimized and tested, so it should be used as much as possible.
Catalog file input/output: Scipy.io Special function: scipy.special linear algebra operation: Scipy.linalg fast Fourier transform: Scipy.fftpack Work Example: Find the original cycle work example: Gaussian image Blur Optimization and fitting: scipy.optimize statistics and random numbers: scipy.stats histogram and probability density function percentile statistical detection interpolation: scipy.interpolate Numerical integration: Scipy.integratefusy, Signal Processing: scipy.signal image processing: scipy.ndimage image Geometric transform image filter Mathematical morphology image measurement summary exercise footnotes
Warning: This tutorial is far from true numerical calculation. Because the different modules and functions in the enumeration scipy are very boring, we focus on using a few examples to give a general idea of how to use ' scipy ' for computing.
SCIPY consists of a number of sub modules of a specific function:
Module |
function |
Scipy.cluster |
Vector quantization/K-mean value |
Scipy.constants |
Physical and mathematical constants |
Scipy.fftpack |
Fourier transform |
Scipy.integrate |
Integration Program |
Scipy.interpolate |
Insert Value |
Scipy.io |
Data input and output |
Scipy.linalg |
Linear algebra Program |
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 structure and algorithms |
Scipy.special |
Any particular mathematical function |
Scipy.stats |
Statistics |
They are all dependent on numpy, but each is basically independent. The standard way to import NumPy and these scipy modules is:
Import NumPy as NP from
scipy import Stats # Other sub modules are the same
Most of the main scipy namespaces contain real numpy functions (try Scipy.cos is Np.cos). These are just historical reasons, and there is usually no reason to use import scipy file input/output in your code: Scipy.io
Import and save Matlab files:
In [1]: from scipy import io as Spio in
[3]: Import NumPy as NP into
[4]: A = Np.ones ((3, 3)) in
[5]: Spio.savem At (' File.mat ', {' A ': A}) # Savemat expects a dictionary
/usr/lib/python2.7/site-packages/scipy/io/matlab/mio.py : 266:futurewarning:using oned_as Default value (' column ') This'll change to ' row ' in future versions
Oned_as=oned_ AS) in
[6]: data = Spio.loadmat (' File.mat ', struct_as_record=true) in
[7]: data[' a ']
out[7]:
Array ([[ 1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]]
Read pictures:
in [[]: From scipy import misc in []: Misc.imread (' scikit.png ') out[17]: Array ([[[255, 255, 255, 255], [255, 255, 255, 255], [255, 255, 255, 255], ..., [255, 255, 255, 255], [2
55, 255, 255, 255], [255, 255, 255, 255]], [[255, 255, 255, 255], [255, 255, 255, 255],
[255, 255, 255, 255], ..., [255, 255, 255, 255], [255, 255, 255, 255], [255, 255, 255, 255]], [[255, 255, 255, 255], [255, 255, 255, 255], [255, 255, 255, 255]
,..., [255, 255, 255, 255], [255, 255, 255, 255], [255, 255, 255, 255]], ..., [[255, 255, 255, 255], [255, 255, 255, 255], [255, 255, 255, 255],. ., [255, 255, 255, 255], [255, 255, 255, 255], [255, 255, 255, 255]], [[255,
255, 255, 255], [255, 255, 255, 255], [255, 255, 255, 255], ..., [255, 255, 255, 255], [255, 255
, 255, 255], [255, 255, 255, 255]], [[255, 255, 255, 255], [255, 255, 255, 255], [255, 255, 255, 255], ..., [255, 255, 255, 255], [255, 255, 255, 255], [255, 2 255, 255]], dtype=uint8) in [[]: Import Matplotlib.pyplot as PLT in [P]: Plt.imread (' scikit.png ') O
UT[19]: Array ([[[1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.], ..., [1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.]], [[1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.],..., [1., 1., 1, 1.] , [1., 1., 1., 1.], [1., 1., 1., 1.]], [[1., 1., 1., 1.], [1.;, 1 ., 1., 1.], [ 1., 1., 1., 1.],..., [1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.]],..., [[1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1.,
1.],..., [1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.]],
[[1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.]], [[1., 1., 1., 1
.], [1., 1., 1., 1.], [1., 1., 1., 1.],..., [1., 1., 1., 1.],
[1., 1., 1., 1.], [1., 1., 1., 1.]]], dtype=float32
See also: Load TXT file: Numpy.loadtxt ()/numpy.savetxt () Intelligent Import Text/csv file: Numpy.genfromtxt ()/numpy.recfromcsv () high speed, Efficient but numpy-specific binary format: Numpy.save ()/numpy.load () special function: Scipy.special
A special function is a priori function. Scipy.special's document string is very well written, so we don't list all the functions here. Commonly used are: Bessel functions, such as SCIPY.SPECIAL.JN () (integer n-order Bessel function) elliptic functions (SCIPY.SPECIAL.ELLIPJ () Jacobian elliptic function, ...) gamma function: Scipy.special.gamma (), Also note that Scipy.special.gammaln, which gives the gamma function of logarithmic coordinates, has a higher numerical precision. linear algebra operations: Scipy.linalg
The SCIPY.LINALG module provides standard linear algebra operations and relies on the underlying efficient implementation (BLAS,LAPACK).
Scipy.linalg.det () function computes the determinant of a square matrix:
In [_]: from scipy import Linalg [[]: arr = Np.array ([[1, 2], ...: [3, 4]]) in [24 ]: Linalg.det (arr) out[24]: -2.0 in [[]: Linalg.det (Np.ones ((3,4)))------------------------------------------ ---------------------------------ValueError Traceback (most recent call last) <ipyt Hon-input-25-375ad1d49940> in <module> ()----> 1 Linalg.det (Np.ones (3,4))/usr/lib/python2.7/site-p Ackages/scipy/linalg/basic.pyc in Det (A, overwrite_a) 398 a1 = Np.asarray_chkfinite (a) 399 If Len (a1.s Hape)!= 2 or a1.shape[0]!= a1.shape[1]:--> raise ValueError (' expected square matrix ') 401 ov Erwrite_a = Overwrite_a or _datacopied (A1, a) 402 fdet, = Get_flinalg_funcs ((' Det ',), (A1,)) ValueError: Expected Square matrix
PY.LINALG.INV () ' function computes the inverse of a square matrix:
in [num]: arr = Np.array ([[1, 2],
[3, 4]]) in
[[]: Iarr = LINALG.INV (arr) in
[]: Iarr
out[28]:
array ([[ -2., 1.],
[1.5, -0.5]] in
[[]: Np.allclose (Np.dot (arr, Iarr), Np.eye (2))
out[29]: True
The final calculation of the inverse of the singular array (its determinant is 0) will trigger the (raise) Linalgerror:
in [+]: arr = Np.array ([[3, 2], [6, 4]]" in [): Linalg . INV (arr)---------------------------------------------------------------------------Linalgerror Traceback (most recent) <ipython-input-33-52c04c854a80> in <module> ()----> 1 Linalg.inv (arr)/usr/lib/python2.7/site-packages/scipy/linalg/basic.pyc in Inv (A, overwrite_a) 346 Inv_a, info = Getri (Lu, PIV, overwrite_lu=1) 347 if info > 0:--> 348 raise Linalge Rror ("singular matrix") 349 if info < 0:350 raise ValueError (' illegal value in%d-th Argum ent of internal ' linalgerror:singular matrix
There are more advanced operations, such as singular value decomposition (SVD):
In [%]: arr = np.arange (9). Reshape ((3, 3)) + Np.diag ([1, 0, 1])
in []: Uarr, spec, Vharr = LINALG.SVD (arr)
Its result array spectrum is:
in [+]: Spec
out[36]: Array ([14.88982544, 0.45294236, 0.29654967])
The original matrix can be np.dot by the output of SVD by a combination of points:
In [Panax]: Sarr = np.diag (spec) in
[[]: Svd_mat = Uarr.dot (Sarr). dot (Vharr) in
[i]: Np.allclose (Svd_mat, arr)
OUT[39]: True
SVD is widely used in signal processing and statistics. Many other standard decomposition (Qr,lu,cholesky,schur) and solutions of linear systems can also be obtained from SCIPY.LINALG. Fast Fourier transform: Scipy.fftpack