Introduction to Python Numerical Library NumPy

This post is a formatted version of my blog post for Qiita. If there are additional items, I will write them on the blog. __ "Introduction to Python Numerical Library NumPy" __ http://rest-term.com/archives/2999/

Introduction to Python Numerical Library NumPy

numpy-sm.pngScientificComputingToolsForPython—Numpy

NumPy is an extension of the Python programming language that provides support for large multidimensional arrays and matrices, and a large high-level library of mathematical functions for manipulating them. (via Wikipedia)

I would like to review again NumPy, whose knowledge was ambiguous so far. NumPy is often used in scientific computing along with SciPy. In addition, since matrix operations can be performed at high speed, OpenCV (Computer Vision Library) now provides a Python interface using NumPy.

The Python binding of OpenCV was also covered in the blog in 2011, so for reference.

environment

CentOS 6.4 (x86_64) Python 2.7.5 (ELF 64-bit LSB executable) NumPy 1.7.1

What is a NumPy array (numpy.ndarray)?

An ndarray is a (usually fixed-size) multidimensional container of items of the same type and size.

numpy.ndarray is a class that handles multidimensional arrays. Basically, there are the following restrictions.

(* However, the shape of the array can be changed with restrictions. The explanation will be described later.) It's very similar to an array in C language. Due to the limitation, it is more efficient when dealing with large arrays compared to Python's list type. In addition, when the term "array" is used thereafter, it basically refers to numpy.ndarray.

import


##It seems customary to give an alias of np when importing.
import numpy as np

Numpy.ndarray attributes

The main attributes of numpy.ndarray are as follows. The data in ndarray is kept in a contiguous area of memory for efficient processing (), but you can refer to these attributes to see how the data is laid out in memory.

An instance of class ndarray consists of a contiguous one-dimensional segment of computer memory

ndarray.flags Memory layout information for array data(numpy.flagsobj)
ndarray.ndim Number of dimensions of array
ndarray.size Number of elements in the array
ndarray.shape Number of elements in each dimension
ndarray.itemsize Number of bytes per element
ndarray.strides Number of bytes required to move to the next element in each dimension
ndarray.nbytes Number of bytes in the entire array
ndarray.dtype Array element data type(numpy.dtype)

numpy.ndarray


##Two-dimensional array(queue)
>>> a = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
>>> a
array([[ 1,  2,  3],
       [ 4,  5,  6],
       [ 7,  8,  9],
       [10, 11, 12]])
>>> a.flags
  C_CONTIGUOUS : True   ##Is the data continuous in memory?(C array type)
  F_CONTIGUOUS : False  ##Same as above(Fortran array type)
  OWNDATA : True        ##View whether it's your own data(See below)In the case of False
  WRITEABLE : True      ##Is it possible to change the data?
  ALIGNED : True        ##Is the data type aligned?
  UPDATEIFCOPY : False  ##You can't change it to True, so you don't have to worry about it.
>>> a.ndim              ##Number of dimensions
2
>>> a.size              ##Element count
12
>>> a.shape             ##Number of elements in each dimension(Number of lines,Number of columns)
(4, 3)
>>> a.itemsize          ##Number of bytes per element
8
>>> a.strides           ##24 bytes for the next row, 8 bytes for the next column
(24, 8)
>>> a.nbytes            ##Number of bytes in the entire array(size*itemsize)
96
>>> a.dtype             ##Element data type(See below)
dtype('int64')

strides indicates that a [0,0] and a [0,1] are 8 bytes apart and a [0,0] and a [1,0] are 24 bytes apart in memory. If the data sequence is Fortran type, strikes will be (8, 32). In the case of one dimension, neither C type nor Fortran type is relevant.

About data type

The data type of array elements is handled by an object called numpy.dtype. The types that can be specified are roughly divided into five types: logical type, signed integer type, unsigned integer type, floating point type, and complex number type. After that, you can specify each by the number of bits of the data type. Please refer to the official website for details.

datatypes


