numpy.ma.squeeze#

ma.squeeze = <numpy.ma.core._convert2ma object>#

a 中删除长度为 1 的轴.

参数:
aarray_like

输入数据.

axisNone 或 int 或 int 元组,可选

选择形状中长度为1的条目的子集. 如果选择的轴的形状条目大于1,则会引发错误.

返回:
squeezedMaskedArray

输入数组,但已删除全部或部分长度为1的维度.这始终是 a 本身或 a 的视图. 请注意,如果所有轴都被压缩,则结果是0d数组而不是标量.

提出:
ValueError

如果 axis 不为None,并且要压缩的轴的长度不为1

参见

expand_dims

逆运算,添加长度为1的条目

reshape

插入,删除和组合维度,并调整现有维度的大小

示例

>>> import numpy as np
>>> 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