43

I am using a python function called "incidence_matrix(G)", which returns the incident matrix of graph. It is from Networkx package. The problem that I am facing is the return type of this function is "Scipy Sparse Matrix". I need to have the Incident matrix in the format of numpy matrix or array. I was wondering if there is any easy way of doing that or not? Or is there any built-in function that can do this transformation for me or not?

Thanks

Mr.Boy
  • 615
  • 1
  • 7
  • 13
  • 1
    Actually yes, it works and gives you an array. I was looking for a way to directly (using python functions) get the matrix having all zeros and ones. But thank you for that, I think finally I will go with the array if I could not find anything better. – Mr.Boy Oct 26 '14 at 18:49
  • Also, you may want to look at [this](http://stackoverflow.com/questions/4151128/what-are-the-differences-between-numpy-arrays-and-matrices-which-one-should-i-u). – farenorth Oct 26 '14 at 18:54
  • Yes, I used that but the problem with that is when you use it, it only stores the whole sparse matrix as one element in a matrix. when you wanna print it, you will see this: [[ <4x4 sparse matrix of type '' with 8 stored elements in Compressed Sparse Column format>]] – Mr.Boy Oct 26 '14 at 18:56
  • 1
    What about `numpy.matrix(numpy.array())`? – farenorth Oct 26 '14 at 19:01
  • I have already tried that. Same results. – Mr.Boy Oct 26 '14 at 19:04
  • 1
    OK. I think this is what you want `numpy.matrix(.toarray())`. – farenorth Oct 26 '14 at 19:04
  • `np.array()` or `np.matrix()` don't know anything special about sparse matrices. That's why they just wrap the input without any transformation. You need to use the sparse matrix methods. – hpaulj Oct 27 '14 at 02:03

3 Answers3

63

The scipy.sparse.*_matrix has several useful methods, for example, if a is e.g. scipy.sparse.csr_matrix:

  • a.toarray() or a.A - Return a dense ndarray representation of this matrix. (numpy.array, recommended)
  • a.todense() or a.M - Return a dense matrix representation of this matrix. (numpy.matrix)
sebix
  • 2,943
  • 2
  • 28
  • 43
  • 8
    Those two attributes have short aliases: if your sparse matrix is `a`, then `a.M` returns a dense numpy matrix object, and `a.A` returns a dense numpy array object. Unless you have very good reasons for it (and you probably don't!), stick to numpy arrays, i.e. `a.A`, and stay away from numpy matrix. – Jaime Oct 27 '14 at 00:42
4

I found that in the case of csr matrices, todense() and toarray() simply wrapped the tuples rather than producing a ndarray formatted version of the data in matrix form. This was unusable for the skmultilearn classifiers I'm training.

I translated it to a lil matrix- a format numpy can parse accurately, and then ran toarray() on that:

sparse.lil_matrix(<my-sparse_matrix>).toarray()
user108569
  • 450
  • 5
  • 8
2

The simplest way is to call the todense() method on the data:

In [1]: import networkx as nx

In [2]: G = nx.Graph([(1,2)])

In [3]: nx.incidence_matrix(G)
Out[3]: 
<2x1 sparse matrix of type '<type 'numpy.float64'>'
    with 2 stored elements in Compressed Sparse Column format>

In [4]: nx.incidence_matrix(G).todense()
Out[4]: 
matrix([[ 1.],
        [ 1.]])

In [5]: nx.incidence_matrix(G).todense().A
Out[5]: 
array([[ 1.],
       [ 1.]])
Aric
  • 24,511
  • 5
  • 78
  • 77