好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

python中numpy.squeeze()函数的使用

         numpy.squeeze() 这个函数的作用是去掉矩阵里维度为1的维度。例如,(1, 5)的矩阵经由np.squeeze处理后变成5;(5, 1, 6)的矩阵经由np.squeeze处理后变成(5, 6)。

        numpy提供了numpy.squeeze(a, axis=None)函数,从数组的形状中删除单维条目。其中a表示输入的数组;axis用于指定需要删除的维度,但是指定的维度必须为单维度,否则将会报错。axis的取值可为None 或 int 或 tuple of ints,若axis为空时则删除所有单维度的条目。

examples1: >>> import numpy as np >>> a = np.array([[[0], [1], [2]]]) >>> a.shape (1, 3, 1) >>> np.squeeze(a).shape (3,) >>> np.squeeze(a, axis=(2,)).shape (1, 3) >>> np.squeeze(a, axis=(0,)).shape (3, 1)

examples2: >>> import numpy as np >>> a = np.arange(5).reshape(1,5) >>> a array([[0, 1, 2, 3, 4]]) >>> a.shape (1, 5) >>> b = np.squeeze(a) >>> b array([0, 1, 2, 3, 4]) >>> b.shape (5, )

examples3: >>> import numpy as np >>> a = np.arange(10).reshape(1,2,5) >>> a array([[[0, 1, 2, 3, 4],             [5, 6, 7, 8, 9]]]) >>> a.shape (1, 2, 5) >>> b = np.squeeze(a) >>> b array([[0, 1, 2, 3, 4],            [5, 6, 7, 8, 9]]) >>> b.shape (2, 5)

查看更多关于python中numpy.squeeze()函数的使用的详细内容...

  阅读:41次