numpy change ndarray dimension of some operations

np.expand_dims(a, axis)- expanded shape of the array, a new dimension in the insertion axis dimension designated input array is a

example:

>>>x = np.array([1,2])
>>>x.shape
(2,)
>>>y = np.expand_dims(x, axis=0)
>>>y
array([[1,2]])
>>>y.shape
(1,2)
>>>y = np.expand_dims(x, axis=1)
>>>y
array([[1],
       [2]])
>>>y.shape
(2,1)

numpy.squeeze(a, axis=None)- Remove array in dimension is the dimension 1, axis can be specified to remove dimension is a dimension 1, the array returned after the removal of these dimensions

Example:

>>>x = np.array([[[0], [1], [2]]]
>>>x.shape
(1, 3, 1)
>>>np.squeeze(x).shape
(3,)
>>>np.squeeze(x, axis=0).shape
(3,1)

Therefore, one pair of squeeze and expand_dims reciprocal operation

Guess you like

Origin www.cnblogs.com/patrolli/p/11992198.html