yas*_*sin 6 python numpy scipy sparse-matrix
我使用Numpy在Python中有以下代码:
p = np.diag(1.0 / np.array(x))
Run Code Online (Sandbox Code Playgroud)
如何转换它以获得p2具有相同值的稀疏矩阵p而不创建p第一个?
使用scipy.sparse.spdiags(做了很多,所以最初可能会令人困惑)scipy.sparse.dia_matrix和/或scipy.sparse.lil_diags.(取决于你想要稀疏矩阵的格式 ......)
例如使用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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4849 次 |
| 最近记录: |