[英]numpy 3 dimension array middle indexing bug
I seems found a bug when I'm using python 2.7 with numpy module: 当我使用带有numpy模块的python 2.7时,我似乎发现了一个错误:
import numpy as np
x=np.arange(3*4*5).reshape(3,4,5)
x
Here I got the full 'x' array as follows: 在这里,我得到了完整的'x'数组,如下所示:
array([[[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]],
[[20, 21, 22, 23, 24],
[25, 26, 27, 28, 29],
[30, 31, 32, 33, 34],
[35, 36, 37, 38, 39]],
[[40, 41, 42, 43, 44],
[45, 46, 47, 48, 49],
[50, 51, 52, 53, 54],
[55, 56, 57, 58, 59]]])
Then I try to indexing single row values in sheet [1]: 然后我尝试索引sheet [1]中的单行值:
x[1][0][:]
Result: 结果:
array([20, 21, 22, 23, 24])
But something wrong while I was try to indexing single column in sheet [1]: 但在我尝试索引工作表[1]中的单个列时出了点问题:
x[1][:][0]
Result still be the same as previous: 结果仍与上一个相同:
array([20, 21, 22, 23, 24])
Should it be array([20, 25, 30, 35])?? 它应该是数组([20,25,30,35])??
It seems something wrong while indexing the middle index with range? 使用范围索引中间索引似乎有些不对劲?
No, it's not a bug. 不,这不是一个错误。
When you use [:]
you are using slicing notation and it takes all the list: 当你使用
[:]
你使用切片表示法,它占用了所有列表:
l = ["a", "b", "c"]
l[:]
#output:
["a", "b", "c"]
and in your case: 在你的情况下:
x[1][:]
#output:
array([[20, 21, 22, 23, 24],
[25, 26, 27, 28, 29],
[30, 31, 32, 33, 34],
[35, 36, 37, 38, 39]])
What you realy wish is using numpy indexing
notation: 你真正想要的是使用numpy
indexing
表示法:
x[1, : ,0]
#output:
array([20, 25, 30, 35])
This is not a bug. 这不是一个错误。
x[1][:][0]
is not a multiple index ("give me the elements where first dimension is 1, second is any, third is 0"). x[1][:][0]
不是多重索引(“给我第一个维度为1,第二个为任意,第三个为0”的元素)。 Instead, you are indexing three times, three objects. 相反,您正在索引三次,三个对象。
x1 = x[1] # x1 is the first 4x5 subarray
x2 = x1[:] # x2 is same as x1
x3 = x2[0] # x3 is the first row of x2
To use multiple index, you want to do it in a single slice: 要使用多个索引,您希望在单个切片中执行此操作:
x[1, :, 0]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.