简体   繁体   English

切片3d numpy数组

[英]Slicing 3d numpy arrays

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? 难道我不是要制作一个有4个2x3矩阵的立方体吗? I'm even more confused because although the data structure looks incorrect, the slicing works exactly as planned: 我更加困惑,因为虽然数据结构看起来不正确,但切片的工作原理与计划完全一致:

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! 非常感谢!

NumPy arrays iterate over the left-most axis first. NumPy数组首先迭代最左边的轴。 Thus if B has shape (2,3,4), then B[0] has shape (3,4) and B[1] has shape (3,4). 因此,如果B具有形状(2,3,4),则B[0]具有形状(3,4)并且B[1]具有形状(3,4)。 In this sense, you could think of B as 2 arrays of shape (3,4). 从这个意义上讲,你可以将B视为2个形状阵列(3,4)。 You can sort of see the two arrays in the repr of B : 您可以在B的repr中看到两个数组:

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. NumPy数组以这种方式非常灵活。 But as far as the repr of B is concerned, what you see corresponds to two (3x4) arrays since B iterates over the left-most axis first. 但至于reprB而言,你所看到的对应于两个(3×4)阵列,因为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]]

B is a 3D matrix. B是3D矩阵。 the indices that you specified (2x3x4) is exactly what is printed out. 您指定的索引(2x3x4)正是打印出来的。 the outermost brackets have 2 elements, the middle brackets have 3 elements, and the innermost brackets have 4 elements. 最外面的括号有2个元素,中间括号有3个元素,最里面的括号有4个元素。

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]) 我希望下面的例子能够澄清你的问题的第二部分,当你输入print(B[:,:,1])时,你已经询问过如何获得2X3 matrics

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 . 现在在B[:,:,1]我们传递::1 First : indicates that we are selecting both the 3X4 matrices . 第一:表示我们正在选择3X4矩阵 The second : indicates that we are selecting all the rows from both the 3X4 matrices . 第二个:表示我们正在3X4矩阵中选择所有行 The third parameter 1 indicates that we are selecting only the second column values of all the rows from both the 3X4 matrices . 第三个参数1表示我们只选择3X4矩阵中所有行的第二列值 Hence we get 因此,我们得到

[[ 2  6 10]
 [14 18 22]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM