Last night, I summarized [Introduction to Data Scientists] Basics of Numpy as the basis of how to use scientific calculation, data processing, and graph drawing library, but tonight I will summarize the basics of Scipy. However, since Scipy is enormous because it is difficult to understand even if it is copied without darkness, I will supplement the explanation in this book. 【Caution】 ["Data Scientist Training Course at the University of Tokyo"](https://www.amazon.co.jp/%E6%9D%B1%E4%BA%AC%E5%A4%A7%E5%AD%A6%E3 % 81% AE% E3% 83% 87% E3% 83% BC% E3% 82% BF% E3% 82% B5% E3% 82% A4% E3% 82% A8% E3% 83% B3% E3% 83 % 86% E3% 82% A3% E3% 82% B9% E3% 83% 88% E8% 82% B2% E6% 88% 90% E8% AC% 9B% E5% BA% A7-Python% E3% 81 % A7% E6% 89% 8B% E3% 82% 92% E5% 8B% 95% E3% 81% 8B% E3% 81% 97% E3% 81% A6% E5% AD% A6% E3% 81% B6 % E3% 83% 87% E2% 80% 95% E3% 82% BF% E5% 88% 86% E6% 9E% 90-% E5% A1% 9A% E6% 9C% AC% E9% 82% A6% I will read E5% B0% 8A / dp / 4839965250 / ref = tmm_pap_swatch_0? _ Encoding = UTF8 & qid = & sr =) and summarize the parts that I have some doubts or find useful. Therefore, I think the synopsis will be straightforward, but please read it, thinking that the content has nothing to do with this book.
Scipy is a library for scientific and technological calculations, and can perform various mathematical processes (linear algebra calculation, Fourier transform, etc.). Here, let's find the inverse matrix and eigenvalues of linear algebra, the solution of the equation, and so on.
>>> import scipy as sp
>>> import scipy.linalg as linalg
>>> from scipy.optimize import minimize_scalar
>>> matrix = np.array([[1,-1,-1],[-1,1,-1],[-1,-1,1]])
>>> matrix
array([[ 1, -1, -1],
[-1, 1, -1],
[-1, -1, 1]])
>>> print('Determinant', linalg.det(matrix))
Determinant-4.0
>>> print('Inverse matrix','\n', linalg.inv(matrix))
Inverse matrix
[[ 0. -0.5 -0.5]
[-0.5 -0. -0.5]
[-0.5 -0.5 0. ]]
>>> print('Matrix multiplication','\n',matrix.dot(linalg.inv(matrix)))
Matrix multiplication
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
>>> matrix*linalg.inv(matrix) #Element product of numpy
array([[ 0. , 0.5, 0.5],
[ 0.5, -0. , 0.5],
[ 0.5, 0.5, 0. ]])
>>> [email protected](matrix) #Product of numpy
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
>>> eig_value, eig_vector = linalg.eig(matrix)
>>> print('eigenvalue','\n',eig_value)
eigenvalue
[-1.+0.j 2.+0.j 2.+0.j]
>>> print('Eigenvector','\n',eig_vector)
Eigenvector
[[ 0.57735027 -0.81649658 0.42755853]
[ 0.57735027 0.40824829 -0.81618716]
[ 0.57735027 0.40824829 0.38862863]]
>>> eig_value, eig_vector = np.linalg.eig(matrix) #Same for numpy
>>> print('Eigenvector','\n',eig_vector)
Eigenvector
[[ 0.57735027 -0.81649658 0.42755853]
[ 0.57735027 0.40824829 -0.81618716]
[ 0.57735027 0.40824829 0.38862863]]
>>> print('eigenvalue','\n',eig_value) #eigenvalueは実数表記
eigenvalue
[-1. 2. 2.]
About scipy.linalg vs numpy.linalg 【reference】 Linear Algebra (scipy.linalg) "Scipy.linalg contains all the functions of numpy.linalg. In addition, it contains some other more advanced ones that are not included in numpy.linalg." "Another advantage of using scipy.linalg over numpy.linalg is that it is always compiled with BLAS / LAPACK support, whereas it is optional for numpy. Therefore, depending on how you installed numpy, The scipy version may be faster. " "Therefore, use scipy.linalg instead of numpy.linalg unless you don't want to add scipy as a dependency on your numpy program."
Example of optimization calculation
>>> def my_function(x):
... return (x**2 + 2*x + 1)
...
>>> from scipy.optimize import newton
>>> newton(my_function,0)
-0.9999999852953906
Use Brent's method And find the minimum value of my_function ().
>>> minimize_scalar(my_function, method = 'Brent')
fun: 0.0
nfev: 9
nit: 4
success: True
x: -1.0000000000000002
>>> def my_function2(x):
... return (x**3 + 2*x + 1)
...
>>> newton(my_function2,0)
-0.45339765151640365
SciPy Tutorial For a moment, with this alone, I can only write how to use the entrance, so I would like to copy the sutras soon. SciPy Tutorial Introduction Basic functions Special functions (scipy.special) Integration (scipy.integrate) Optimization (scipy.optimize) Interpolation (scipy.interpolate) Fourier Transforms (scipy.fft) Signal Processing (scipy.signal) Linear Algebra (scipy.linalg) Sparse eigenvalue problems with ARPACK Compressed Sparse Graph Routines (scipy.sparse.csgraph) Spatial data structures and algorithms (scipy.spatial) Statistics (scipy.stats) Multidimensional image processing (scipy.ndimage) File IO (scipy.io)
・ Summarized the basics of Scipy in this book ・ Scipy often suffers from numpy, but basic Scipy seems to be faster.
・ SciPy Tutorial has a lot of volume, but there are many interesting examples, so I would like to summarize it.
Recommended Posts