了解numpy中奇怪的布尔二维数组索引行为 [英] Understanding weird boolean 2d-array indexing behavior in numpy

查看:151
本文介绍了了解numpy中奇怪的布尔二维数组索引行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这样做:

a=np.random.rand(10,20)
x_range=np.arange(10)
y_range=np.arange(20)

a_tmp=a[x_range<5,:]
b=a_tmp[:,np.in1d(y_range,[3,4,8])]

而这不是:

a=np.random.rand(10,20)
x_range=np.arange(10)
y_range=np.arange(20)    

b=a[x_range<5,np.in1d(y_range,[3,4,8])]

推荐答案

Numpy参考文档的索引页面包含答案,但需要仔细阅读.

The Numpy reference documentation's page on indexing contains the answers, but requires a bit of careful reading.

此处的答案是,使用布尔值进行索引等效于使用首先通过np.nonzero转换布尔值数组而获得的整数数组进行索引.因此,对于布尔数组m1m2

The answer here is that indexing with booleans is equivalent to indexing with integer arrays obtained by first transforming the boolean arrays with np.nonzero. Therefore, with boolean arrays m1, m2

a[m1, m2] == a[m1.nonzero(), m2.nonzero()]

(成功时,即m1.nonzero().shape == m2.nonzero().shape)等效于:

which (when it succeeds, i.e., m1.nonzero().shape == m2.nonzero().shape) is equivalent to:

[a[i, i] for i in range(a.shape[0]) if m1[i] and m2[i]]

我不确定为什么要设计成这样工作---通常,不是是您想要的.

I'm not sure why it was designed to work like this --- usually, this is not what you'd want.

要获得更直观的结果,您可以代替

To get the more intuitive result, you can instead do

a[np.ix_(m1, m2)]

产生的结果等同于

[[a[i,j] for j in range(a.shape[1]) if m2[j]] for i in range(a.shape[0]) if m1[i]]

这篇关于了解numpy中奇怪的布尔二维数组索引行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