【问题标题】:How to make all elements in many columns of a sparse csc matrix 0如何使稀疏 csc 矩阵的多列中的所有元素为 0
【发布时间】:2018-07-18 10:09:10
【问题描述】:
my_csr_matrix

my_csr_matrix[:,736225:1783504] = 0

Traceback (most recent call last):
  File "C:\Users\abhatia\AppData\Local\Continuum\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2862, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-135-b0e125d5d27e>", line 1, in <module>
    my_csr_matrix[:,736225:1783504] = 0
  File "C:\Users\abhatia\AppData\Local\Continuum\anaconda3\lib\site-packages\scipy\sparse\compressed.py", line 695, in __setitem__
    i, j = self._swap((i.ravel(), j.ravel()))
MemoryError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\abhatia\AppData\Local\Continuum\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2802, in run_ast_nodes
    if self.run_code(code, result):
  File "C:\Users\abhatia\AppData\Local\Continuum\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2879, in run_code
    self.showtraceback(running_compiled_code=True)
TypeError: showtraceback() got an unexpected keyword argument 'running_compiled_code'

【问题讨论】:

    标签: python scipy sparse-matrix


    【解决方案1】:
    In [5]: M = sparse.random(10,10,.2,format='csr')
    In [6]: M
    Out[6]: 
    <10x10 sparse matrix of type '<class 'numpy.float64'>'
        with 20 stored elements in Compressed Sparse Row format>
    
    In [8]: M[:,5:]
    Out[8]: 
    <10x5 sparse matrix of type '<class 'numpy.float64'>'
        with 12 stored elements in Compressed Sparse Row format>
    In [9]: M[:,5:] = 0
    /usr/local/lib/python3.5/dist-packages/scipy/sparse/compressed.py:742: SparseEfficiencyWarning: Changing the sparsity structure of a csr_matrix is expensive. lil_matrix is more efficient.
      SparseEfficiencyWarning)
    In [10]: M
    Out[10]: 
    <10x10 sparse matrix of type '<class 'numpy.float64'>'
        with 58 stored elements in Compressed Sparse Row format>
    

    设置这些元素会添加一堆non-zero 元素(即使值为0)。此分配不区分设置为 0 和设置为其他值。

    我们通过单独的步骤删除 0;

    In [11]: M.eliminate_zeros?
    Signature: M.eliminate_zeros()
    Docstring:
    Remove zero entries from the matrix
    
    This is an *in place* operation
    File:      /usr/local/lib/python3.5/dist-packages/scipy/sparse/compressed.py
    Type:      method
    In [12]: M.eliminate_zeros()
    In [13]: M
    Out[13]: 
    <10x10 sparse matrix of type '<class 'numpy.float64'>'
        with 8 stored elements in Compressed Sparse Row format>
    

    乘以 0 不会添加新值;它只是改变了现有的:

    In [16]: M[:,5:] *= 0
    In [17]: M
    Out[17]: 
    <10x10 sparse matrix of type '<class 'numpy.float64'>'
        with 20 stored elements in Compressed Sparse Row format>
    In [18]: M.data
    Out[18]: 
    array([0.89042028, 0.        , 0.        , 0.93756551, 0.34072221,
           0.3883514 , 0.        , 0.18581085, 0.        , 0.        ,
           0.        , 0.76948544, 0.        , 0.        , 0.        ,
           0.        , 0.90694047, 0.00354749, 0.        , 0.        ])
    In [19]: M.eliminate_zeros()
    In [20]: M
    Out[20]: 
    <10x10 sparse matrix of type '<class 'numpy.float64'>'
        with 8 stored elements in Compressed Sparse Row format>
    

    这应该可以避免大型矩阵中的内存错误。

    这些列上的矩阵乘积可能会更快 - 尽管它会生成一个新矩阵。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-05
      • 2018-06-14
      • 2020-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多