The difference and connection functions and np.newaxis of np.squeeze

np.squeeze () function
syntax: numpy.squeeze (A, Axis = None)

 1) A represents an array of input;
 2) Axis for the specified dimensions need to be deleted, but the dimensions must be specified as a single dimension, otherwise it will error ;
 . 3) may be a value of None Axis or int or tuple of ints, optional. If the axis is empty, all entries in a single dimension is deleted;
 4) Return value: Array
 5) does not modify the original array;

action: Remove entries from the single dimension of an array shape, i.e. the shape is removed as a dimension of 1

np.newaxis function
role: to add a one-dimensional in this position, this position refers to the location where np.newaxis

Example 1:

x1 = np.array([1, 2, 3, 4, 5]) # the shape of x1 is (5,) x1_new = x1[:, np.newaxis] # now, the shape of x1_new is (5, 1) # array([[1], # [2], # [3], # [4], # [5]]) x1_new = x1[np.newaxis,:] # now, the shape of x1_new is (1, 5) # array([[1, 2, 3, 4, 5]])
例子二:
In [124]: arr = np.arange(5*5).reshape(5,5)

In [125]: arr.shape
Out[125]: (5, 5)

# promoting 2D array to a 5D array
In [126]: arr_5D = arr[np.newaxis, ..., np.newaxis, np.newaxis]

In [127]: arr_5D.shape
Out[127]: (1, 5, 5, 1, 1)
 

Guess you like

Origin www.cnblogs.com/dtpromise/p/11788986.html