稀疏矩阵到cvxopt spmatrix? [英] scipy sparse matrix to cvxopt spmatrix?

查看:309
本文介绍了稀疏矩阵到cvxopt spmatrix?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将稀疏稀疏矩阵转换为cvxopt的稀疏矩阵格式spmatrix,并且还没有遇到任何问题(矩阵太大,无法转换为密集矩阵)。任何想法如何做到这一点?

I need to convert a scipy sparse matrix to cvxopt's sparse matrix format, spmatrix, and haven't come across anything yet (the matrix is too big to be converted to dense, of course). Any ideas how to do this?

推荐答案

更可靠的答案是 hpaulj的答案 OferHelman的答案

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

定义形状变量可保留SP上A的维数。我发现,如果不添加此步骤,任何以scipy稀疏矩阵结尾的零列都将丢失。

Defining the shape variable preserves the dimensionality of A on SP. I found that any zero columns ending the scipy sparse matrix would be lost without this added step.

这篇关于稀疏矩阵到cvxopt spmatrix?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