简体   繁体   English

从嵌套的 numpy 中获取/切片元素 arrays

[英]Get/Slice elements from numpy nested arrays

I am working with huge nested arrays which look something like this in structure:我正在使用巨大的嵌套 arrays,它在结构上看起来像这样:

import numpy as np


arr1 = np.array([[11, 12, 13],[21, 22, 23],[31, 32, 33]], dtype=complex)
arr2 = np.zeros([2, 2], dtype=object)

for i in range(2):
    for j in range(2):
        arr2[i,j] = arr1
        
arr2
array([[array([[11.+0.j, 12.+0.j, 13.+0.j],
               [21.+0.j, 22.+0.j, 23.+0.j],
               [31.+0.j, 32.+0.j, 33.+0.j]]),
        array([[11.+0.j, 12.+0.j, 13.+0.j],
               [21.+0.j, 22.+0.j, 23.+0.j],
               [31.+0.j, 32.+0.j, 33.+0.j]])],
       [array([[11.+0.j, 12.+0.j, 13.+0.j],
               [21.+0.j, 22.+0.j, 23.+0.j],
               [31.+0.j, 32.+0.j, 33.+0.j]]),
        array([[11.+0.j, 12.+0.j, 13.+0.j],
               [21.+0.j, 22.+0.j, 23.+0.j],
               [31.+0.j, 32.+0.j, 33.+0.j]])]], dtype=object)

Now I want to create another array which only holds for example the 11 value of each sub array without using a loop.现在我想创建另一个数组,例如只保存每个子数组的 11 值而不使用循环。 I know how to call the correct item.我知道如何调用正确的项目。 However, I cant figure out how to get the whole slice with every sub item.但是,我无法弄清楚如何获得每个子项的整个切片。 I thought I could use something like this to get every cell of the outer array and then specify the index of the inner array:我想我可以使用这样的东西来获取外部数组的每个单元格,然后指定内部数组的索引:

arr2[0, 0][0, 0]
(11+0j) 

arr2[::][0, 0]
array([[11.+0.j, 12.+0.j, 13.+0.j],
       [21.+0.j, 22.+0.j, 23.+0.j],
       [31.+0.j, 32.+0.j, 33.+0.j]])

What I wish to create is something like this with the 11 element from every sub array:我希望用每个子数组中的 11 元素创建类似这样的东西:

arr3 = array([[11+0j], [11+0j], [11+0j], [11+0j]])

I tried different approaches but could not get to my desired output. What am I doing wrong?我尝试了不同的方法,但无法到达我想要的 output。我做错了什么? Thanks in advance!提前致谢!

Note: I could not find any existing topic around here.注意:我在这里找不到任何现有主题。 Further, this is my very first post here.此外,这是我在这里的第一篇文章。 If I did something wrong, just tell me for I am happy to learn how to behave here;)如果我做错了什么,请告诉我,因为我很高兴在这里学习如何表现;)

Don't use dtype=object unless you 100% know what you're doing.不要使用dtype=object除非你 100% 知道你在做什么。 As a beginner it is almost always the wrong answer.作为初学者,它几乎总是错误的答案。

What you want is this:你想要的是这样的:

>>> arr1 = np.array([[11, 12, 13],[21, 22, 23],[31, 32, 33]], dtype=complex)
>>> arr2 = np.broadcast_to(arr1, (2, 2) + arr1.shape)
>>> arr2
array([[[[11.+0.j, 12.+0.j, 13.+0.j],
         [21.+0.j, 22.+0.j, 23.+0.j],
         [31.+0.j, 32.+0.j, 33.+0.j]],

        [[11.+0.j, 12.+0.j, 13.+0.j],
         [21.+0.j, 22.+0.j, 23.+0.j],
         [31.+0.j, 32.+0.j, 33.+0.j]]],


       [[[11.+0.j, 12.+0.j, 13.+0.j],
         [21.+0.j, 22.+0.j, 23.+0.j],
         [31.+0.j, 32.+0.j, 33.+0.j]],

        [[11.+0.j, 12.+0.j, 13.+0.j],
         [21.+0.j, 22.+0.j, 23.+0.j],
         [31.+0.j, 32.+0.j, 33.+0.j]]]])

>>> arr2[:,:,0,0]
array([[11.+0.j, 11.+0.j],
       [11.+0.j, 11.+0.j]])

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

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