From: Bill B. <wb...@gm...> - 2006-05-18 14:56:41
|
One thing I haven't quite managed to figure out yet, is what the heck indexing an array with an array is supposed to give you. This is sort of an offshoot of the "nonzero()" discussion. I was wondering why nonzero() and where() return tuples at all instead of just an array, till I tried it. >>> b array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) >>> b[ num.asarray(num.where(b>4)) ] array([[[3, 4, 5], [6, 7, 8], [6, 7, 8], [6, 7, 8]], [[6, 7, 8], [0, 1, 2], [3, 4, 5], [6, 7, 8]]]) Whoa, not sure what that is. Can someone explain the current rule and in what situations is it useful? Thanks --bb |
From: Alan G I. <ai...@am...> - 2006-05-18 15:13:14
|
On Thu, 18 May 2006, Bill Baxter apparently wrote: > One thing I haven't quite managed to figure out yet, is > what the heck indexing an array with an array is supposed > to give you. I think you want section 3.3.6.1 of Travis's book, which is easily one of the hardest sections of the book. I find it nonobvious that when x and y are nd-arrays that x[y] should differ from x[tuple(y)] or x[list(y)], but as explained in this section it does in a big way. Cheers, Alan Isaac |
From: Pau G. <pau...@gm...> - 2006-05-18 15:23:21
|
On 5/18/06, Bill Baxter <wb...@gm...> wrote: > One thing I haven't quite managed to figure out yet, is what the heck > indexing an array with an array is supposed to give you. > This is sort of an offshoot of the "nonzero()" discussion. > I was wondering why nonzero() and where() return tuples at all instead of > just an array, till I tried it. > > >>> b > array([[0, 1, 2], > [3, 4, 5], > [6, 7, 8]]) > >>> b[ num.asarray(num.where(b>4)) ] > array([[[3, 4, 5], > [6, 7, 8], > [6, 7, 8], > [6, 7, 8]], > > [[6, 7, 8], > [0, 1, 2], > [3, 4, 5], > [6, 7, 8]]]) > > Whoa, not sure what that is. Can someone explain the current rule and in > what situations is it useful? > > Thanks > --bb i think that is b and x are arrays then, b[x]_ijk =3D b[ x_ijk ] where ijk is whatever x needs to be indexed with. Note that the indexing on b is _only_ on its first dimension. >>> from numpy import * >>> b =3D arange(9).reshape(3,3) >>> b array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) >>> x,y =3D where(b>4) >>> xy =3D asarray( (x,y) ) >>> xy array([[1, 2, 2, 2], [2, 0, 1, 2]]) >>> b[x,y] array([5, 6, 7, 8]) >>> b[xy] array([[[3, 4, 5], [6, 7, 8], [6, 7, 8], [6, 7, 8]], [[6, 7, 8], [0, 1, 2], [3, 4, 5], [6, 7, 8]]]) >>> asarray( (b[x],b[y]) ) array([[[3, 4, 5], [6, 7, 8], [6, 7, 8], [6, 7, 8]], [[6, 7, 8], [0, 1, 2], [3, 4, 5], [6, 7, 8]]]) |
From: Travis O. <oli...@ie...> - 2006-05-18 16:40:25
|
Pau Gargallo wrote: > On 5/18/06, Bill Baxter <wb...@gm...> wrote: >> One thing I haven't quite managed to figure out yet, is what the heck >> indexing an array with an array is supposed to give you. >> This is sort of an offshoot of the "nonzero()" discussion. >> I was wondering why nonzero() and where() return tuples at all >> instead of >> just an array, till I tried it. >> >> >>> b >> array([[0, 1, 2], >> [3, 4, 5], >> [6, 7, 8]]) >> >>> b[ num.asarray(num.where(b>4)) ] >> array([[[3, 4, 5], >> [6, 7, 8], >> [6, 7, 8], >> [6, 7, 8]], >> >> [[6, 7, 8], >> [0, 1, 2], >> [3, 4, 5], >> [6, 7, 8]]]) >> >> Whoa, not sure what that is. Can someone explain the current rule >> and in >> what situations is it useful? I can't take too much credit for the indexing behavior. I took what numarray had done and extended it just a little bit to allow mixing of slices and index arrays. It was easily one of the most complicated things to write for NumPy. What you are observing is called "partial indexing" by Numarray. The indexing rules where also spelled out in emails to this list last year and placed in the design document for what was then called Numeric3 (I'm not sure if that design document is still visible or not (probably under old.scipy.org it could be found). The section in my book that covers this is based on that information. I can't pretend to have use cases for all the indexing fanciness because I was building off the work that numarray had already pioneered. -Travis |
From: Bill B. <wb...@gm...> - 2006-05-19 01:45:05
|
I read the chapter in your book and sort of vaguely understand what it does now. Take an array A and index array ind: >>> A array([[ 0, 5, 10, 15], [ 1, 6, 11, 16], [ 2, 7, 12, 17], [ 3, 8, 13, 18], [ 4, 9, 14, 19]]) >>> ind array([[1, 3, 4], [0, 2, 1]]) And you get >>> A[ind] array([[[ 1, 6, 11, 16], [ 3, 8, 13, 18], [ 4, 9, 14, 19]], [[ 0, 5, 10, 15], [ 2, 7, 12, 17], [ 1, 6, 11, 16]]]) In this case it's roughly equivalent to [ A[row] for row in ind ]. >>> num.asarray( [ A[r] for r in ind ] ) array([[[ 1, 6, 11, 16], [ 3, 8, 13, 18], [ 4, 9, 14, 19]], [[ 0, 5, 10, 15], [ 2, 7, 12, 17], [ 1, 6, 11, 16]]]) >>> So I guess it could be useful if you want to take a bunch of different random samples of your data and stack them all up. E.g. you have a (1000,50) shaped grid of data, and you want to take N random samplings, eac= h consisting of 100 rows from the original grid, and then put them all together into an (N,100,50) array. Or say you want to make a stack of sliding windows on the data like rows 0-5, then rows 1-6, then rows 2-7, et= c to make a big (1000-5,5,50) array. Might be useful for that kind of thing. But thinking about applying an index obj of shape (2,3,4) to a (10,20,30,40,50) shaped array just makes my head hurt. :-) Does anyone actually use it, though? I also found it unexpected that A[ (ind[0], ind[1] ) ] doesn't do the same thing as A[ind] when ind.shape=3D= ( A.ndim, N). List of array -- as in A[ [ind[0], ind[1]] ] -- seems to act just like tuple of array also. --bill |