## numpy.array()If you do not specify the dtype option in, it will be set automatically.
##Since it is a 64-bit environment here, it is generated with a 64-bit type.
>>> a = np.array([1,2,3,4,5])
>>> a.dtype
dtype('int64')    ##64-bit signed integer
>>> a.itemsize
8
>>> a = np.array([1.0,2.0,3.0,4.0,5.0])
>>> a.dtype
dtype('float64')  ##64-bit floating point number
>>> a.itemsize
8
## numpy.array()Specify the dtype option with
##32-bit signed integer
>>> a = np.array([1,2,3], dtype=np.int32)
>>> a.dtype
dtype('int32')
>>> a.itemsize
4
##8-bit unsigned integer(Often used in image processing)
>>> a = np.array([1,2,3], dtype=np.uint8)
>>> a.dtype
dtype('uint8')
>>> a.itemsize
1

Data type casting is also possible. Use ndarray.astype ().

ndarray.astype()


>>> a = np.array([1, 2, 3, 4, 5]))
>>> a.dtype
dtype('int64')
##Cast from int64 type to int32 type
>>> a.astype(np.int32)
array([1, 2, 3, 4, 5], dtype=int32)

>>> a = np.array([1.2, 2.4, 3.6, 4.8, 5.0])
>>> a.dtype
dtype('float64')
##Cast from float64 to int64 type, truncate after the decimal point
>>> a.astype(np.int64)
array([1, 2, 3, 4, 5])

Downcasting does not raise an exception. Also note that ndarray.astype () creates and returns a new array.

Array generation

First of all, from the array generation method. There are many ways to generate it, but here we will focus on the basic and commonly used methods.

python


>>> import numpy as np
## numpy.array()Generated by, list as an argument(Or tuple)give
>>> np.array([1,4,3,2,5])
array([1, 4, 3, 2, 5])
##Two-dimensional array is also OK
>>> np.array([[3,1,2], [6,4,5]])
array([[3, 1, 2],
       [6, 4, 5]])

## numpy.zeros()Generated by, the value of all elements is 0
>>> np.zeros(5)
array([ 0.,  0.,  0.,  0.,  0.])
>>> np.zeros([2,3])
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])

## numpy.ones()Generated by, the value of all elements is 1
>>> np.ones(5)
array([ 1.,  1.,  1.,  1.,  1.])
>>> np.ones([2,3])
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]])

## numpy.identity()Generated by, identity matrix(Since it is a square matrix, it has one argument.)
>>> np.identity(3)
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])

## numpy.eye()Generated by, identity()Similar to, but you can specify the number of columns
>>> np.eye(3, 2)
array([[ 1.,  0.],
       [ 0.,  1.],
       [ 0.,  0.]])

## numpy.arange()Generated by, built-in range()Same procedure as
>>> np.arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
## (start point,end point,incremental)Specify
>>> np.arange(1.0, 2.0, 0.2)
array([ 1. ,  1.2,  1.4,  1.6,  1.8])

## numpy.linspace()Generated by, arrange()Similar to, but you can specify the number of elements
>>> np.linspace(1, 4, 6)
array([ 1. ,  1.6,  2.2,  2.8,  3.4,  4. ])

## numpy.logspace()Generated by, the value is logarithmic scale(Common logarithm)Lined up with
>>> np.logspace(2, 3, 4)
array([  100.        ,   215.443469  ,   464.15888336,  1000.        ])
##bottom(base)Is specified as 2
>>> np.logspace(2, 4 ,4, base=2)
array([  4.        ,   6.34960421,  10.0793684 ,  16.        ])

## numpy.tile()Generated by, returns an array with repeated elements
>>> np.tile([0,1,2,3,4], 2)
array([0, 1, 2, 3, 4, 0, 1, 2, 3, 4])

## numpy.meshgrid()Generated by, grid-like array that is evenly spaced vertically and horizontally
>>> a, b = np.meshgrid([1,2,3], [4,5,6,7])
>>> a
array([[1, 2, 3],
       [1, 2, 3],
       [1, 2, 3],
       [1, 2, 3]])
>>> b
array([[4, 4, 4],
       [5, 5, 5],
       [6, 6, 6],
       [7, 7, 7]])

## numpy.tri()Generated by, triangular matrix
>>> np.tri(3)
array([[ 1.,  0.,  0.],
       [ 1.,  1.,  0.],
       [ 1.,  1.,  1.]])

