On 9/11/07, **Robert Kern** <robert.kern@gmail.com> wrote:

At the risk of making Robert grumpy: while it is true the form we ended up with is more general I've come to the conclusion that it was a bit of a mistake. In the spirit of making simple things simple and complex things possible, I suspect that having fancy-indexing do the obvious thing here[1] and delegating the more powerful but also more difficult to understand case to a function or method would have been overall more useful. Cases where the multidimensional features of fancy-indexing get used are messy enough that they don't benefit much from the conciseness of the indexing notation, at least in my experience.

--

. __Mike Ressler wrote:

> The following seems to be a wart: is it expected?

>

> Set up a 10x10 array and some indexing arrays:

>

> a=arange(100)

> a.shape=(10,10)

> q=array([0,2,4,6,8])

> r=array([0,5])

>

> Suppose I want to extract only the "even" numbered rows from a - then

>

> print a[q,:]

>

> <works - output deleted>

>

> Every fifth column:

>

> print a[:,r]

>

> <works - output deleted>

>

> Only the even rows of every fifth column:

>

> print a[q,r]

>

> ---------------------------------------------------------------------------

> <type 'exceptions.ValueError'> Traceback (most recent call last)

>

> /.../.../.../<ipython console> in <module>()

>

> <type 'exceptions.ValueError '>: shape mismatch: objects cannot be

> broadcast to a single shape

>

> But, this works:

>

> print a[q,:][:,r]

>

> [[ 0 5]

> [20 25]

> [40 45]

> [60 65]

> [80 85]]

>

> So why does the a[q,r] form have problems? Thanks for your insights.

It is intended that the form a[q,r] be the general case: q and r are broadcasted

against each other to a single shape. The result of the indexing is an array of

that broadcasted shape with elements found by using each pair of elements in the

broadcasted q and r arrays as indices.

There are operations you can express with this form that you couldn't if the

behavior that you expected were the case whereas you can get the result you want

relatively straightforwardly.

In [6]: a[q[:,newaxis], r]

Out[6]:

array([[ 0, 5],

[20, 25],

[40, 45],

[60, 65],

[80, 85]])

At the risk of making Robert grumpy: while it is true the form we ended up with is more general I've come to the conclusion that it was a bit of a mistake. In the spirit of making simple things simple and complex things possible, I suspect that having fancy-indexing do the obvious thing here[1] and delegating the more powerful but also more difficult to understand case to a function or method would have been overall more useful. Cases where the multidimensional features of fancy-indexing get used are messy enough that they don't benefit much from the conciseness of the indexing notation, at least in my experience.

--

. |-\

.

. tim.hochberg@ieee.org

[1] Just in case the 'obvious' thing isn't all that obvious: I mean restrict index-arrays to one dimension and have them simply select the given values along the axis. Hmmm. Without giving examples, which I have not time for right now, that's probably not any clearer than saying nothing. Ah well.