Scipy sparse matrix for cvxopt spmatrix?

I need to convert the scipy sparse matrix to the sparse matrix format cvxopt, spmatrix, and so far nothing has come across (the matrix is ​​too large to, of course, convert to dense). Any ideas how to do this?

+4
source share
3 answers

A more robust response - a combination hpaulj answer and OferHelman response .

def scipy_sparse_to_spmatrix(A):
    coo = A.tocoo()
    SP = spmatrix(coo.data.tolist(), coo.row.tolist(), coo.col.tolist(), size=A.shape)
    return SP

The definition of a variable form preserves the dimension A on SP. I found that any null columns ending in a scipy sparse matrix would be lost without this added step.

+3
source

http://maggotroot.blogspot.co.il/2013/11/constrained-linear-least-squares-in.html

coo = A.tocoo()
SP = spmatrix(coo.data, coo.row.tolist(), coo.col.tolist())
+1

From http://cvxopt.org/userguide/matrices.html#sparse-matrices

cvxopt.spmatrix(x, I, J[, size[, tc]])

looks like scipy.sparse

coo_matrix((data, (i, j)), [shape=(M, N)])

I assume that if Ais a matrix in the format coo, then

cvxopt.spmatrix(A.data, A.row, A.col, A.shape)

will work. (I do not have cvxoptto verify this.)

0
source

All Articles