Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I give row and column names to Scipy's csr_matrix?

I don't know if it's possible, and it's possibly a naive question, but how can I set the equivalent of R's rownames() and colnames() to a scipy.sparse.csr.csr_matrix ?

I saw that my_matrix.dtype.names doesn't work here, and I can't find any "index" equivalend for such sparse matrix...

Moreover, pandas.sparse.* is not an option here, because of some open issue...

Thank you so much for your help,

like image 226
Jean Lescut Avatar asked Jan 29 '16 14:01

Jean Lescut


1 Answers

You'll have to maintain the names separately, as none of scipy's sparse formats support named indexing. This might look like:

foo = csr_matrix(...)
row_names = np.array(...)
col_names = np.array(...)

# index by name:
row_idx, = np.where(row_names == "my row")
col_idx, = np.where(col_names == "my col")
foo[row_idx, col_idx]
like image 74
perimosocordiae Avatar answered Sep 26 '22 03:09

perimosocordiae