使用python稀疏矩阵进行快速行操作 [英] Fast row operations with python sparse matrices

查看:105
本文介绍了使用python稀疏矩阵进行快速行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python中有一个大的稀疏矩阵,并且想对其执行许多基本行操作.即,将一行的倍数添加到另一行.最有效的方法是什么? lil矩阵可以进行基本的行操作,但是速度很慢. csc和csr矩阵不支持这些操作.

I have a large sparse matrix in python, and would like to perform many elementary row operations on it. That is, adding a multiple of one row to another row. What is the most efficient way of doing this? Elementary row operations are possible with a lil matrix, but very slow. csc and csr matrices do not support these operations.

推荐答案

首先,MCVexample对这个问题有很大帮助.我只能推测您的行操作.

First, a MCVexample would help the question a lot. I can only speculate about your row operations.

一个基本问题-行的稀疏结构是否有所不同?以lil情况为例.如果2行具有相同的rows列表,则您的数学运算可以直接与data列表一起使用.如果rows与数学不同,则变得更加复杂,因为您必须同时更改两个列表.

A fundamental question - do the rows differ in their sparsity structure? Take the lil case. If 2 rows have the same rows lists, then your math can work with the data lists directly. If rows differ than math becomes much more complicated, since you have to change both lists.

lilcsr均支持按行编制索引

Both lil and csr support indexing by row

In [7]: M=sparse.rand(10,10,.3)
In [8]: Mr=M.tocsr()
In [9]: Ml=M.tolil()

是的,如果您通过添加另一行来更改行,则csr会发出警告:

Yes, csr gives a warning if you change a row by adding another:

In [17]: Ml[2,:] += Ml[1,:]
In [18]: Mr[2,:] += Mr[1,:]
...
  SparseEfficiencyWarning)

但是lil数学实际上使用的是csr中介. lil行表示为列表,而不是数组.

But the lil math actually uses a csr intermediary. lil rows are represented as lists, not arrays.

In [14]: Ml[1,:]+Ml[2,:]
Out[14]: 
<1x10 sparse matrix of type '<class 'numpy.float64'>'
    with 5 stored elements in Compressed Sparse Row format>

索引矩阵运算很慢,尤其是与密集数组等效项相比.但是他们会为您处理很多小细节.

Indexed matrix operations are slow, especially compared to the dense array equivalents. But they take care of a lot of little details for you.

我已经在其他SO答案中探讨了行操作.当我对您要做什么有更好的了解时,我会搜索这些内容.

I've explored row operations in other SO answers. When I have a better idea of what you are trying to do, I search those.

总的来说,这不是万能的,尤其是当您更改稀疏性时. scipy sparse并不是用于快速行计算的最佳工具.

Overall, there isn't a magic bullet, especially if you are changing sparsity. scipy sparse isn't the best tool for fast row calculations.

scipy:添加稀疏向量到稀疏矩阵的特定行的稀疏向量-这个距离足够近,我很想将这个问题标记为重复.

scipy: Adding a sparse vector to a specific row of a sparse matrix - this one is close enough that I'm tempted to flag this question as a duplicate.

Sparse中的极慢和行操作Python中的LIL矩阵

(有关'user:901925 [scipy] rows'的SO搜索中的更多内容)

(more in a SO search on 'user:901925 [scipy] rows')

这篇关于使用python稀疏矩阵进行快速行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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