Finding Nonzero Elements in a Sparse Matrix

Discussion in 'Python' started by deLenn, Nov 7, 2006.

  1. deLenn

    deLenn Guest

    Hi,

    Does scipy have an equivalent to Matlab's 'find' function, to list the
    indices of all nonzero elements in a sparse matrix?

    Cheers.
     
    deLenn, Nov 7, 2006
    #1
    1. Advertising

  2. The function you might want is nonzero() or flatnonzero()

    >>> from numpy import *


    >>> a=array([ [1,2],[0,4] ])


    >>> a

    array([[1, 2],
    [0, 4]])

    >>> flatnonzero(a)

    array([0, 1, 3])

    nonzero() will return the a sequence of index arrays of non zero
    elements
    flatnonzero() returns the non-zero elements of the flattened version
    of the array.

    Cheers,
    Nick Vatamaniuc



    deLenn wrote:
    > Hi,
    >
    > Does scipy have an equivalent to Matlab's 'find' function, to list the
    > indices of all nonzero elements in a sparse matrix?
    >
    > Cheers.
     
    Nick Vatamaniuc, Nov 7, 2006
    #2
    1. Advertising

  3. deLenn

    deLenn Guest

    Thanks for the reply.

    'nonzero' deos not seem to work with sparse matrices. here is an
    example:


    from scipy import *
    A = sparse.lil_matrix((3,3))
    A[1,2] = 10
    A[2,0] = -10

    nonzero(A)
    >>> ()



    (I tried it with an ordinary matrix, and it works fine)

    Cheers.

















    Nick Vatamaniuc wrote:
    > The function you might want is nonzero() or flatnonzero()
    >
    > >>> from numpy import *

    >
    > >>> a=array([ [1,2],[0,4] ])

    >
    > >>> a

    > array([[1, 2],
    > [0, 4]])
    >
    > >>> flatnonzero(a)

    > array([0, 1, 3])
    >
    > nonzero() will return the a sequence of index arrays of non zero
    > elements
    > flatnonzero() returns the non-zero elements of the flattened version
    > of the array.
    >
    > Cheers,
    > Nick Vatamaniuc
    >
    >
    >
    > deLenn wrote:
    > > Hi,
    > >
    > > Does scipy have an equivalent to Matlab's 'find' function, to list the
    > > indices of all nonzero elements in a sparse matrix?
    > >
    > > Cheers.
     
    deLenn, Nov 7, 2006
    #3
  4. deLenn

    Robert Kern Guest

    deLenn wrote:
    > Hi,
    >
    > Does scipy have an equivalent to Matlab's 'find' function, to list the
    > indices of all nonzero elements in a sparse matrix?


    You will want to ask scipy questions on the scipy list.

    http://www.scipy.org/Mailing_Lists

    There is no explicit interface on sparse matrix objects to expose the indices of
    the nonzero elements. A different implementation would have to be written for
    each type of sparse matrix format. However, if one can spare the memory, one can
    convert to the coordinate list format and read the row and column indices from
    that object.


    In [1]: from scipy.sparse import lil_matrix

    In [2]: A = lil_matrix((3,3))

    In [3]: A[1,2] = 10

    In [4]: A[2,0] = -10

    In [5]: Acoo = A.tocoo()

    In [6]: Acoo.row
    Out[6]: array([2, 1])

    In [7]: Acoo.col
    Out[7]: array([0, 2])


    --
    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, Nov 7, 2006
    #4
  5. de Lenn,

    Sorry I assumed the nonzero would work for sparse matrices as well.

    BUT! -- If the sparse matrix used is the default scipy's
    sparse.lil_matrix, you just need to print out the representation
    because the lil_matrix is implemented as a _sequence of non-zero
    elements_ i.e. just what you need.

    In other words it is kind of silly to provide a nonzero for lil_matrix
    because it has _only_ non-zero elements.

    Well, here is the example:
    ------------------------------------
    >>> from scipy import *
    >>> A=sparse.lil_matrix((3,3))
    >>> A[1,2]=10
    >>> A[2,0]=-10
    >>> print A

    (1, 2) 10
    (2, 0) -10
    >>>

    ------------------------------------

    The only way it could be helpful is if you get a lil_matrix returned as
    an object from some code and you need to list all the elements...

    Hope this helps,
    Nick Vatamaniuc


    deLenn wrote:
    > Thanks for the reply.
    >
    > 'nonzero' deos not seem to work with sparse matrices. here is an
    > example:
    >
    >
    > from scipy import *
    > A = sparse.lil_matrix((3,3))
    > A[1,2] = 10
    > A[2,0] = -10
    >
    > nonzero(A)
    > >>> ()

    >
    >
    > (I tried it with an ordinary matrix, and it works fine)
    >
    > Cheers.
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > Nick Vatamaniuc wrote:
    > > The function you might want is nonzero() or flatnonzero()
    > >
    > > >>> from numpy import *

    > >
    > > >>> a=array([ [1,2],[0,4] ])

    > >
    > > >>> a

    > > array([[1, 2],
    > > [0, 4]])
    > >
    > > >>> flatnonzero(a)

    > > array([0, 1, 3])
    > >
    > > nonzero() will return the a sequence of index arrays of non zero
    > > elements
    > > flatnonzero() returns the non-zero elements of the flattened version
    > > of the array.
    > >
    > > Cheers,
    > > Nick Vatamaniuc
    > >
    > >
    > >
    > > deLenn wrote:
    > > > Hi,
    > > >
    > > > Does scipy have an equivalent to Matlab's 'find' function, to list the
    > > > indices of all nonzero elements in a sparse matrix?
    > > >
    > > > Cheers.
     
    Nick Vatamaniuc, Nov 8, 2006
    #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. Luis Esteban Valencia

    nonzero values?

    Luis Esteban Valencia, Jan 13, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    356
    David Jessee
    Jan 14, 2005
  2. Trent Buck

    nonzero != 1, right?

    Trent Buck, Nov 11, 2004, in forum: C Programming
    Replies:
    28
    Views:
    854
  3. friends

    Sparse Matrix operations

    friends, Sep 17, 2005, in forum: C Programming
    Replies:
    0
    Views:
    517
    friends
    Sep 17, 2005
  4. mariaczi
    Replies:
    7
    Views:
    408
    mariaczi
    Jun 9, 2006
  5. Replies:
    5
    Views:
    1,215
Loading...

Share This Page