[PYTHON] [Introduction to Data Scientists] Basics of scientific calculation, data processing, and how to use graph drawing library ♬ Basics of Scipy

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.

Basics of Scipy

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.

2-3-1 Import of Scipy library

>>> import scipy as sp
>>> import scipy.linalg as linalg
>>> from scipy.optimize import minimize_scalar

2-3-2 Matrix calculation

2-3-2-1 Determinant and inverse matrix calculation
>>> 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.]])
2-3-2-2 Eigenvalues and eigenvectors
>>> 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."

2-3-3 Newton's method

Example of optimization calculation

2-3-3-1 Solving the equation
>>> def my_function(x):
...     return (x**2 + 2*x + 1)
...
>>> from scipy.optimize import newton
>>> newton(my_function,0)
-0.9999999852953906
2-3-3-2 Find the minimum value

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
Exercise 2-6
>>> 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)

Summary

・ 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

[Introduction to Data Scientists] Basics of scientific calculation, data processing, and how to use graph drawing library ♬ Basics of Scipy
[Introduction to Data Scientists] Basics of scientific calculation, data processing, and how to use graph drawing library ♬ Basics of Pandas
[Introduction to Data Scientists] Basics of scientific calculation, data processing, and how to use graph drawing library ♬ Basics of Matplotlib
[Introduction to Data Scientists] Basics of Python ♬ Functions and classes
[Introduction to Data Scientists] Basics of Python ♬
[Introduction to Data Scientists] Basics of Python ♬ Conditional branching and loops
[Introduction to Data Scientists] Basics of Python ♬ Functions and anonymous functions, etc.
How to use the graph drawing library Bokeh
[Introduction to Data Scientists] Basics of Probability and Statistics ♬ Probability / Random Variables and Probability Distribution
Introduction of DataLiner ver.1.3 and how to use Union Append
Jupyter Notebook Basics of how to use
Basics of PyTorch (1) -How to use Tensor-
Introduction of cyber security framework "MITRE CALDERA": How to use and training
Use decorators to prevent re-execution of data processing
How to use PyTorch-based image processing library "Kornia"
[Introduction to Python] How to use while statements (repetitive processing)
[Introduction to Azure for kaggle users] Comparison of how to start and use Azure Notebooks and Azure Notebooks VM
How to use Python Kivy ① ~ Basics of Kv Language ~
[Python] How to use the graph creation library Altair
[Introduction to Data Scientists] Descriptive Statistics and Simple Regression Analysis ♬
[python] How to use the library Matplotlib for drawing graphs
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
[Python] Summary of how to use split and join functions
Comparison of how to use higher-order functions in Python 2 and 3
[Introduction] How to use open3d
How to extract features of time series data with PySpark Basics
[Introduction to cx_Oracle] (Part 2) Basics of connecting and disconnecting to Oracle Database
[Introduction to Python] How to use the Boolean operator (and ・ or ・ not)
How to install and use Tesseract-OCR
How to use Requests (Python Library)
Summary of how to use pyenv-virtualenv
How to use .bash_profile and .bashrc
How to install and use Graphviz
Summary of how to use csvkit
From the introduction of GoogleCloudPlatform Natural Language API to how to use it
[Introduction to pytorch-lightning] How to use torchvision.transforms and how to freely create your own dataset ♬
[Introduction to Python] How to use class in Python?
How to install and use pandas_datareader [Python]
How to calculate Use% of df command
[Python2.7] Summary of how to use unittest
python: How to use locals () and globals ()
How to use "deque" for Python data
How to use Python zip and enumerate
Summary of how to use Python list
[Python2.7] Summary of how to use subprocess
How to use is and == in Python
How to use pandas Timestamp and date_range
[Question] How to use plot_surface of python
[Python] From morphological analysis of CSV data to CSV output and graph display [GiNZA]
OpenGoddard How to use 2-python library for nonlinear optimal control and orbit generation
How to use OpenGoddard 4-python library for nonlinear optimal control and orbit generation
How to use OpenGoddard 1-python library for nonlinear optimal control and orbit generation
[Introduction to Python] How to get the index of data with a for statement