Dear People, Suppose I create a numarray arry (say). A numeric array would be fine too; it probably does not matter.
import numarray foo = numarray.reshape(numarray.arange(9),(3,3)) array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15]])
Is there some way to select the indexes corresponding only to (say) two rows or columns? ie suppose I just want the first and the fourth row or the first and third columns? Is there some clean way to do this which does not involve extracting individual rows or columns? Ie. I want something like
foo[?,:] array([[ 0, 1, 2, 3], [12, 13, 14, 15]])
etc. Suggestions appreciated. Please cc me; I'm not subscribed. Thanks in advance. Faheem.
On Sat, 2004-03-27 at 12:05, Faheem Mitha wrote:
Dear People,
Suppose I create a numarray arry (say). A numeric array would be fine too; it probably does not matter.
import numarray foo = numarray.reshape(numarray.arange(9),(3,3)) array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15]])
Is there some way to select the indexes corresponding only to (say) two rows or columns?
ie suppose I just want the first and the fourth row or the first and third columns? Is there some clean way to do this which does not involve extracting individual rows or columns?
Ie. I want something like
foo[?,:] array([[ 0, 1, 2, 3], [12, 13, 14, 15]])
Try foo[[0,2]]. Note that the array indexing syntax is a little picky: foo[(0,2)] (tuple index) won't work, just foo[[0,2]] (list index) or foo[array([0,2])] (array index). Regards, Todd
etc.
Suggestions appreciated. Please cc me; I'm not subscribed. Thanks in advance.
Faheem.
------------------------------------------------------- This SF.Net email is sponsored by: IBM Linux Tutorials Free Linux tutorial presented by Daniel Robbins, President and CEO of GenToo technologies. Learn everything from fundamentals to system administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click _______________________________________________ Numpy-discussion mailing list Numpy-discussion@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/numpy-discussion -- Todd Miller
On Sat, 27 Mar 2004, Todd Miller wrote:
Try foo[[0,2]]. Note that the array indexing syntax is a little picky: foo[(0,2)] (tuple index) won't work, just foo[[0,2]] (list index) or foo[array([0,2])] (array index).
Thanks. I see this extracts the relevant rows. How about columns? Also, where is this documented? I spent quite a while poring over the numarray documentation. The section on index arrays looked relevant but I found it difficult to understand. Faheem.
On Sat, 2004-03-27 at 19:27, Faheem Mitha wrote:
On Sat, 27 Mar 2004, Todd Miller wrote:
Try foo[[0,2]]. Note that the array indexing syntax is a little picky: foo[(0,2)] (tuple index) won't work, just foo[[0,2]] (list index) or foo[array([0,2])] (array index).
Thanks. I see this extracts the relevant rows. How about columns?
I think for columns you need to transpose the matrix or use take() with the right axis=1.
Also, where is this documented?
pp 35,36,37,49,50,51
I spent quite a while poring over the numarray documentation. The section on index arrays looked relevant but I found it difficult to understand.
That's probably a corollary to the fact that index arrays, put, and take *are* a little difficult to understand, even more so in numarray than in Numeric. Regards, Todd
Faheem.
------------------------------------------------------- This SF.Net email is sponsored by: IBM Linux Tutorials Free Linux tutorial presented by Daniel Robbins, President and CEO of GenToo technologies. Learn everything from fundamentals to system administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click _______________________________________________ Numpy-discussion mailing list Numpy-discussion@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/numpy-discussion
--
Todd Miller
On Sat, 27 Mar 2004, Todd Miller wrote:
On Sat, 2004-03-27 at 19:27, Faheem Mitha wrote:
On Sat, 27 Mar 2004, Todd Miller wrote:
Try foo[[0,2]]. Note that the array indexing syntax is a little picky: foo[(0,2)] (tuple index) won't work, just foo[[0,2]] (list index) or foo[array([0,2])] (array index).
Thanks. I see this extracts the relevant rows. How about columns?
I think for columns you need to transpose the matrix or use take() with the right axis=1.
I see. Thanks. I think that take() does what I need. BTW, this documentation example for take looks wrong, and gives an error for me. *************************************************
a1 = array([10,20,30,40]) print a1[[3,5]] [40 60] print take(a1,[3,5]) [40 60]
I get
a1 = numarray.array([10,20,30,40]) a1 array([10, 20, 30, 40]) numarray.take(a1,[3,5]) Traceback (most recent call last): File "<stdin>", line 1, in ? File "/usr/lib/python2.3/site-packages/numarray/ufunc.py", line 1776, in take return array._take((indices,), outarr=outarr, clipmode=clipmode) File "/usr/lib/python2.3/site-packages/numarray/numarraycore.py", line 498, in _take return ufunc._take(self, indices, **keywds) File "/usr/lib/python2.3/site-packages/numarray/ufunc.py", line 1579, in __call__ result = self._doit(computation_mode, woutarr, cfunc, ufargs, 0) File "/usr/lib/python2.3/site-packages/numarray/ufunc.py", line 1551, in _doit apply(cfunc, ufargs) IndexError: Index[1,0]=5 out of range[4]
a1[[3,5]] array([40, 40])
Faheem.
On Sat, 2004-03-27 at 23:47, Faheem Mitha wrote:
On Sat, 27 Mar 2004, Todd Miller wrote:
On Sat, 2004-03-27 at 19:27, Faheem Mitha wrote:
On Sat, 27 Mar 2004, Todd Miller wrote:
Try foo[[0,2]]. Note that the array indexing syntax is a little picky: foo[(0,2)] (tuple index) won't work, just foo[[0,2]] (list index) or foo[array([0,2])] (array index).
Thanks. I see this extracts the relevant rows. How about columns?
I think for columns you need to transpose the matrix or use take() with the right axis=1.
I see. Thanks. I think that take() does what I need. BTW, this documentation example for take looks wrong, and gives an error for me.
*************************************************
a1 = array([10,20,30,40]) print a1[[3,5]] [40 60] print take(a1,[3,5]) [40 60]
Yes, you're right, that's an error.
I get
a1 = numarray.array([10,20,30,40]) a1 array([10, 20, 30, 40]) numarray.take(a1,[3,5]) Traceback (most recent call last): File "<stdin>", line 1, in ? File "/usr/lib/python2.3/site-packages/numarray/ufunc.py", line 1776, in take return array._take((indices,), outarr=outarr, clipmode=clipmode) File "/usr/lib/python2.3/site-packages/numarray/numarraycore.py", line 498, in _take return ufunc._take(self, indices, **keywds) File "/usr/lib/python2.3/site-packages/numarray/ufunc.py", line 1579, in __call__ result = self._doit(computation_mode, woutarr, cfunc, ufargs, 0) File "/usr/lib/python2.3/site-packages/numarray/ufunc.py", line 1551, in _doit apply(cfunc, ufargs) IndexError: Index[1,0]=5 out of range[4]
a1[[3,5]] array([40, 40])
This too looks like an error. Both usages should raise an exception...
and hopefully a prettier one in the future.
Thanks for the questions and feedback.
Regards,
Todd
--
Todd Miller
participants (2)
-
Faheem Mitha
-
Todd Miller