7

I am doing some simulations in experimental cosmology, and encountered this problem while working with numpy arrays. I'm new to numpy, so I am not sure if I'm doing this wrong or if it's a bug. I run:

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
>>> 

I expected it to return:

array([0, 1, 2, 3, 4])

since t[t < 8] should presumably be treated as just another ndarray?

The numpy documentation (http://docs.scipy.org/doc/numpy/user/basics.indexing.html) says about boolean arrays as indices:

As with index arrays, what is returned is a copy of the data, not a view as one gets with slices.

running type(t[t < 8]) also gives ndarray, which I guess should have all the properties of a numpy array. Should I perhaps do this better with list expressions? I have not done a timed comparison yet, but I would imagine this to be a problem for large 2D arrays.

4

2 に答える 2

7

t[ t < 8 ]確かに配列が得られますが、開始時と同じサイズの配列は得られません。 t < 8と同じ形状のブール配列を返しtます。それを使用して index を使用すると、ブール配列が短い配列を残すt要素のみを引き出します。Trueそれをもう一度行うと:

result = t[t<8]
result[t<5]

t次に、ブール値のインデックス配列は再び同じ形状になりますが、それを使用して小さな配列にインデックスを付けているため、エラーが発生します。

ドキュメントは完全に正しいです。新しい配列は元の配列ではありませんview...データのコピーですが、新しい配列が元の配列と同じ形状またはサイズであることを意味するものではありません。

于 2013-04-17T22:38:54.870 に答える
1

これは意図されたものです。't' への参照は、2 番目のブール ステートメントに到達するまでには意味がありません。最初のステートメントでは、t を 8 未満の値でセグメント化しています。2 番目のステートメントでは、引き続き 't' をセグメント化していますが、一時的な配列 ('s' と呼びます) を使用しています。's' のインデックス リクエストは、常に 't' に正しくマップできるとは限りません。したがって、例外がスローされます。

複数のブールステートメントを実行したい場合。それらを組み合わせて、次のようにします。

s = t[t < 8]
s[s < 5]

または代わりに @mgilson から:

t[np.logical_and(t < 8, t < 5)]
于 2013-04-17T22:39:20.520 に答える