Question

I have the following matrix which I believe is sparse. I tried converting to dense using the x.dense format but it never worked. Any suggestions as to how to do this?, thanks.

mx=[[(0, 2), (1, 1), (2, 1), (3, 1), (4, 1), (5, 3), (6, 4), (7, 2), (8, 5), (9, 1)], 
[(10, 1), (11, 5), (12, 2), (13, 1), (21, 1), (22, 1), (23, 1), (24, 1), (25, 1), (26, 2)], 
[(27, 2), (28, 1), (29, 1), (30, 1), (31, 2), (32, 1), (33, 1), (34, 1), (35, 1), (36, 1)]]

someone put forward the solution below, but is there a better way?

def assign_coo_to_dense(sparse, dense):
    dense[sparse.row, sparse.col] = sparse.data

mx.todense(). Intended output should appear in this form:[[2,1,1,1,1,3,4], [1,5,2,1,1,1,1], [2,1,1,1,2,1,1,1]]

Was it helpful?

Solution

List comprehension is the easiest way:

new_list = [[b for _,b in sub] for sub in mx]

Result:

>>> new_list
[[2, 1, 1, 1, 1, 3, 4, 2, 5, 1], [1, 5, 2, 1, 1, 1, 1, 1, 1, 2], [2, 1, 1, 1, 2, 1, 1, 1, 1, 1]]

OTHER TIPS

Your source data do not really match any of the built-in formats supported by sparse matrices in SciPy (see http://docs.scipy.org/doc/scipy/reference/sparse.html and http://en.wikipedia.org/wiki/Sparse_matrix), so using .todense() will not really be productive here. In particular, if you have something like:

import numpy as np

my_sparseish_matrix = np.array([[(1, 2), (3, 4)]])

then my_sparseish_matrix will already be a dense numpy array ! Calling .todense() on it at that point will produce an error, and doesn't make sense anyway.

So my recommendation is to construct your dense array explicitly using a couple of for loops. To do this you'll need to know how many items are possible in your resulting vector -- call it N.

dense_vector = np.zeros((N, ), int)
for inner in mx:
    for index, value in inner:
        dense_vector[index] = value

Here's a pretty hacky way to do what you're asking for :

dense = [[int(''.join(str(val) for _, val in doc))] for doc in mx]

Basically it converts each value from the nested tuples into a string and concatenates all of those strings together, then converts that back to an integer. Repeat for each element of mx.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top