## numpy.diag()Returns an array with diagonal elements extracted from the input array generated by
>>> a = np.array([[0,1,2], [3,4,5], [6,7,8]])
>>> a
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
>>> np.diag(a)
array([0, 4, 8])

## numpy.empty()Generated by, only secure area, not initialized
>>> np.empty(5)
array([  1.06452759e-312,   1.06452759e-312,   1.00000000e+000,
         1.00000000e+000,   2.37151510e-322])

## ndarray.copy()Deep copy of the array with
>>> a = np.array([1,2,3])
>>> b = a.copy()

## numpy.Use of random module
## numpy.random.randint()Generates an array with integer random values as elements
##Range of random numbers to generate(Minimum value, maximum value), Specify the number of elements
>>> np.random.randint(0,100,10)
array([54, 68, 19, 57, 23, 27, 36, 99, 53, 70])

Change of array shape

The array shape of the ndarray object can be changed with restrictions.

python


##One-dimensional array
>>> a = np.arange(10)
>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

## ndarray.reshape()Change the array shape with
##Change to 2D array, return view
>>> b = a.reshape((2,5))
>>> b
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])
##Change the value of the view
>>> b[0] = 100
>>> b
array([[100, 100, 100, 100, 100],
       [  5,   6,   7,   8,   9]])
##The original data is also changed
>>> a
array([100, 100, 100, 100, 100,   5,   6,   7,   8,   9])
##An error will occur if the number of elements is different.
>>> a.reshape((2,3))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: total size of new array must be unchanged

## ndarray.resize()Change the array shape with(in-place)
>>> a = np.arange(10)
##Change to a two-dimensional array with the same number of elements, no view returned
>>> a.resize((2,5))
##The shape of the original data is changed
>>> a
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])
##Number of elements if the variable is referenced even once after generation(Array length)Cannot be changed
>>> a.resize(11)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: cannot resize an array references or is referenced
by another array in this way.  Use the resize function
##However, if the refcheck option is set to False, reference checking will not be performed.
>>> a.resize(11, refcheck=False)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0])

## ndarray.flatten()Change to a one-dimensional array with, returns a copy instead of a view
>>> a = np.arange(10).reshape((2,5))
>>> a
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])
>>> b = a.flatten()
>>> b
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

It's a good idea to know if the returned array will be a view or a copy. Also, ndarray.reshape () is used a lot, so it's a good idea to remember this as well.

Indexing Introduces array element reference / assignment, array slice, etc. It's basically similar to Python's list, but it has some extended syntax, so check it out.

python


##Two-dimensional array(queue)
>>> a = np.array([[0,1,2], [3,4,5], [6,7,8]])
>>> a
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

##Element reference/Substitution(Same as list)
>>> a[2]
array([7, 8, 9])
>>> a[1][2]
6
>>> a[1][2] = 1
>>> a[1][2]
1
##IndexError if out of range is specified
>>> a[3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: index out of bounds

##See comma separated/Substitution(Extended syntax)
## a[line,Column]
>>> a[1,2]  
6
##If you specify a negative number for the index, it counts from the end
>>> a[-1,-2]
8
>>> a[1,2] = 10
>>> a[1,2]
10

##slice(Notation is the same as list, return value is view)
## a[start:end:step]
>>> a = np.arange(10)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> a[1:8:2]
array([1, 3, 5, 7])
>>> a[3:]    ## a[3:10:1]
array([3, 4, 5, 6, 7, 8, 9])
>>> a[:8]    ## a[0:8:1]
array([0, 1, 2, 3, 4, 5, 6, 7])
>>> a[::-2]  ## a[-1:-10:-2]
array([9, 7, 5, 3, 1])

##Can be combined
>>> a = np.arange(25).reshape((5,5))
>>> a
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])
>>> a[:,3] = 10     ## a[0:5:1,3] = 10 (Update multiple elements at once)
>>> a
array([[ 0,  1,  2, 10,  4],
       [ 5,  6,  7, 10,  9],
       [10, 11, 12, 10, 14],
       [15, 16, 17, 10, 19],
       [20, 21, 22, 10, 24]])
>>> a[:2,:3] = 100  ## a[0:2:1,0:3:1] = 100
>>> a
array([[100, 100, 100,  10,   4],
       [100, 100, 100,  10,   9],
       [ 10,  11,  12,  10,  14],
       [ 15,  16,  17,  10,  19],
       [ 20,  21,  22,  10,  24]])
