Numpy knowledge: np.expand_dims () & np.argmax ()

np.expand_dims: an array shape for extended

The original array:

import numpy as np

In [12]:
a = np.array([[[1,2,3],[4,5,6]]])
a.shape
Out[12]:
(1, 2, 3)

np.expand_dims (a, axis = 0) indicates the position of 0 data is added, the conversion result is as follows:

In [13]:
b = np.expand_dims(a, axis=0)
b
Out[13]:
array([[[[1, 2, 3],
         [4, 5, 6]]]])

In [14]:
b.shape
Out[14]:
(1, 1, 2, 3)

np.expand_dims (a, axis = 1) represents the position of data is added, the conversion result is as follows:

In [15]:
c = np.expand_dims(a, axis=1)
c
Out[15]:
array([[[[1, 2, 3],
         [4, 5, 6]]]])

In [16]:
c.shape
Out[16]:
(1, 1, 2, 3)

np.expand_dims (a, axis = 2) adding data representing the position 2, the conversion result is as follows:

In [17]:
d = np.expand_dims(a, axis=2)
d
Out[17]:
array([[[[1, 2, 3]],
        [[4, 5, 6]]]])

In [18]:
d.shape
Out[18]:
(1, 2, 1, 3)

np.expand_dims (a, axis = 3) adding data representing the position 3, the conversion result is as follows:

In [19]:
e = np.expand_dims(a, axis=3)
e

In [20]:
e.shape
Out[20]:
(1, 2, 3, 1)

Can be inserted in the (1,2,3) position for a total of four, and then add the following warning will appear, or else will prompt AxisError at some later point.

In [21]:
f = np.expand_dims(a, axis=4)
D:\ProgramData\Anaconda3\envs\dlnd\lib\site-packages\ipykernel_launcher.py:1: DeprecationWarning: Both axis > a.ndim and axis < -a.ndim - 1 are deprecated and will raise an AxisError in the future.
  """Entry point for launching an IPython kernel.

np.argmax: Returns the index of the maximum value along the axis

  • One-dimensional array
A = np.arange(6).reshape(1,6)
A
Out[22]:
array([[0, 1, 2, 3, 4, 5]])

Returns the index of the maximum value of the one-dimensional array:

In [23]:
B = np.argmax(A)
B
Out[23]:
6
  • Two-dimensional array

To index the array:

In [42]:
E = np.array([[1,2,3],[2,1,4],[3,6,1]])
print(E)
E.shape
[[1 2 3]
 [2 1 4]
 [3 6 1]]
Out[42]:
(3, 3)

axis = 0 indicates the row, and outputs the results as shown below, comparing a [0] [j], a [1] [j], a [2] [j] (j = 0,1,2), from a [0] [j] traversing maximum index initially (0,0,0), a [1] [j] as compared to a [0] [j], 2 than 1, 1 to 2 hours, 4 greater than 3, updates the value of the maximum value of the index (1,0,1); a [2] [j] as compared to a [1] [j], 3 is greater than 2, 2 is larger than 6, a ratio of 4 hours, the maximum value of the index is updated to (2,2,1)

In [34]:
F= np.argmax(E, axis=0)
F
Out[34]:
array([2, 2, 1], dtype=int64)

axis = 1 indicates in the column, and outputs the results as shown below, comparing a [i] [0], a [i] [1], a [i] [2] (i = 0,1,2), from a [i] [0] traversing maximum index initially (0,0,0), a [i] [1] and a [i] [0] as compared to 2 larger than 1, 1 to 2 hours, 4 greater than 3, updates the value of the maximum value of the index (1,0,1); a [i] [2] and a [i] [1] compared to 3 is greater than 2, 2 is larger than 6, a ratio of 4 hours, the maximum value of the index is updated to (2,2,1)

In [41]:
G= np.argmax(E, axis=1)
G
Out[41]:
array([2, 2, 1], dtype=int64)

Three-dimensional four-dimensional multidimensional is the same reason -

 

Published 352 original articles · won praise 115 · views 130 000 +

Guess you like

Origin blog.csdn.net/Aidam_Bo/article/details/103245520