[Python] Numpy reference, extraction, combination

Specify one location to access

In the case of one dimension

Specify with X [index]. The index of the first data will be 0. (In the case of Matlab, it is 1 so it is confusing)

In [1]: import numpy as np

In [2]: X = np.arange(10)

In [3]: print X
[0 1 2 3 4 5 6 7 8 9]

In [4]: print X[0]
0

In [5]: print X[2]
2

If you want to count the last data and specify it, use X [-index]. It does not have to be X [len (X) -1] like C language. The Index of the last data is -1, and when you specify it from the beginning, it starts from 0, so it tends to be a little confusing.

In [6]: print X[-1]
9

In the case of two dimensions

In the case of multiple dimensions, specify each by separating them with commas (,). The method of specification is the same as for one-dimensional.

In [17]: X = np.arange(25).reshape(5,5)

In [18]: print X
[[ 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]]

In [19]: print X[3,2]
17

In [20]: print X[-1,-2]
23

Access by specifying a range

In the case of one dimension

To access by specifying the range, specify as X [start: end: step]. Note that end is not included at this time. (End is included in Matlab.) If start is omitted, it will be from the beginning, if end is omitted, it will be the end, and if step is omitted, step will be 1.

In [7]: print X[1:3]
[1 2]

In [8]: print X[3:]
[3 4 5 6 7 8 9]

In [9]: print X[:3]
[0 1 2]

In [10]: print X[:]
[0 1 2 3 4 5 6 7 8 9]

In [11]: print X[::]
[0 1 2 3 4 5 6 7 8 9]

In [12]: print X[::2]
[0 2 4 6 8]

Negative Index may be specified for start and end. If step is set to minus, the data will be acquired from the end.

In [13]: print X[-3:-1]
[7 8]

In [14]: print X[-3:]
[7 8 9]

In [15]: print X[:-3]
[0 1 2 3 4 5 6]

In [16]: print X[::-1]
[9 8 7 6 5 4 3 2 1 0]

In the case of two dimensions

In the case of multiple dimensions, specify each by separating them with commas (,). The method of specification is the same as for one-dimensional.

In [57]: X = np.arange(25).reshape(5,5)

In [58]: print X
[[ 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]]

In [59]: print X[1:3,0:2]
[[ 5  6]
 [10 11]]

In [60]: print X[1:3,::-1]
[[ 9  8  7  6  5]
 [14 13 12 11 10]]

Extract rows

The results of the three methods X [row], X [row,], and X [row ,:] are the same. However, X [row,] is not recommended because X [, col] cannot be used when extracting the columns described later. The row part may be specified in one place or in a range.

In [21]: print X[1]
[5 6 7 8 9]

In [22]: print X[1,]
[5 6 7 8 9]

In [23]: print X[1,:]
[5 6 7 8 9]

Extract columns

The column specified by X [:, col] can be extracted, but the extracted result will be a one-dimensional numpy array as shown below. As with Matlab, if you extract a column, it becomes a column vector, so you can use it for matrix operations as it is, In the case of numpy, the extracted result will be a one-dimensional numpy array, so be careful when using this result for matrix operations.

In [24]: print X[:,1]
[ 1  6 11 16 21]

Extract data that meets the conditions

If X <3, a numpy array is returned in which the part that satisfies the condition is True and the part that does not satisfy is False.


In [1]: import numpy as np

In [2]: X = np.arange(24,-1,-1).reshape(5,5)

In [3]: print X
[[24 23 22 21 20]
 [19 18 17 16 15]
 [14 13 12 11 10]
 [ 9  8  7  6  5]
 [ 4  3  2  1  0]]

In [4]: X < 3
Out[4]: 
array([[False, False, False, False, False],
       [False, False, False, False, False],
       [False, False, False, False, False],
       [False, False, False, False, False],
       [False, False,  True,  True,  True]], dtype=bool)

In [5]: X % 2 == 0
Out[5]: 
array([[ True, False,  True, False,  True],
       [False,  True, False,  True, False],
       [ True, False,  True, False,  True],
       [False,  True, False,  True, False],
       [ True, False,  True, False,  True]], dtype=bool)

If you set X [X <3] or X [X% 2 == 0], the value of the part that satisfies the condition is returned.

In [6]: X[X<3]
Out[6]: array([2, 1, 0])

In [7]: X[X%2==0]
Out[7]: array([24, 22, 20, 18, 16, 14, 12, 10,  8,  6,  4,  2,  0])

Rewrite data that meets the conditions

If X [X% 2 == 0] = 0, only the True part is rewritten.

In [8]: X[X%2==0]=0

In [9]: print X
[[ 0 23  0 21  0]
 [19  0 17  0 15]
 [ 0 13  0 11  0]
 [ 9  0  7  0  5]
 [ 0  3  0  1  0]]

The inside of [] does not have to be an expression, as long as it is a numpy array whose rewriting location is True.

In [10]: X = np.arange(24,-1,-1).reshape(5,5)

In [11]: cond = X%2==0

In [12]: print cond
[[ True False  True False  True]
 [False  True False  True False]
 [ True False  True False  True]
 [False  True False  True False]
 [ True False  True False  True]]

In [13]: X[cond]=0

In [14]: print X
[[ 0 23  0 21  0]
 [19  0 17  0 15]
 [ 0 13  0 11  0]
 [ 9  0  7  0  5]
 [ 0  3  0  1  0]]

Retrieves the specified row and column values.

When specifying a line

