1

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]]

4

3 回答 3

3

列表理解是最简单的方法:

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

结果:

>>> 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]]
于 2013-08-04T13:13:01.157 回答
1

这是一种非常老套的方法来做你要求的事情:

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

基本上它将嵌套元组中的每个值转换为字符串并将所有这些字符串连接在一起,然后将其转换回整数。对 的每个元素重复mx

于 2013-08-03T20:07:36.937 回答
1

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
于 2013-08-03T18:52:09.770 回答