Python 切片 3d numpy 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28010860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Slicing 3d numpy arrays
提问by Patrick Rinker
Consider the following:
考虑以下:
A = np.zeros((2,3))
print(A)
[[ 0. 0. 0.]
[ 0. 0. 0.]]
This make sense to me. I'm telling numpy to make a 2x3 matrix, and that's what I get.
这对我来说很有意义。我告诉 numpy 制作一个 2x3 矩阵,这就是我得到的。
However, the following:
但是,以下内容:
B = np.zeros((2, 3, 4))
print(B)
Gives me this:
给我这个:
[[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]
[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]]
This doesn't make sense to me. Aren't I telling numpy to make a cube which has 4 2x3 matrices? I'm even more confused because although the data structure looks incorrect, the slicing works exactly as planned:
这对我来说没有意义。我不是告诉 numpy 制作一个有 4 个 2x3 矩阵的立方体吗?我更加困惑,因为虽然数据结构看起来不正确,但切片完全按计划工作:
print(B[:,:,1])
[[ 0. 0. 0.]
[ 0. 0. 0.]]
I'm missing something about how these arrays are constructed, but I'm not sure what. Can someone explain what I'm missing or not understanding?
我缺少有关如何构造这些数组的信息,但我不确定是什么。有人可以解释我缺少或不理解的内容吗?
Thanks so much!
非常感谢!
回答by adrianX
B is a 3D matrix. the indices that you specified (2x3x4) is exactly what is printed out. the outermost brackets have 2 elements, the middle brackets have 3 elements, and the innermost brackets have 4 elements.
B 是一个 3D 矩阵。您指定的索引 (2x3x4) 正是打印出来的。最外面的括号有2个元素,中间的括号有3个元素,最里面的括号有4个元素。
回答by unutbu
NumPy arrays iterate over the left-most axis first. Thus if B
has shape
(2,3,4), then B[0]
has shape (3,4) and B[1]
has shape (3,4). In this sense,
you could think of B
as 2 arrays of shape (3,4). You can sort of see the two
arrays in the repr of B
:
NumPy 数组首先迭代最左边的轴。因此,如果B
具有形状 (2,3,4),则B[0]
具有形状 (3,4) 并B[1]
具有形状 (3,4)。从这个意义上讲,您可以将其B
视为 2 个形状为 (3,4) 的数组。您可以在以下的 repr 中看到两个数组B
:
In [233]: B = np.arange(2*3*4).reshape((2,3,4))
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7], <-- first (3,4) array
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19], <-- second (3,4) array
[20, 21, 22, 23]]])
You can also think of B
as containing four 2x3 arrays by iterating over the last index first:
您还可以B
通过首先遍历最后一个索引来将其视为包含四个 2x3 数组:
for i in range(4):
print(B[:,:,i])
# [[ 0 4 8]
# [12 16 20]]
# [[ 1 5 9]
# [13 17 21]]
# [[ 2 6 10]
# [14 18 22]]
# [[ 3 7 11]
# [15 19 23]]
but you could just as easily think of B
as three 2x4 arrays:
但是你可以很容易地想到B
三个 2x4 数组:
for i in range(3):
print(B[:,i,:])
# [[ 0 1 2 3]
# [12 13 14 15]]
# [[ 4 5 6 7]
# [16 17 18 19]]
# [[ 8 9 10 11]
# [20 21 22 23]]
NumPy arrays are completely flexible this way. But as far as the repr
of B
is concerned, what you seecorresponds to two (3x4) arrays since B
iterates over the left-most axis first.
NumPy 数组在这种方式下是完全灵活的。但就repr
ofB
而言,您看到的对应于两个 (3x4) 数组,因为首先B
遍历最左侧的轴。
for arr in B:
print(arr)
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
# [[12 13 14 15]
# [16 17 18 19]
# [20 21 22 23]]
回答by Atul
I hope the below example would clarify the second part of your question where you have asked about getting a 2X3 matrics when you type print(B[:,:,1])
我希望下面的例子能澄清你的问题的第二部分,你在输入时询问获得 2X3 矩阵 print(B[:,:,1])
import numpy as np
B = [[[1,2,3,4],
[5,6,7,8],
[9,10,11,12]],
[[13,14,15,16],
[17,18,19,20],
[21,22,23,24]]]
B = np.array(B)
print(B)
print()
print(B[:,:,1])
[[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
[[13 14 15 16]
[17 18 19 20]
[21 22 23 24]]]
[[ 2 6 10]
[14 18 22]]
Since the dimension of B is 2X3X4
, It means you have two matrices of size 3X4
as far as repr
of B is concerned
由于B的尺寸2X3X4
,这意味着你有大小两个矩阵3X4
至于repr
B的关注
Now in B[:,:,1]
we are passing :
, :
and 1
. First :
indicates that we are selecting both the 3X4
matrices. The second :
indicates that we are selecting all the rows from both the 3X4
matrices. The third parameter 1
indicates that we are selecting only the second column values of all the rows from both the 3X4
matrices. Hence we get
现在B[:,:,1]
我们正在通过:
,:
并且1
。First:
表示我们正在选择两个3X4
矩阵。第二个:
表示我们正在从两个3X4
矩阵中选择所有行。第三个参数1
表示我们只从两个3X4
矩阵中选择所有行的第二列值。因此我们得到
[[ 2 6 10]
[14 18 22]]