NumPy: Importing a Sparse Matrix from R into Python -


i have matrix in r large , sparse, created 'matrix' package, , want handle in python + numpy. r object in csc format, , if export using function writemm in matrix package, output looks this:

%%matrixmarket matrix coordinate real general 4589 17366 160441 22 1 5.954510725783322 36 1 29.77255362891661 41 1 23.81804290313329 74 1 5.954510725783322 116 1 59.54510725783322 127 1 11.909021451566645 159 1 17.863532177349967 

where first column row, second 1 column, , third 1 value.

i wondering how import python. see scipy has module operate column-compressed sparse matrices, has no function create 1 file.

you can use scipy.io.mmread want.

in [11]: mmread("sparse_from_file") out[11]:  <4589x17366 sparse matrix of type '<class 'numpy.float64'>'     7 stored elements in coordinate format> 

note result coo sparse matrix. if want csc_matrix can use sparse.coo_matrix.tocsc.

now mention want handle very large , sparse matrix numpy. might turn out impractical since numpy operates on dense arrays , if matrix indeed large , sparse can't afford store in dense format.

so better off sticking efficient scipy.sparse format use case.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -