numpy ix_ 用法及代码示例

用法:
numpy.ix_(*args)
从多个序列构造一个开放的网格。

此函数采用N个一维序列,并返回N个输出,每个输出具有N个维度,这样,除了一个维度外,其他所有形状均为1,并且具有非单位形状值的维度在所有N个维度中循环。

使用ix_可以快速构造索引数组以对叉积进行索引。a[np.ix_([1,3],[2,5])]返回数组[[a[1,2] a[1,5]], [a[3,2] a[3,5]]]。

描述
参数 args: : 1-D sequences

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

返回值 out: : ndarrays元组

N个数组,每个数组N个维,N个输入序列。这些阵列一起形成一个开放的网格。

例子:

>>> 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]])

发表回复

登录... 后才能评论