【问题标题】:How to transform numpy.matrix or array to scipy sparse matrix如何将 numpy.matrix 或数组转换为 scipy 稀疏矩阵
【发布时间】:2011-12-16 20:58:12
【问题描述】:

对于 SciPy 稀疏矩阵,可以使用 todense()toarray() 转换为 NumPy 矩阵或数组。做逆运算的函数是什么?

我搜索了,但不知道哪些关键字应该是正确的。

【问题讨论】:

    标签: python numpy scipy sparse-matrix


    【解决方案1】:

    您可以在初始化稀疏矩阵时传递一个 numpy 数组或矩阵作为参数。例如,对于 CSR 矩阵,您可以执行以下操作。

    >>> import numpy as np
    >>> from scipy import sparse
    >>> A = np.array([[1,2,0],[0,0,3],[1,0,4]])
    >>> B = np.matrix([[1,2,0],[0,0,3],[1,0,4]])
    
    >>> A
    array([[1, 2, 0],
           [0, 0, 3],
           [1, 0, 4]])
    
    >>> sA = sparse.csr_matrix(A)   # Here's the initialization of the sparse matrix.
    >>> sB = sparse.csr_matrix(B)
    
    >>> sA
    <3x3 sparse matrix of type '<type 'numpy.int32'>'
            with 5 stored elements in Compressed Sparse Row format>
    
    >>> print sA
      (0, 0)        1
      (0, 1)        2
      (1, 2)        3
      (2, 0)        1
      (2, 2)        4
    

    【讨论】:

    • 高维数组呢?
    • 我的矩阵出现内存错误 (~25,000x25,000)。此外,当我申请sparse.csr_matrix 时,内存消耗会像疯了似的跳跃
    【解决方案2】:

    scipy 中有几个稀疏矩阵类。

    bsr_matrix(arg1[, shape, dtype, copy, blocksize]) 块稀疏行矩阵
    coo_matrix(arg1[, shape, dtype, copy]) COOrdinate 格式的稀疏矩阵。
    csc_matrix(arg1[, shape, dtype, copy]) 压缩稀疏列矩阵
    csr_matrix(arg1[, shape, dtype, copy]) 压缩稀疏行矩阵
    dia_matrix(arg1[, shape, dtype, copy]) 具有对角存储的稀疏矩阵
    dok_matrix(arg1[, shape, dtype, copy]) 基于键的稀疏矩阵字典。
    lil_matrix(arg1[, shape, dtype, copy]) 基于行的链表稀疏矩阵

    他们中的任何一个都可以进行转换。

    import numpy as np
    from scipy import sparse
    a=np.array([[1,0,1],[0,0,1]])
    b=sparse.csr_matrix(a)
    print(b)
    
    (0, 0)  1
    (0, 2)  1
    (1, 2)  1
    

    http://docs.scipy.org/doc/scipy/reference/sparse.html#usage-information

    【讨论】:

      【解决方案3】:

      在 Python 中,Scipy 库 可用于将二维 NumPy 矩阵转换为稀疏矩阵。用于数值数据的 SciPy 2-D 稀疏矩阵包是 scipy.sparse

      scipy.sparse 包提供了不同的类来从二维矩阵创建以下类型的稀疏矩阵

      1. 块稀疏行矩阵
      2. COOrdinate 格式的稀疏矩阵。
      3. 压缩稀疏列矩阵
      4. 压缩稀疏行矩阵
      5. 具有对角存储的稀疏矩阵
      6. 基于稀疏矩阵的键字典。
      7. 基于行的列表稀疏矩阵列表
      8. 这个类为所有稀疏矩阵提供了一个基类。

      CSR(压缩稀疏行)或 CSC(压缩稀疏列)格式支持高效访问和矩阵运算。

      使用 Scipy 类将 Numpy 矩阵转换为压缩稀疏列 (CSC) 矩阵和压缩稀疏行 (CSR) 矩阵的示例代码:

      import sys                 # Return the size of an object in bytes
      import numpy as np         # To create 2 dimentional matrix
      from scipy.sparse import csr_matrix, csc_matrix 
      # csr_matrix: used to create compressed sparse row matrix from Matrix
      # csc_matrix: used to create compressed sparse column matrix from Matrix
      

      创建一个二维 Numpy 矩阵

      A = np.array([[1, 0, 0, 0, 0, 0],\
                    [0, 0, 2, 0, 0, 1],\
                    [0, 0, 0, 2, 0, 0]])
      print("Dense matrix representation: \n", A)
      print("Memory utilised (bytes): ", sys.getsizeof(A))
      print("Type of the object", type(A))
      

      打印矩阵和其他细节:

      Dense matrix representation: 
       [[1 0 0 0 0 0]
       [0 0 2 0 0 1]
       [0 0 0 2 0 0]]
      Memory utilised (bytes):  184
      Type of the object <class 'numpy.ndarray'>
      

      使用 csr_matrix 类将矩阵 A 转换为 压缩稀疏行 矩阵表示:

      S = csr_matrix(A)
      print("Sparse 'row' matrix: \n",S)
      print("Memory utilised (bytes): ", sys.getsizeof(S))
      print("Type of the object", type(S))
      

      打印语句的输出:

      Sparse 'row' matrix:
      (0, 0) 1
      (1, 2) 2
      (1, 5) 1
      (2, 3) 2
      Memory utilised (bytes): 56
      Type of the object: <class 'scipy.sparse.csr.csc_matrix'>
      

      使用 csc_matrix 类将矩阵 A 转换为 压缩稀疏列 矩阵表示:

      S = csc_matrix(A)
      print("Sparse 'column' matrix: \n",S)
      print("Memory utilised (bytes): ", sys.getsizeof(S))
      print("Type of the object", type(S))
      

      打印语句的输出:

      Sparse 'column' matrix:
      (0, 0) 1
      (1, 2) 2
      (2, 3) 2
      (1, 5) 1
      Memory utilised (bytes): 56
      Type of the object: <class 'scipy.sparse.csc.csc_matrix'>
      

      可以看出压缩后的矩阵大小为 56 字节,原始矩阵大小为 184 字节。

      更详细的解释和代码示例请参考这篇文章:https://limitlessdatascience.wordpress.com/2020/11/26/sparse-matrix-in-machine-learning/

      【讨论】:

        【解决方案4】:

        至于逆,函数是inv(A),但我不建议使用它,因为对于巨大的矩阵,它的计算成本非常高且不稳定。相反,您应该使用逆的近似值,或者如果您想求解 Ax = b,您实际上并不需要 A-1

        【讨论】:

        • 问题问如何使用numpy矩阵/数组生成scipy稀疏矩阵,而不是逆矩阵运算。
        猜你喜欢
        • 2023-04-10
        • 2014-12-21
        • 2020-12-07
        • 2019-08-17
        • 2020-11-26
        • 2018-12-10
        • 2021-11-25
        • 2017-07-02
        • 2019-12-04
        相关资源
        最近更新 更多