我正在进行一些实验宇宙学的模拟,并在使用numpy数组时遇到了这个问题。我是numpy的新手,所以不确定我是做错了还是这是一个bug。我运行了:
Enthought Python Distribution -- www.enthought.com
Version: 7.3-1 (32-bit)
Python 2.7.3 |EPD 7.3-1 (32-bit)| (default, Apr 12 2012, 11:28:34)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "credits", "demo" or "enthought" for more information.
>>> import numpy as np
>>> t = np.arange(10)
>>> t[t < 8][t < 5]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many boolean indices
>>>
我期望它返回:
array([0, 1, 2, 3, 4])
因为t[t < 8]应该被视为另一个ndarray?
关于布尔数组作为索引,numpy文档(http://docs.scipy.org/doc/numpy/user/basics.indexing.html)说:
与索引数组一样,返回的是数据的副本,而不是像切片一样返回视图。
运行type(t[t < 8])
也会给出ndarray
,我想这应该具有numpy数组的所有属性。也许我应该用列表表达式来更好地完成这个任务吗?我还没有进行定时比较,但我想对于大型2D数组来说,这可能是一个问题。
t[t < 8][t < 5]
会返回什么? - Bi Ricot[0:5][3:5]
这样的操作。这与我最初对副本/视图的想法完全相反。感谢 @mgilson 的观察。 - plan