Explain in detail the method of Numpy expanding matrix dimensions of np.expand_dims np. newaxis and deleting dimensions of np. squeeze

  • 2021-10-13 08:06:44
  • OfStack

When operating the matrix, different interfaces have different requirements for the input dimension of the matrix, and the input may be 1-D, 2-D, 3-D and so on. The following describes the related methods of changing matrix dimensions using Numpy under 1. Mainly includes the following:

1. np. newaxis Extended Matrix Dimension

2. np.expand_dims Extended Matrix Dimension

3. np. squeeze Delete the dimension with dimension size 1 in the matrix

np.newaxis, np.expand_dims Extended Matrix Dimensions:


import numpy as np
 
x = np.arange(8).reshape(2, 4)
print(x.shape)
 
#  Add Parts 0 Dimension , Output shape -> (1, 2, 4)
x1 = x[np.newaxis, :]
print(x1.shape)
 
#  Add Parts 1 Dimension ,  Output shape -> (2, 1, 4)
x2 = np.expand_dims(x, axis=1)
print(x2.shape)

Output:

(2, 4)
(1, 2, 4)
(2, 1, 4)

np. squeeze Reducing Matrix Dimensions:


"""
 squeeze  Function: Deletes a single-dimensional entry from the shape of the array, that is, puts the shape Middle is 1 The dimension of is removed 
  Usage: numpy.squeeze(a,axis = None)
  1 ) a An array representing input; 
  2 ) axis Used to specify the dimension to be deleted, but the specified dimension must be a single dimension, otherwise an error will be reported; 
  3 ) axis The value of can be None  Or  int  Or  tuple of ints,  Optional. If axis If it is empty, all single-dimensional entries are deleted; 
  4 ) Return value: Array 
  5)  The original array will not be modified; 
"""
import numpy as np 
print("#" * 40, " Raw data ", "#" * 40)
x = np.arange(10).reshape(1, 1, 10, 1)
print(x.shape)
print(x)
 
print("#" * 40, " Remove axis=0 This dimension ", "#" * 40)
x_squeeze_0 = np.squeeze(x, axis=0)
print(x_squeeze_0.shape, x_squeeze_0)
 
print("#" * 40, " Remove axis=3 This dimension ", "#" * 40)
x_squeeze_3 = np.squeeze(x, axis=3)
print(x_squeeze_3.shape, x_squeeze_3)
 
print("#" * 40, " Remove axis=0 ,  axis=1 These two dimensions ", "#" * 40)
x_squeeze_0_1 = np.squeeze(x, axis=(0, 1))
print(x_squeeze_0_1.shape, x_squeeze_0_1)
 
print("#" * 40, " Remove all 1 Dimension of dimension ", "#" * 40)
x_squeeze = np.squeeze(x)
print(x_squeeze.shape, x_squeeze)
 
print("#" * 40, " Remove is not 1 Dimension of dimension, throw exception ", "#" * 40)
try:
 x_squeeze = np.squeeze(x, axis=2)
 print(x_squeeze.shape, x_squeeze)
except Exception as e:
 print(e)

Output:

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
(1, 1, 10, 1)
[[[[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]]]]
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
(1, 10, 1) [[[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]]]
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
(1, 1, 10) [[[0 1 2 3 4 5 6 7 8 9]]]
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
(10, 1) [[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]]
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
(10,) [0 1 2 3 4 5 6 7 8 9]
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
cannot select an axis to squeeze out which has size not equal to one

Reference link


Related articles: