简体   繁体   English

numpy/scipy 等效于 MATLAB 的稀疏函数

[英]numpy/scipy equivalent of MATLAB's sparse function

I'm porting a MATLAB code in Python with numpy and scipy and I need to use numpy/scipy equivalent of the sparse function in MATLAB.我正在用 numpy 和 scipy 在 Python 中移植 MATLAB 代码,我需要使用 numpy/scipy 等效于 MATLAB 中的稀疏函数。

Here's the usage of the sparse function in MATLAB,下面是MATLAB中稀疏函数的用法,

sparse([3; 2], [2; 4], [3; 0])

gives:给出:

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

I have these, but they don't give what MATLAB version does,我有这些,但它们没有给出 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]])  

Any ideas?有什么想法吗? Thanks.谢谢。

You're using the sparse(I, J, SV) form [note: link goes to documentation for GNU Octave, not Matlab].您使用的是sparse(I, J, SV)形式 [注意:链接指向 GNU Octave 的文档,而不是 Matlab]。 The scipy.sparse equivalent is csr_matrix((SV, (I, J))) -- yes, a single argument which is a 2-tuple containing a vector and a 2-tuple of vectors. scipy.sparse等价物是csr_matrix((SV, (I, J))) - 是的,是一个包含向量和向量的 2 元组的 2 元组的单个参数。 You also have to correct the index vectors because Python consistently uses 0-based indexing.您还必须更正索引向量,因为 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)

Note that scipy, unlike Matlab, does not automatically discard explicit zeroes, and will use integer storage for matrices containing only integers.请注意,与 Matlab 不同,scipy 不会自动丢弃显式零,并且将使用整数存储来存储仅包含整数的矩阵。 To perfectly match the matrix you got in Matlab, you must explicitly ask for floating-point storage and you must call eliminate_zeros() on the result:要完美匹配您在 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.]])

You could also change [3,0] to [3., 0.] but I recommend an explicit dtype= argument because that will prevent surprises when you are feeding in real data.您也可以将[3,0]更改为[3., 0.]但我建议使用明确的dtype=参数,因为这将防止您在输入真实数据时出现意外。

(I don't know what Matlab's internal sparse matrix representation is, but Octave appears to default to compressed sparse column representation. The difference between CSC and CSR should only affect performance. If your NumPy code winds up being slower than your Matlab code, try using sps.csc_matrix instead of csr_matrix , as well as all the usual NumPy performance tips.) (我不知道 Matlab 的内部稀疏矩阵表示是什么,但 Octave 似乎默认为压缩稀疏表示。CSC 和 CSR 之间的差异应该只会影响性能。如果您的 NumPy 代码最终比您的 Matlab 代码慢,请尝试使用sps.csc_matrix而不是csr_matrix ,以及所有常用的 NumPy 性能技巧。)

(You probably need to read NumPy for Matlab users if you haven't already.) (如果你还没有,你可能需要为 Matlab 用户阅读NumPy 。)

here a conversion I made.这是我所做的转换。 It is working for the 5 arguments version of sparse.它适用于 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM