a question about numpy

Discussion in 'Python' started by hi_roger, Sep 9, 2009.

  1. hi_roger

    hi_roger Guest

    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
     
    hi_roger, Sep 9, 2009
    #1
    1. Advertising

  2. hi_roger

    sturlamolden Guest

    On 9 Sep, 03:45, hi_roger <> 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.
     
    sturlamolden, Sep 9, 2009
    #2
    1. Advertising

  3. hi_roger

    sturlamolden Guest

    On 9 Sep, 03:45, 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.


    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.
     
    sturlamolden, Sep 9, 2009
    #3
  4. hi_roger

    Robert Kern Guest

    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/reference/arrays.indexing.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
     
    Robert Kern, Sep 9, 2009
    #4
  5. hi_roger

    Robert Kern Guest

    On 2009-09-08 22:03 PM, sturlamolden wrote:
    > On 9 Sep, 03:45, hi_roger<> 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
     
    Robert Kern, Sep 9, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. drife
    Replies:
    1
    Views:
    368
    Travis E. Oliphant
    Mar 1, 2006
  2. Duncan Smith
    Replies:
    3
    Views:
    423
    Duncan Smith
    Apr 25, 2007
  3. Replies:
    2
    Views:
    492
    Robert Kern
    Nov 13, 2007
  4. W. eWatson
    Replies:
    2
    Views:
    946
    W. eWatson
    Nov 23, 2009
  5. Tom Kacvinsky

    Installing numpy over an older numpy

    Tom Kacvinsky, Jun 15, 2012, in forum: Python
    Replies:
    1
    Views:
    354
    Miki Tebeka
    Jun 15, 2012
Loading...

Share This Page