我在Python中使用Numpy编写了以下代码:
p = np.diag(1.0 / np.array(x))
如何将其转换为与p
具有相同值的稀疏矩阵p2
,而不先创建p
?
发布于 2010-09-13 00:06:30
使用scipy.sparse.spdiags
(它做了很多事情,所以一开始可能会让人感到困惑)、scipy.sparse.dia_matrix
和/或scipy.sparse.lil_diags
。(取决于稀疏矩阵所在的format ...)
例如,使用spdiags
import numpy as np
import scipy as sp
import scipy.sparse
x = np.arange(10)
# "0" here indicates the main diagonal...
# "y" will be a dia_matrix type of sparse array, by default
y = sp.sparse.spdiags(x, 0, x.size, x.size)
发布于 2010-12-11 06:13:48
使用scipy.sparse模块,
p = sparse.dia_matrix(1.0 / np.array(x), shape=(len(x), len(x)));
https://stackoverflow.com/questions/3695434
复制相似问题