>>> a[2::2,::2]     ## a[2:5:2,0:5:2](Cases that are difficult to read if omitted)
array([[10, 12, 14],
       [20, 22, 24]])

Fancy Indexing It is also possible to specify a special index masked by the position and conditions of the element.

python


>>> a = np.arange(15,0,-1)
>>> a
array([15, 14, 13, 12, 11, 10,  9,  8,  7,  6,  5,  4,  3,  2,  1])
##Index specified as an integer array, return a copy
>>> a[[0,2,4,8]]
array([15, 13, 11,  7])
##Index specified in bool type array, return copy
>>> n = a>10
>>> n
array([ True,  True,  True,  True,  True, False, False, False, False,
       False, False, False, False, False, False], dtype=bool)
>>> a[n]
array([15, 14, 13, 12, 11])
##It is OK to specify the condition directly in the index
>>> a[a>10]
array([15, 14, 13, 12, 11])
##Use bitwise operators when there are multiple conditions
>>> a[(4<a) & (a<10)]
array([9, 8, 7, 6, 5])
##You can also assign values
>>> a[a<8] = 0
>>> a
array([15, 14, 13, 12, 11, 10,  9,  8,  0,  0,  0,  0,  0,  0,  0])

Index search

python


## numpy.argmax()Returns the smallest index of the maximum value elements
>>> a = np.tile(np.arange(3),3)
>>> a
array([0, 1, 2, 0, 1, 2, 0, 1, 2])
>>> np.argmax(a)
2

## numpy.argmin()Returns the smallest index of the minimum value elements
>>> np.argmin(a)
0

## numpy.nonzero()Returns an index array of nonzero elements in
>>> a = np.eye(3)
>>> a
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])
>>> np.nonzero(a)  ##Since it is a two-dimensional array, two arrays are returned.
(array([0, 1, 2]), array([0, 1, 2]))

## numpy.where()Returns an index array of elements that match the conditions with
>>> a = np.arange(15).reshape((3,5))
>>> a
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])
>>> np.where(a%2==0)
(array([0, 0, 0, 1, 1, 2, 2, 2]), array([0, 2, 4, 1, 3, 0, 2, 4]))

## numpy.select()Search multiple conditions with, set the value to the index
##First argument:Array of conditions(bool array)
##Second argument:An array of values to set in the index of the element that matches the condition
>>> a = np.arange(10)
>>> np.select([a<3, a>5], [a, a**2])
array([ 0,  1,  2,  0,  0,  0, 36, 49, 64, 81])

Even though the internal structure of ndarray is similar to C array, the interface is a high-level language. However, if you use too much NumPy-specific notation in array slicing processing, it may be difficult to read, so it seems better to use it modestly when developing with multiple people.

Operations / operations on arrays

Various operations are possible in addition to the above-mentioned change of the arrangement shape. I can't cover all of them here, so I'll list only a part of them.

Array join / split, axis manipulation

python


>>> a = np.arange(9).reshape((3,3))
>>> a
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
>>> b = np.arange(8,-1,-1).reshape((3,3))
>>> b
array([[8, 7, 6],
       [5, 4, 3],
       [2, 1, 0]])

## numpy.dstack()Combine two-dimensional arrays into a three-dimensional array with
>>> np.dstack((a,b))
array([[[0, 8],
        [1, 7],
        [2, 6]],

       [[3, 5],
        [4, 4],
        [5, 3]],

       [[6, 2],
        [7, 1],
        [8, 0]]])

## numpy.hstack()Join in column direction with
>>> np.hstack((a,b))
array([[0, 1, 2, 8, 7, 6],
       [3, 4, 5, 5, 4, 3],
       [6, 7, 8, 2, 1, 0]])

## numpy.vstack()Join in the row direction with
>>> np.vstack((a,b))
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8],
       [8, 7, 6],
       [5, 4, 3],
       [2, 1, 0]])

## numpy.dsplit()Divide the 3D array with
>>> a = np.arange(16).reshape(2,2,4)
>>> a
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7]],

       [[ 8,  9, 10, 11],
        [12, 13, 14, 15]]])

