[英]numpy/scipy equivalent of MATLAB's sparse function
我正在用 numpy 和 scipy 在 Python 中移植 MATLAB 代码,我需要使用 numpy/scipy 等效于 MATLAB 中的稀疏函数。
下面是MATLAB中稀疏函数的用法,
sparse([3; 2], [2; 4], [3; 0])
给出:
Trial>> m = sparse([3; 2], [2; 4], [3; 0])
m =
(3,2) 3
Trial>> full(m)
ans =
0 0 0 0
0 0 0 0
0 3 0 0
我有这些,但它们没有给出 MATLAB 版本的功能,
sps.csr_matrix([3, 2], [2, 4], [3, 0])
sps.csr_matrix(np.array([[3], [2]]), np.array([[2], [4]]), np.array([[3], [0]]))
sps.csr_matrix([[3], [2]], [[2], [4]], [[3], [0]])
有什么想法吗? 谢谢。
您使用的是sparse(I, J, SV)
形式 [注意:链接指向 GNU Octave 的文档,而不是 Matlab]。 scipy.sparse
等价物是csr_matrix((SV, (I, J)))
- 是的,是一个包含向量和向量的 2 元组的 2 元组的单个参数。 您还必须更正索引向量,因为 Python 始终使用基于 0 的索引。
>>> m = sps.csr_matrix(([3,0], ([2,1], [1,3]))); m
<3x4 sparse matrix of type '<class 'numpy.int64'>'
with 2 stored elements in Compressed Sparse Row format>
>>> m.todense()
matrix([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 3, 0, 0]], dtype=int64)
请注意,与 Matlab 不同,scipy 不会自动丢弃显式零,并且将使用整数存储来存储仅包含整数的矩阵。 要完美匹配您在 Matlab 中获得的矩阵,您必须明确要求浮点存储,并且必须对结果调用eliminate_zeros()
:
>>> m2 = sps.csr_matrix(([3,0], ([2,1], [1,3])), dtype=np.float)
>>> m2.eliminate_zeros()
>>> m2
<3x4 sparse matrix of type '<class 'numpy.float64'>'
with 1 stored elements in Compressed Sparse Row format>
>>> m2.todense()
matrix([[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 3., 0., 0.]])
您也可以将[3,0]
更改为[3., 0.]
但我建议使用明确的dtype=
参数,因为这将防止您在输入真实数据时出现意外。
(我不知道 Matlab 的内部稀疏矩阵表示是什么,但 Octave 似乎默认为压缩稀疏列表示。CSC 和 CSR 之间的差异应该只会影响性能。如果您的 NumPy 代码最终比您的 Matlab 代码慢,请尝试使用sps.csc_matrix
而不是csr_matrix
,以及所有常用的 NumPy 性能技巧。)
(如果你还没有,你可能需要为 Matlab 用户阅读NumPy 。)
这是我所做的转换。 它适用于 5 个参数版本的 sparse。
def sparse(i, j, v, m, n):
"""
Create and compressing a matrix that have many zeros
Parameters:
i: 1-D array representing the index 1 values
Size n1
j: 1-D array representing the index 2 values
Size n1
v: 1-D array representing the values
Size n1
m: integer representing x size of the matrix >= n1
n: integer representing y size of the matrix >= n1
Returns:
s: 2-D array
Matrix full of zeros excepting values v at indexes i, j
"""
return scipy.sparse.csr_matrix((v, (i, j)), shape=(m, n))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.