Reference site: [Introduction to Python] Basic usage of the library scipy that you absolutely must know
There are many useful libraries in Python, and scipy is one of them. scipy is a library for performing advanced scientific calculations. Numpy exists in a similar library, but scipy can perform array and matrix operations that can be done with numpy, as well as further calculations such as signal processing and statistics.
This time, I will explain the basic usage of scipy.
table of contents 1 Install scipy 1.1 Installing numpy 1.2 Installation of scipy 2 Use scipy 2.1 Integral 2.2 Matrix operation
You need numpy to use scipy, so let's install numpy first.
Then install scipy. scipy can be installed with the pip command.
pip install scipy
If you cannot install with pip, download Scipy from the following page and install the downloaded file with the pip command.
http://www.lfd.uci.edu/~gohlke/pythonlibs/
pip command
pip install the path to the file you just downloaded
There are several versions of scipy, so download the version that suits your python or OS. For example, the file "scipy-0.18.1-cp36-cp36m-win_amd64.whl" is for python3.6, 64-bit Windows.
After installing scipy, let's use it immediately. However, scipy has countless features, so I can't cover all of them. Here are just a few of the most commonly used features.
If you use scipy, you can easily integrate the function (definite integral). Use quad () in the scipy.integrate module for integration.
from scipy import integrate #Must be required
Variable 1,Variable 2= integrate.quad(function,The beginning of the integration interval,End of integration interval)
quad () definitely integrates the given function at the interval [beginning of interval, end of interval] and returns two values. The result of integration is returned in variable 1, and the error in the integral calculation is returned in variable 2.
from scipy import integrate
#2x+Represents 5
def func(x):
return 2*x + 5
result, err = integrate.quad(func, 0, 5)
print('Integral result:{0}\n error:{1}'.format(result, err))
Execution result
Integral result: 50.0 Error: 5.551115123125783e-13
Since it is difficult, I will omit it here, but you can perform advanced operations such as two-dimensional integration and differential equations as well as definite integrals.
Matrix operations could be done with numpy, but scipy can be used for more advanced operations. For matrix operations, use scipy.linalg, which summarizes the functions for linear algebra.
rom scipy import linalg
import numpy as np #use numpy
narray = np.array([[1, 2], [3, 4]]) #Representing a matrix with a numpy array
inv_narray = linalg.inv(narray)
print('Inverse matrix of narray:\n{}\n'.format(inv_narray))
result_det = linalg.det(inv_narray)
print('inv_Determinant of narray:{}\n'.format(result_det))
inv_narray_norm = linalg.norm(inv_narray)
print('inv_narray norm:{}'.format(inv_narray_norm))
Execution result
Inverse matrix of narray: [[-2. 1. ] [ 1.5 -0.5]] Determinant of inv_narray: -0.49999999999999967 norm of inv_narray: 2.73861278752583
In addition to this, scipy can easily perform advanced scientific calculations such as signal processing, image analysis, statistical processing, and Fourier transform. The number is huge and I can't explain all of them here, so if you want to use scipy more, please refer to the official documentation. Official Document
Recommended articles ➡ Reputation of Python books and reference books ➡ Must-see for anyone who wants to learn Python! 5 Recommended Schools Summary
Recommended Posts