Numpy 3D数组在单步索引和两步索引时转换 [英] Numpy 3D array transposed when indexed in single step vs two steps

查看:129
本文介绍了Numpy 3D数组在单步索引和两步索引时转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import numpy as np
x = np.random.randn(2, 3, 4)
mask = np.array([1, 0, 1, 0], dtype=np.bool)
y = x[0, :, mask]
z = x[0, :, :][:, mask]
print(y)
print(z)
print(y.T)

为什么要进行上述操作两个步骤导致在一个步骤中进行转置?

Why does doing the above operation in two steps result in the transpose of doing it in one step?

推荐答案

以下是与列表索引相同的行为:

Here's the same behavior with a list index:

In [87]: x=np.arange(2*3*4).reshape(2,3,4)
In [88]: x[0,:,[0,2]]
Out[88]: 
array([[ 0,  4,  8],
       [ 2,  6, 10]])
In [89]: x[0,:,:][:,[0,2]]
Out[89]: 
array([[ 0,  2],
       [ 4,  6],
       [ 8, 10]])

在第二种情况下, x [0,:,]] 返回一个(3,4)数组,下一个索引选择2列。

In the 2nd case, x[0,:,:] returns a (3,4) array, and the next index picks 2 columns.

在第一种情况下,它首先选择第一个和最后一个维度,然后附加切片(中间维度)。 0 [0,2] 产生 2 尺寸,并从中间附加 3 ,给出(2,3)形状。

In the 1st case, it first selects on the first and last dimensions, and appends the slice (the middle dimension). The 0 and [0,2] produce a 2 dimension, and the 3 from the middle is appended, giving (2,3) shape.

这是混合基本和高级索引的情况。

This is a case of mixed basic and advanced indexing.

http://docs.scipy.org/doc/numpy/reference/arrays.indexing。 html#combined-advanced-and-basic-indexing


在第一种情况下,高级索引操作产生的维度在结果数组中排在第一位,之后是子空间维。

In the first case, the dimensions resulting from the advanced indexing operation come first in the result array, and the subspace dimensions after that.

这不是一个容易理解或解释的案例。基本上对最终维度应该是什么有一些模糊性。它试图用一个例子说明 x [:,ind_1,:,ind_2] 其中 ind_1 ind_2 是3d(或者一起广播)。

This is not an easy case to comprehend or explain. Basically there's some ambiguity as to what the final dimension should be. It tries to illustrate with an example x[:,ind_1,:,ind_2] where ind_1 and ind_2 are 3d (or together broadcast to that).

之前的解释是:

numpy order数组切片索引如何?

为多维numpy数组组合切片和广播索引

============ ===============

===========================

解决这个问题的方法是用数组替换切片 - 列向量

A way around this problem is to replace the slice with an array - a column vector

In [221]: x[0,np.array([0,1,2])[:,None],[0,2]]
Out[221]: 
array([[ 0,  2],
       [ 4,  6],
       [ 8, 10]])
In [222]: np.ix_([0],[0,1,2],[0,2])
Out[222]: 
(array([[[0]]]), array([[[0],
         [1],
         [2]]]), array([[[0, 2]]]))
In [223]: x[np.ix_([0],[0,1,2],[0,2])]
Out[223]: 
array([[[ 0,  2],
        [ 4,  6],
        [ 8, 10]]])

虽然最后一种情况是3d,(1,3,2)。 ix _ 不喜欢标量0.使用的另一种方式ix _

Though this last case is 3d, (1,3,2). ix_ didn't like the scalar 0. An alternate way of using ix_:

In [224]: i,j=np.ix_([0,1,2],[0,2])
In [225]: x[0,i,j]
Out[225]: 
array([[ 0,  2],
       [ 4,  6],
       [ 8, 10]])

这是一种获得相同数字的方法,但是在(2,1, 3)数组:

And here's a way of getting the same numbers, but in a (2,1,3) array:

In [232]: i,j=np.ix_([0,2],[0])
In [233]: x[j,:,i]
Out[233]: 
array([[[ 0,  4,  8]],

       [[ 2,  6, 10]]])

这篇关于Numpy 3D数组在单步索引和两步索引时转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