numpy.
squeeze
从中删除长度为1的轴 a .
输入数据。
1.7.0 新版功能.
选择形状中长度为1的条目的子集。如果选择的轴的形状输入大于1,则会引发错误。
输入数组,但删除了长度为1的所有或一个子集。总是这样 a 自身或视图 a . 请注意,如果压缩所有轴,则结果是0d数组而不是标量。
如果 axis 不是无,并且被挤压的轴的长度不是1
参见
expand_dims
逆操作,添加长度为1的条目
reshape
插入、删除和组合尺寸,并调整现有尺寸的大小
实例
>>> 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 () >>> np.squeeze(x)[()] 1234