numpy.squeeze
numpy.squeeze(a, axis=None) [source]
从数组shape中删除一维条目。
参数 : | a :array_like 输入数据。 axis : 1.7.0版中的新功能。 选择形状中一维条目的子集。 如果选择的形状输入大于一个的轴,则会引发错误。 |
返回值 : | squeezed :ndarray 输入数组,但删除了长度为1的全部或部分维度。 可能始终是其本身或a的视图。 请注意,如果所有轴都受到squeeze,则结果为0d数组,而不是标量。 |
Raises : | ValueError 如果axis不为 |
例子
>>> 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