Reference site: Indispensable if you use Python! How to use Numpy to speed up operations!
There are many useful libraries in Python that you can use freely. By using the library, you can easily realize advanced calculations and complicated processing. NumPy is one of the most commonly used libraries. NumPy is used for high-speed array and matrix operations in scientific and technological calculations. It is a very effective library when you want to do mathematical calculations in research.
This time, I will explain the basic usage of such NumPy.
NumPy can be easily installed using the pip command. Enter the following command from the console to install.
pip install numpy
However, the installation with pip may fail depending on the version of Python. In that case, please download NumPy from the following page and install the downloaded file with the pip command.
http://www.lfd.uci.edu/~gohlke/pythonlibs/
There are multiple NumPy files, so please download the one that matches your Python version and OS version. In the file name, cp ○○ indicates the Python version, and win_amd ○○ indicates the OS version.
For example, the file "numpy-1.12.0rc2 + mkl-cp36-cp36m-win_amd64.whl" means that the Python version is 3.6 and the OS version is 64bitOS.
After the download is complete, use pip to install the file. The command is as follows.
pip install The path of the file you downloaded earlier
Import NumPy
After installing NumPy, import NumPy. Now you're ready to use NumPy.
import numpy #Must be required when using NumPy
NumPy is a library that can perform array and matrix calculations at high speed. First, generate an array with NumPy. An array of NumPy can be generated with the array method.
import numpy
n_array = numpy.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(n_array)
Execution result
[[1 2 3] [4 5 6] [7 8 9]]
In addition, NumPy can express matrices used in mathematics. Use matrix to generate a matrix.
n_matrix = numpy.matrix('1,2 ; 3, 4')
print(n_matrix)
Implementation result [[1 2] [3 4]]
Arrays in NumPy can be treated like regular Python lists, and matrices are, in other words, almost the same as two-dimensional lists. So what's the difference from a regular list? It's easy to do mathematical operations on matrices and arrays.
For example, multiplying a regular list by a constant duplicates the list, but multiplying an array of NumPy by a constant performs an operation that multiplies the matrix by a constant, resulting in an array in which each element is multiplied by a constant.
n_array = numpy.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(n_array * 3)
Execution result
[[ 3 6 9] [12 15 18] [21 24 27]]
In addition to this, you can perform array and constants, and four arithmetic operations between arrays. In addition, NumPy has many functions for operations, and you can also use mathematical functions defined in the math module, which is a standard library. The functions available in NumPy include:
function | effect |
---|---|
numpy.add(Array,constant) | Arrayに定数を足す |
numpy.multiply(Array,constant) | Arrayに定数を掛ける |
Array.dot(Array) | Array(行列)の内積を計算する |
numpy.mean(Array) | Arrayの平均を計算する |
numpy.median(Array) | Arrayの中央値を計算する |
numpy.std(Array) | Arrayの標準偏差を計算する |
numpy.var(Array) | Arrayの分散を計算する |
n_array = numpy.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print('n_Average value of array:{}'.format(numpy.mean(n_array)))
print('n_Median of array:{}'.format(numpy.median(n_array)))
print('n_standard deviation of array:{}'.format(numpy.std(n_array)))
Execution result
Average value of n_array: 5.0 Median n_array: 5.0 Standard deviation of n_array: 2.581988897471611
Recommended Posts