问题描述
稀疏矩阵格式(dok)假定不在字典中的键的值等于零.有什么办法可以使它使用默认值而不是零?
还有一种方法可以计算稀疏矩阵的对数(类似于常规numpy矩阵中的np.log)
该功能不是内置功能,但是如果您确实需要此功能,则应该能够编写自己的dok_matrix
类或Scipy的子类. Scipy实现在这里: https://github.com/scipy/scipy/blob/master/scipy/sparse/dok.py 至少在进行dict.*
调用的地方,需要更改默认值---也许还有一些其他更改需要要做.
但是,我会尝试重新提出问题,以便不需要此问题.例如,如果您进行线性代数运算,则可以隔离常数项,而改为
from scipy.sparse.linalg import LinearOperator
A = whatever_dok_matrix_minus_constant_term
def my_matvec(x):
return A*x + constant_term * x.sum()
op = LinearOperator(A.shape, matvec=my_matvec)
对于大多数线性代数例程(例如迭代求解器),您可以传入op
而不是A
.
scipy.linalg.logm
所示)通常是稠密的,因此您应该先将矩阵转换为稠密的对数,然后照常计算对数.据我所知,使用稀疏矩阵将不会提高性能.如果只需要计算向量和对数log(A) * v
向量的乘积,则可以使用某些Krylov方法.如果OTOH要按元素计算对数,则可以直接修改.data
属性(至少在COO,CSR和CSC中可用)
x = A.tocoo()
x.data = np.log(x.data)
A = x.todok()
这只剩下零个元素,但是如上所述,它允许单独处理常量部分.
The sparse matrix format (dok) assumes that values of keys not in the dictionary are equal to zero. Is there any way to make it use a default value other than zero?
Also, is there a way to calculate the log of a sparse matrix (akin to np.log in regular numpy matrix)
That feature is not built-in, but if you really need this, you should be able to write your own dok_matrix
class, or subclass Scipy's one. The Scipy implementation is here: https://github.com/scipy/scipy/blob/master/scipy/sparse/dok.py At least in the places where dict.*
calls are made, the default value needs to be changed --- and maybe there are some other changes that need to be made.
However, I'd try to reformulate the problem so that this is not needed. If you for instance do linear algebra, you can isolate the constant term, and do instead
from scipy.sparse.linalg import LinearOperator
A = whatever_dok_matrix_minus_constant_term
def my_matvec(x):
return A*x + constant_term * x.sum()
op = LinearOperator(A.shape, matvec=my_matvec)
To most linear algebra routines (e.g. iterative solvers), you can pass in op
instead of A
.
As to the matrix logarithm: logarithm of a sparse matrix (as in scipy.linalg.logm
) is typically dense, so you should just convert the matrix to a dense one first, and then compute the logarithm as usual. As far as I see, using a sparse matrix would give no performance gain. If you need only to compute a product of a vector and the logarithm, log(A) * v
vector, some Krylov method might help, though.
If you OTOH want to compute the logarithm elementwise, you can modify the .data
attribute directly (available at least in COO, CSR, and CSC)
x = A.tocoo()
x.data = np.log(x.data)
A = x.todok()
This leaves the zero elements alone, but as above, this allows treating the constant part separately.
这篇关于scipy.sparse默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!