布尔索引但结果却是其他一些操作 [英] Boolean Indexing but turns out to be some other operation

查看:82
本文介绍了布尔索引但结果却是其他一些操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试进行布尔索引 但是..

I was trying to do boolean indexing but..

np.random.randn(8).reshape((4,2))
Out[11]: 
array([[-1.13058416,  1.08397186],
       [-1.2730122 ,  0.78306498],
       [-0.05370502, -1.16723298],
       [ 1.01750955, -0.95029671]])

a=np.random.randn(8).reshape((4,2))

a[[2==3,3==0,0==0,1!=1]]
Out[13]: 
array([[ 0.18235299, -2.53482367],
       [ 0.18235299, -2.53482367],
       [-1.03752809, -2.2790847 ],
       [ 0.18235299, -2.53482367]])

发生了什么事? 我当时想的更像是布尔索引.这是什么操作? 我不要求将其更正为Bool索引.相反,我问这个手术正在发生什么?这是合法的吗?

What just happened? I was thinking more like Bool indexing. What operation is this? I am not asking to correct this to Bool indexing. Rather, Im asking what is happening in this operation? Is this legit?

推荐答案

很容易将ndarray视为已增强的list.广播和对数组的操作会自动扩展到这些操作所涉及的列表,因此您可以添加一个数组和一个与广播兼容的形状的列表,并且numpy不会尝试将两者串联(因为它会尝试对两个列表进行处理) ).

It's easy to think of ndarrays as buffed-up lists. Broadcasting and operations on arrays are automatically extended to lists involved in those operations, so you can add an array and a list of broadcast-compatible shapes, and numpy won't try to concatenate the two (as it would try to do with two lists).

One huge (and, for me, confusing) exception is fancy indexing. Fancy indexing itself is already confusing to me (as someone coming from MATLAB), since it's odd that the following two give a different result:

import numpy as np
A = np.random.rand(3,3)
A[0:1,0:1]
A[range(2),range(2)]

前者是切片操作,并返回2 x 2子矩阵.后者是花式索引的一种情况,它仅返回包含2个元素的数组,其中包含A[0,0]A[1,1].

The former is a slicing operation, and returns a 2-by-2 submatrix. The latter is a case of fancy indexing, and returns only a 2-element array, containing A[0,0] and A[1,1].

您的问题与同样奇怪的事情有关:在花式索引中使用的 boolean 值的列表和数组的行为不同.根据您的问题考虑以下两个示例:

Your question is related to something equally odd: lists and arrays of boolean values behave differently when used in fancy indexing. Consider the following two examples, along the lines of your question:

A = np.random.rand(4,2)
bool_index_list = [False, True, True, False]
bool_index_array = np.array(bool_index_list)
A[bool_index_list].shape
A[bool_index_array].shape

前者返回(4,2),后者返回(2,2).

在前一种情况下,由于索引是list,因此布尔值将转换为相应的整数,并且[0,1,1,0]的结果值将用作矩阵中的实际索引,并返回[first,second,第二,第一]行.

In the former case, since the index is a list, the boolean values are converted to corresponding integers, and the resulting values of [0,1,1,0] are used as actual indices in the matrix, returning the [first,second,second,first] row, respectively.

在后一种情况下,如您期望的那样使用dtype=bool的索引array:它用作掩码,以忽略索引为FalseA行.

In the latter case, the index array of dtype=bool is used as you would expect it to be: it is used as a mask to ignore those rows of A for which the index is False.

numpy发行说明表示

将来,类似布尔数组的对象(例如python bool列表)将始终被视为布尔索引,而布尔标量(包括python True)将成为合法的 boolean 索引.

In the future Boolean array-likes (such as lists of python bools) will always be treated as Boolean indexes and Boolean scalars (including python True) will be a legal boolean index.

相应地,以上基于列表的索引案例在numpy 1.10.1中给了我以下警告:

Correspondingly, the list-based indexing cases above give me the following warning in numpy 1.10.1:

FutureWarning:将来,类似布尔数组的对象将作为布尔数组索引处理

FutureWarning: in the future, boolean array-likes will be handled as a boolean array index

因此,对您的问题的简短回答是,这是合法的,但时间不长.坚持使用基于ndarray的花式索引,在此过程中您应该不会遇到麻烦.

So the short answer to your question is that it's legit, but not for long. Stick to ndarray-based fancy indexing, and you should experience no bumps along the way.

这篇关于布尔索引但结果却是其他一些操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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