Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > a question about numpy

Reply
Thread Tools

a question about numpy

 
 
hi_roger
Guest
Posts: n/a
 
      09-09-2009
hello, i want to ask a question about numpy.

i know how to select a submatrix using the slice object in numpy. But
how can i select a submatrix
A[i1,i2,i3;j1,j2,j3] (elements in A on line i1,i2,i3 and column
j1,j2,j3 , and i1,i2,i3,j1,j2,j3 are all arbitrary numbers )
The submatrix must share data memory with original matrix.

Any one help? thank you
 
Reply With Quote
 
 
 
 
sturlamolden
Guest
Posts: n/a
 
      09-09-2009
On 9 Sep, 03:45, hi_roger <(E-Mail Removed)> wrote:

> i know how to select a submatrix using the slice object in numpy. But
> how can i select a submatrix
> A[i1,i2,i3;j1,j2,j3] (elements in A on line i1,i2,i3 and column
> j1,j2,j3 , *and i1,i2,i3,j1,j2,j3 are all arbitrary numbers )


You just pass an array of ints for each dimension. If you want a 3x3
submatrix, you must pass in two 3x3 arrays of indices.





> The submatrix must share data memory with original matrix.


That is the tricky part. An ndarray must be indexable using an array
of strides. That is in C:

void *get_element_ptr( PyArrayObject *a, int indices[])
{
char *out = a->data;
int d;
for (d=0; d < a->nd; d++)
out += indices[d] * a->strides[d];
return (void *)out;
}

If you slice with an array or list of ints in Python, the resulting
array cannot be indexed like this. Therefore NumPy is forced to make a
copy. So if I do

>>> import numpy as np
>>> a = np.zeros((10,10))
>>> b = a[[1,3,5],[6,7,8]]


I get this:

>>> b.flags['OWNDATA']

True


But:

>>> c = a[::2,::2]
>>> c.flags['OWNDATA']

False

This is because C can still be indexed with strides as shown above,
and no copy is made.

















 
Reply With Quote
 
 
 
 
sturlamolden
Guest
Posts: n/a
 
      09-09-2009
On 9 Sep, 03:45, hi_roger <(E-Mail Removed)> wrote:
> hello, i want to ask a question about numpy.
>
> i know how to select a submatrix using the slice object in numpy. But
> how can i select a submatrix
> A[i1,i2,i3;j1,j2,j3] (elements in A on line i1,i2,i3 and column
> j1,j2,j3 , *and i1,i2,i3,j1,j2,j3 are all arbitrary numbers )
> The submatrix must share data memory with original matrix.


So the only way to do this is to make an ndarray subclass that
overloads __getitem__, __setitem__, and __iter__, and takes care of
the mapping into A. Thus you get a double indirection.






 
Reply With Quote
 
Robert Kern
Guest
Posts: n/a
 
      09-09-2009
On 2009-09-08 20:45 PM, hi_roger wrote:
> hello, i want to ask a question about numpy.
>
> i know how to select a submatrix using the slice object in numpy. But
> how can i select a submatrix
> A[i1,i2,i3;j1,j2,j3] (elements in A on line i1,i2,i3 and column
> j1,j2,j3 , and i1,i2,i3,j1,j2,j3 are all arbitrary numbers )
> The submatrix must share data memory with original matrix.
>
> Any one help? thank you


Sturla is almost correct. What you really want is this:

i = [[i1], [i2], [i3]]
j = [[j1, j2, j3]]
B = A[i, j]

http://docs.scipy.org/doc/numpy/refe...g.html#integer

If you have more numpy questions, please ask on the numpy mailing list.

http://www.scipy.org/Mailing_Lists

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

 
Reply With Quote
 
Robert Kern
Guest
Posts: n/a
 
      09-09-2009
On 2009-09-08 22:03 PM, sturlamolden wrote:
> On 9 Sep, 03:45, hi_roger<(E-Mail Removed)> wrote:
>
>> i know how to select a submatrix using the slice object in numpy. But
>> how can i select a submatrix
>> A[i1,i2,i3;j1,j2,j3] (elements in A on line i1,i2,i3 and column
>> j1,j2,j3 , and i1,i2,i3,j1,j2,j3 are all arbitrary numbers )

>
> You just pass an array of ints for each dimension. If you want a 3x3
> submatrix, you must pass in two 3x3 arrays of indices.


Oops! I just responded to the OP saying that you were "almost correct"; but I
was just thinking of your latter 1D example rather than this correct statement.
My apologies.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Installing numpy over an older numpy Tom Kacvinsky Python 1 06-15-2012 07:15 PM
A More Concise Description of Numpy than the Guide to Numpy? W. eWatson Python 2 11-23-2009 08:58 PM
NumPy Question - numpy.put in multi-dimensional array Bryan.Fodness@gmail.com Python 2 11-13-2007 10:36 PM
numpy migration (also posted to numpy-discussion) Duncan Smith Python 3 04-25-2007 01:36 AM
Trouble with numpy-0.9.4 and numpy-0.9.5 drife Python 1 03-01-2006 05:59 PM



Advertisments