>>> np.dsplit(a,2)
[array([[[ 0,  1],
        [ 4,  5]],

       [[ 8,  9],
        [12, 13]]]), array([[[ 2,  3],
        [ 6,  7]],

       [[10, 11],
        [14, 15]]])]

## numpy.hsplit()Split in the column direction with
>>> a = np.arange(16).reshape(4,4)
>>> a
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])
>>> np.hsplit(a,2)
[array([[ 0,  1],
       [ 4,  5],
       [ 8,  9],
       [12, 13]]), array([[ 2,  3],
       [ 6,  7],
       [10, 11],
       [14, 15]])]

## numpy.vsplit()Split in the row direction with
>>> np.vsplit(a,2)
[array([[0, 1, 2, 3],
       [4, 5, 6, 7]]), array([[ 8,  9, 10, 11],
       [12, 13, 14, 15]])]

## numpy.transpose()Transpose the array with
>>> a = np.array([[1, 2], [3, 4]])
>>> a
array([[1, 2],
       [3, 4]])
>>> np.transpose(a)
array([[1, 3],
       [2, 4]])
## ndarray.T is fine
>>> a.T
array([[1, 3],
       [2, 4]])
       
## numpy.swapaxes()Replacing the shaft with
>>> a = np.array([[1,2,3]])
>>> np.swapaxes(a, 0, 1)
array([[1],
       [2],
       [3]])

Array sort

python


## numpy.sort()Sort the array with and return a copy
>>> a = np.random.randint(0,100,10)
>>> a
array([75, 24, 74, 49, 93, 18, 19, 85, 73, 90])
>>> np.sort(a)
array([18, 19, 24, 49, 73, 74, 75, 85, 90, 93])

## ndarray.sort()Destructive array with(in-place)Sort to
>>> a.sort()
>>> a
array([18, 19, 24, 49, 73, 74, 75, 85, 90, 93])

##For multidimensional arrays
>> a = np.array([[1,4,2],[9,6,8],[5,3,7]])
>>> a
array([[1, 4, 2],
       [9, 6, 8],
       [5, 3, 7]])
>>> np.sort(a)
array([[1, 2, 4],
       [6, 8, 9],
       [3, 5, 7]])
##Specify the axis(Sort by column here)
>>> np.sort(a, axis=0)
array([[1, 3, 2],
       [5, 4, 7],
       [9, 6, 8]])
##Sort into a one-dimensional array
>>> np.sort(a, axis=None)
array([1, 2, 3, 4, 5, 6, 7, 8, 9])

Operations on array elements

It provides element-wise arithmetic capabilities for various array elements. Many of these functions can also be used using each operator.

python


>>> a = np.array([1,2,3])
>>> b = np.array([4,5,6])
##Addition
>>> a + b    ## np.add(a,b)
array([5, 7, 9])
##Subtraction
>>> b - a    ## np.subtract(b,a)
array([3, 3, 3])
##Multiply
>>> a * b    ## np.multiply(a,b)
array([ 4, 10, 18])
##division
>>> b / a    ## np.divide(b,a)
array([4, 2, 2])
##Surplus
>>> a % b    ## np.mod(a,b)
array([1, 2, 3])
##Exponentiation
>>> a ** b   ## np.power(a,b)
array([  1,  32, 729])
##Sign inversion
>>> -a       ## np.negative(a)
array([-1, -2, -3])

Broadcasting Each operation on the array element mentioned above was performed between arrays of the same size / shape, but with NumPy it is also possible to perform operations between arrays of different sizes / shapes, which is called broadcast.

python


>>> a = np.array([1,2,3,4,5])
>>> b = np.array([10])
>>> a * b    ## (1,5) * (1,1) = (1,5)
array([10, 20, 30, 40, 50]

>>> a = np.array([0,10,20,30]).reshape((4,1))
>>> a
array([[ 0],
       [10],
       [20],
       [30]])
>>> b = np.array([0,1,2])
>>> a + b    ## (4,1) + (1,3) = (4,3)
array([[ 0,  1,  2],
       [10, 11, 12],
       [20, 21, 22],
       [30, 31, 32]])

##Examples that cannot be broadcast
>>> b = np.array([[1,2],[3,4]])
>>> a + b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: operands could not be broadcast together with shapes (4,1) (2,2)

The second example is interesting. It seems that the amount of code can be greatly reduced if the broadcast function is used well.

Array scan

Array traversal can be done using the for in syntax, similar to a list.

python


>>> a = np.arange(9).reshape((3,3))
>>> a
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])

