numpy.ma.squeeze#

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

a 中移除长度为 1 的轴.

参数:
aarray_like

输入数据.

None 或 int 或 int 元组,可选

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

返回:
squeezedMaskedArray

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

Raises:
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