Indispensable if you use Python! How to use Numpy to speed up operations!

Reference site: Indispensable if you use Python! How to use Numpy to speed up operations!

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.

Install 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

Handle NumPy arrays

Array generation

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]]

Array arithmetic

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

Indispensable if you use Python! How to use Numpy to speed up operations!
How to speed up Python calculations
How to use numpy
How to speed up scikit-learn like conda Numpy
python3: How to use bottle (2)
[Python] How to use list 1
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
How to install and use pyenv, what to do if you can't switch python versions
Python: How to use async with
Numba to speed up as Python
[Python] How to use Pandas Series
How to use Requests (Python Library)
How to use SQLite in Python
[Python] How to use list 3 Added
How to use Mysql in python
How to use OpenPose's Python API
How to use ChemSpider in Python
How to use FTP with Python
Python: How to use pydub (playback)
How to use PubChem in Python
How to use python zip function
[Python] How to use Typetalk API
[Python] Summary of how to use pandas
[Introduction to Python] How to use class in Python?
How to speed up instantiation of BeautifulSoup
How to install and use pandas_datareader [Python]
[python] How to use __command__, function explanation
[Python] How to use import sys sys.argv
[Python] Organizing how to use for statements
python: How to use locals () and globals ()
How to use __slots__ in Python class
How to use "deque" for Python data
How to use Python zip and enumerate
[Python] Understand how to use recursive functions
Summary of how to use Python list
How to use regular expressions in Python
[Python2.7] Summary of how to use subprocess
How to use is and == in Python
[Blender x Python] How to use modifiers
[Question] How to use plot_surface of python
What to do if you can't use scikit grid search in Python
If you want to use NumPy, Pandas, Matplotlib, IPython, SciPy on Windows
If you want to count words in Python, it's convenient to use Counter.
[Python] How to use two types of type ()
[Introduction to Udemy Python3 + Application] 23. How to use tuples
Study from Python Hour7: How to use classes
How to use Raspberry Pi pie camera Python
How to use Python Image Library in python3 series
Summary of how to use MNIST in Python
[Algorithm x Python] How to use the list
How to use tkinter with python in pyenv
[Python] How to use hash function and tuple.
How to use Ruby's PyCall to enable pyenv Python