numpy.squeeze() 这个函数的作用是去掉矩阵里维度为1的维度
numpy提供了numpy.squeeze(a, axis=None)函数,从数组的形状中删除单维条目。其中a表示输入的数组;axis用于指定需要删除的维度,但是指定的维度必须为单维度,否则将会报错。
Examples
--------
>>> x = np.array([[[0], [1], [2]]])
>>> x.shape
(1, 3, 1)
>>> np.squeeze(x).shape
(3,)
>>> np.squeeze(x, axis=0).shape
(3, 1)
>>> np.squeeze(x, axis=1).shape
Traceback (most recent call last):
...
ValueError: cannot select an axis to squeeze out which has size not equal to one
>>> np.squeeze(x, axis=2).shape
(1, 3)
>>> x = np.array([[1234]])
>>> x.shape
(1, 1)
>>> np.squeeze(x)
array(1234) # 0d array
>>> np.squeeze(x).shape
()
数组形状:
a=np.array([[[1,2],[1,2],[2,3]]]).shape
print(a)
(1, 3, 2)