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 |