简体   繁体   English

从3D numpy数组中提取2D

[英]Extracting 2 D from 3D numpy array

I have two 3D arrays of the form (1000, 1000, 20). 我有两个格式为(1000、1000、20)的3D数组。 The last dimension, 13, is an index through time stamps. 最后一个维度13是时间戳的索引。 I want to step through the arrays by time stamps and compare arrays. 我想通过时间戳逐步遍历数组并比较数组。 Suppose I have A (1000, 1000, 20) and B(1000, 1000, 20). 假设我有A(1000,1000,20)和B(1000,1000,20)。

I want something like 我想要类似的东西

for t in range(0,21):   
     asub = A[,,t]  
     bsub = B[,,t]  
     #compare asub and bsub

However, that syntax does not seem to work. 但是,该语法似乎不起作用。 how can I do this? 我怎样才能做到这一点?

From the documentation , you need to "Combine advanced and basic indexing" . documentation ,您需要“合并高级索引和基本索引”

So advanced indexing involves indexing specific axis of an array . 因此, 高级 indexing涉及对array特定axis进行indexing

For example, taking the elements from index 1 onwards: 例如,从index 1开始获取elements

>>> a = np.array([[1,2,3], [4,5,6],[7,8,9]])
>>> a[1:, 1:]
array([[5, 6],
       [8, 9]])

So if you want to get the element from the third axis at index t , you need to select all the elements from the other axis with just a regular colon ( : ) and then specify t for the last index : 所以,如果你想获得的element从第三axisindex t ,你需要选择的所有elements与其它axis与只是一个普通的冒号( : ),然后指定t最后index

So you want to do: 所以你想做:

A[:, :, t] 

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

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