简体   繁体   English

多维数组上的 numpy 高级索引

[英]numpy advanced indexing on multidimensional-array

Say x is a 3x3 numpy array that contains the following:假设x是一个 3x3 numpy 数组,其中包含以下内容:

import numpy as np
x = np.array([[ 1.,  2.,  3.],
              [ 4.,  5.,  6.],
              [ 7.,  8.,  9.]])

is there some indexing that can give me the following subarray:是否有一些索引可以给我以下子数组:

array([[ 1.,  2.],
       [ 5.,  6.]])

You can use integer array indexing with a tuple of arrays:您可以对数组元组使用整数数组索引

>>> rows = np.array([[0, 0],
...                  [1, 1]], dtype=np.intp)
>>> columns = np.array([[0, 1],
...                     [1, 2]], dtype=np.intp)
>>> x[rows, columns]
array([[ 1.,  2.],
       [ 5.,  6.]])

you can use您可以使用

x[:2,:2]

to solve your problem解决你的问题

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

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