##list(list)Can be scanned with for in as well as
##Here we scan row by row
>>> for row in a:
...     print row
...
[0 1 2]
[3 4 5]
[6 7 8]
       
## numpy.ndenumerate()Scanning a multidimensional array with
## enumerate()You can get the index as well as
>>> for i, value in np.ndenumerate(a):
...     print i, value
...
(0, 0) 0
(0, 1) 1
(0, 2) 2
(1, 0) 3
(1, 1) 4
(1, 2) 5
(2, 0) 6
(2, 1) 7
(2, 2) 8

## ndarray.Scan as a one-dimensional array with flat
>>> for value in a.flat:
...     print value
...
0
1
2
3
4
5
6
7
8

Statistical function

NumPy provides a set of functions for basic statistical processing.

python


>>> a = np.random.randint(0,500,20)
>>> a
array([260, 253, 185, 240, 252, 375,  63, 413, 293, 431, 207, 230, 288,
       462, 270, 477, 451,  58, 408, 492])
>>> np.amax(a)       ##Maximum value
492
>>> np.amin(a)       ##minimum value
58
>>> np.ptp(a)        ##Range of values(Maximum value-minimum value)
434
>>> np.mean(a)       ##Arithmetic mean
305.39999999999998
>>> np.median(a)     ##Median
279.0
>>> np.std(a)        ##standard deviation
125.73519793597973
>>> np.var(a)        ##Distributed
15809.34
>>> b = np.random.randint(0,500,20)
>>> b
array([313, 117, 167, 353, 468, 289, 177, 196,  20,  70, 235, 280, 480,
       125, 195, 271, 253,  55,  49, 354])
>>> np.corrcoef(a,b) ##Correlation coefficient
array([[ 1.        ,  0.05950681],
       [ 0.05950681,  1.        ]])
>>> c = np.random.randint(0,10,20)
>>> c
array([3, 8, 7, 9, 1, 8, 4, 0, 8, 3, 9, 4, 2, 1, 4, 3, 0, 4, 8, 4])
>>> np.histogram(c)  ##histogram
(array([2, 2, 1, 3, 5, 0, 0, 1, 4, 2]), array([ 0. ,  0.9,  1.8,  2.7,  3.6,  4.5,  5.4,  6.3,  7.2,  8.1,  9. ]))

If you want to do more advanced statistical processing, you will probably use SciPy.

File input / output

You can also input and output array data files. This function is often taken care of in experimental work.

python


>>> a = np.array([[1,3,2], [4,6,5]])
>>> a
array([[1, 3, 2],
       [4, 6, 5]])
## numpy.savetxt()Write array data to a file in ASCII format with
>>> np.savetxt('data.txt', a)
##Delimiter can be specified freely as an option(TSV format)
>>> np.savetxt('data.txt', a, delimiter='\t')

##Since it is in ASCII format, the contents of the file can be read by humans.
$ cat data.txt
1.000000000000000000e+00        3.000000000000000000e+00        2.000000000000000000e+00
4.000000000000000000e+00        6.000000000000000000e+00        5.000000000000000000e+00

## numpy.loadtxt()Read array data from a text file with
>>> np.loadtxt('data.txt', delimiter='\t')
array([[ 1.,  3.,  2.],
       [ 4.,  6.,  5.]])

## numpy.save()Write array data to a file in binary format with
##The extension of the output file is.Become npy
>>> np.save('data', a)

## numpy.load()Read array data from a binary file with
>>> np.load('data.npy')
array([[1, 3, 2],
       [4, 6, 5]])

## numpy.savez()Write multiple array data to a file in binary format with
>>> a, y = np.meshgrid([1,2,3], [4,5,6,7])
>>> a
array([[1, 2, 3],
       [1, 2, 3],
       [1, 2, 3],
       [1, 2, 3]])
>>> y
array([[4, 4, 4],
       [5, 5, 5],
       [6, 6, 6],
       [7, 7, 7]])
