numpy/scipy 相当于MATLAB的稀疏函数
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)
form [note: link goes to documentation for GNU Octave, not Matlab]. The scipy.sparse
equivalent is csr_matrix((SV, (I, J)))
-- 是的,单个参数是一个包含一个向量和一个向量的二元组的二元组。您还必须更正索引向量,因为 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 性能提示。)
(如果您还没有阅读 NumPy for Matlab users,您可能需要阅读。)
这里是我做的转换。它适用于稀疏的 5 个参数版本。
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))
我正在使用 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)
form [note: link goes to documentation for GNU Octave, not Matlab]. The scipy.sparse
equivalent is csr_matrix((SV, (I, J)))
-- 是的,单个参数是一个包含一个向量和一个向量的二元组的二元组。您还必须更正索引向量,因为 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 性能提示。)
(如果您还没有阅读 NumPy for Matlab users,您可能需要阅读。)
这里是我做的转换。它适用于稀疏的 5 个参数版本。
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))