np.expand_dims(a, axis)--扩展array的形状,在指定的axis维度插入新的一维,输入a是array

例子:

>>>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)--移除array中维数为1的维度,axis可以指定移除维数为1的某个维度,返回移除这些维度之后的array

实例:

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

所以squeeze和expand_dims一对互逆的操作

相关文章:

  • 2021-11-05
  • 2021-07-05
  • 2021-06-23
  • 2021-05-18
  • 2021-09-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-05-31
  • 2022-12-23
  • 2021-08-03
  • 2022-12-23
  • 2021-09-27
  • 2022-01-09
相关资源
相似解决方案