##The extension of the output file is.becomes npz
>>> np.savez('data', x=x, y=y)

## .npy, .Both npz files are numpy.load()Can be read by
>>> npz = np.load('data.npz')
>>> npz.files
['y', 'x']
>>> npz['x']
array([[1, 2, 3],
       [1, 2, 3],
       [1, 2, 3],
       [1, 2, 3]])
>>> npz['y']
array([[4, 4, 4],
       [5, 5, 5],
       [6, 6, 6],
       [7, 7, 7]])

When saving a huge amount of data, it is better to save it in binary format because the file size will be smaller.

This time I tried to organize the basics of NumPy. I rarely use NumPy on its own, but it's often taken care of when using OpenCV's Python bindings. Since NumPy has a lot of functions, it seems that you need to refer to the official reference each time you need to use it.

Recommended Posts

Introduction to Python Numerical Library NumPy
[Introduction to Python] <numpy ndarray> [edit: 2020/02/22]
[Introduction to Udemy Python 3 + Application] 10. Numerical values
Introduction to Python language
Introduction to OpenCV (python)-(2)
Introduction to Python Django (2) Win
Convert numpy int64 to python int
Introduction to serial communication [Python]
[Introduction to Python] <list> [edit: 2020/02/22]
Introduction to Python (Python version APG4b)
An introduction to Python Programming
Introduction to Python For, While
[Introduction to Python] Basic usage of the library matplotlib
Introduction to Python numpy pandas matplotlib (~ towards B3 ~ part2)
[Introduction to Udemy Python 3 + Application] 58. Lambda
[Introduction to Udemy Python 3 + Application] 31. Comments
Subscript access to python numpy array
Practice! !! Introduction to Python (Type Hints)
[Introduction to Python3 Day 1] Programming and Python
[Introduction to Udemy Python 3 + Application] 57. Decorator
Introduction to Python Hands On Part 1
[Introduction to Python3 Day 13] Chapter 7 Strings (7.1-7.1.1.1)
How to use Requests (Python Library)
[Introduction to Python] How to parse JSON
[Introduction to Udemy Python 3 + Application] 56. Closure
[Introduction to Python3 Day 14] Chapter 7 Strings (7.1.1.1 to 7.1.1.4)
Introduction to Protobuf-c (C language ⇔ Python)
[Introduction to Udemy Python3 + Application] 59. Generator
[Python] How to import the library
[Introduction to Python3 Day 15] Chapter 7 Strings (7.1.2-7.1.2.2)
[Introduction to Python] Let's use pandas
Introduction to Machine Learning Library SHOGUN
[Introduction to Python] Let's use pandas
[Introduction to Udemy Python 3 + Application] Summary
Introduction to image analysis opencv python
[Introduction to Python] Let's use pandas
An introduction to Python for non-engineers
Introduction to Python Django (2) Mac Edition
[AWS SAM] Introduction to Python version
[Introduction to Python3 Day 21] Chapter 10 System (10.1 to 10.5)
[Python Tutorial] An Easy Introduction to Python
Python learning memo for machine learning by Chainer Chapter 8 Introduction to Numpy
[Introduction to Udemy Python3 + Application] 18. List methods
[Introduction to Udemy Python3 + Application] 63. Generator comprehension
[Introduction to Udemy Python3 + Application] 28. Collective type
[Introduction to Python] How to use class in Python?
[Introduction to Udemy Python3 + Application] 25. Dictionary-type method
[Introduction to Udemy Python3 + Application] 33. if statement
Introduction to Discrete Event Simulation Using Python # 1
[Introduction to Udemy Python3 + Application] 13. Character method
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.1-8.2.5)
[Introduction to Udemy Python3 + Application] 55. In-function functions
[Introduction to Udemy Python3 + Application] 48. Function definition
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.3-8.3.6.1)
A super introduction to Python bit operations
Introduction to Python Image Inflating Image inflating with ImageDataGenerator
Web-WF Python Tornado Part 3 (Introduction to Openpyexcel)
[Introduction to Udemy Python3 + Application] 21. Tuple type
[Introduction to Udemy Python3 + Application] 45. enumerate function
[Introduction to Udemy Python3 + Application] 41. Input function
[Introduction to Python] Let's use foreach with Python