In [34]: X = np.arange(24,-1,-1).reshape(5,5)

In [35]: print X
[[24 23 22 21 20]
 [19 18 17 16 15]
 [14 13 12 11 10]
 [ 9  8  7  6  5]
 [ 4  3  2  1  0]]

In [36]: X[[1,3],:]
Out[36]: 
array([[19, 18, 17, 16, 15],
       [ 9,  8,  7,  6,  5]])

When specifying a column

In [37]: X[:,[0,2]]
Out[37]: 
array([[24, 22],
       [19, 17],
       [14, 12],
       [ 9,  7],
       [ 4,  2]])

The case where the row is specified and the case where the column is specified are combined as follows. In this, the data in the 1st row and 0th column and the 3rd row and 2nd column are taken out.

In [38]: X[[1,3],[0,2]]
Out[38]: array([19,  7])

To retrieve the data in the 1st or 3rd row and the 0th and 2nd columns, use np.ix_ as shown below. np.ix_ returns a tuple of numpy array with row and column numbers.

In [39]: X[np.ix_([1,3],[0,2])]
Out[39]: 
array([[19, 17],
       [ 9,  7]])

In [40]: np.ix_([1,3],[1,2])
Out[40]: 
(array([[1],
        [3]]), array([[1, 2]]))

Extract non-zero locations

Find is used for Matlab, but nonzero () is used for numpy. Alternatively, it may be np.where.

In [44]: X = np.arange(24,-1,-1).reshape(5,5)

In [45]: i,j=X.nonzero()

In [46]: print i
[0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4]

In [47]: print j
[0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3]

In [48]: X[i,j]
Out[48]: 
array([24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10,  9,  8,
        7,  6,  5,  4,  3,  2,  1])
In [51]: i,j=np.where(X!=0)

In [52]: print i
[0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4]

In [53]: print j
[0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3]

In [54]: X[i,j]
Out[54]: 
array([24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10,  9,  8,
        7,  6,  5,  4,  3,  2,  1])

Combine numpy arrays.

Combine vertically

use vstack

In [76]: X = np.arange(5)

In [77]: Y = np.arange(4,-1,-1)

In [78]: print X
[0 1 2 3 4]

In [79]: print Y
[4 3 2 1 0]

In [80]: np.vstack((X,Y))
Out[80]: 
array([[0, 1, 2, 3, 4],
       [4, 3, 2, 1, 0]])

Join sideways

use hstack

In [81]: np.hstack((X,Y))
Out[81]: array([0, 1, 2, 3, 4, 4, 3, 2, 1, 0])

When X and Y are made into column vectors and connected horizontally, it becomes as follows.

In [82]: X.reshape(len(X),1)
Out[82]: 
array([[0],
       [1],
       [2],
       [3],
       [4]])

In [83]: Y.reshape(len(Y),1)
Out[83]: 
array([[4],
       [3],
       [2],
       [1],
       [0]])

In [84]: np.hstack((X.reshape(len(X),1),Y.reshape(len(Y),1)))
Out[84]: 
array([[0, 4],
       [1, 3],
       [2, 2],
       [3, 1],
       [4, 0]])

Precautions for transpose of one-dimensional numpy array

X.transpose looks fine, but transposing a one-dimensional numpy array doesn't do anything. You can make a column vector by transposing after making it two-dimensional. transpose means transpose.

In [87]: X.transpose()
Out[87]: array([0, 1, 2, 3, 4])

In [88]: X.reshape(1,len(X))
Out[88]: array([[0, 1, 2, 3, 4]])

In [89]: X.reshape(1,len(X)).transpose()
Out[89]: 
array([[0],
       [1],
       [2],
       [3],
       [4]])

Combine images

Joining matrices can also be used to stick images together.

ipython


In [102]: from skimage import data

In [103]: from skimage import io

In [104]: image = data.lena()

In [105]: image.shape
Out[105]: (512, 512, 3)

In [106]: patch1 = image[200:200+32,200:200+32,:]

In [107]: patch2 = image[300:300+32,300:300+32,:]

In [108]: patch12 = np.hstack((patch1,patch2))

image 20160104_1.png

patch1 20160104_2.png

patch2 20160104_3.png

patch12 20160104_4.png

Recommended Posts

[Python] Numpy reference, extraction, combination
#Python basics (#Numpy 1/2)
#Python basics (#Numpy 2/2)
About python reference
Python #Numpy basics
Python reference page
[Python] Numpy memo
Python basics 8 numpy test
[Python] Search (NumPy) ABC165C
python numpy array calculation
[Python] Sorting Numpy data
Python Basic --Pandas, Numpy-
Python / Numpy np.newaxis thinking tips
Convert numpy int64 to python int
[Python] Calculation method with numpy
Implemented SMO with Python + NumPy
Matrix multiplication in python numpy
Keyword extraction by MeCab (python)
[Python] Random data extraction / combination from DataFrame using random and pandas
Python3 | Getting Started with numpy
Python # About reference and copy
[Python] Manipulate two-dimensional arrays at will. [Initialization / Reference / Extraction / Calculation / Transpose]
Subscript access to python numpy array
Introduction to Python Numerical Library NumPy
[Introduction to Python] <numpy ndarray> [edit: 2020/02/22]
Text extraction with AWS Textract (Python3.6)
Put python, numpy, opencv3 in ubuntu14
Self-organizing map in Python NumPy version
python datetime format quick reference table
[docker] python3.5 + numpy + matplotlib environment construction
Reputation of Python books and reference books