numpy.ix_#

numpy.ix_(*args)[源代码]#

从多个序列构建一个开放网格.

此函数接受 N 个 1-D 序列并返回 N 个输出,每个输出有 N 个维度,使得形状在除一个维度外的所有维度中为 1,并且非单位形状值的维度循环通过所有 N 个维度.

使用 ix_ 可以快速构建索引数组,这些数组将索引笛卡尔积. a[np.ix_([1,3],[2,5])] 返回数组 [[a[1,2] a[1,5]], [a[3,2] a[3,5]]].

参数:
args1-D 序列

每个序列应该是整数或布尔类型.布尔序列将被解释为相应维度的布尔掩码(等同于传入 np.nonzero(boolean_sequence)).

返回:
outndarrays 的元组

每个具有 N 维的 N 个数组,其中 N 是输入序列的数量.这些数组共同形成一个开放网格.

参见

ogrid, mgrid, meshgrid

示例

>>> import numpy as np
>>> a = np.arange(10).reshape(2, 5)
>>> a
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])
>>> ixgrid = np.ix_([0, 1], [2, 4])
>>> ixgrid
(array([[0],
       [1]]), array([[2, 4]]))
>>> ixgrid[0].shape, ixgrid[1].shape
((2, 1), (1, 2))
>>> a[ixgrid]
array([[2, 4],
       [7, 9]])
>>> ixgrid = np.ix_([True, True], [2, 4])
>>> a[ixgrid]
array([[2, 4],
       [7, 9]])
>>> ixgrid = np.ix_([True, True], [False, False, True, False, True])
>>> a[ixgrid]
array([[2, 4],
       [7, 9]])