如何使稀疏csc矩阵的多列中的所有元素为0

2024-10-02 14:23:56 发布

您现在位置:Python中文网/ 问答频道 /正文

my_csr_matrix

类型为“”的<;338232x1783504稀疏矩阵 以压缩稀疏列格式存储1574056个元素>

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'

Tags: inpyselflibpackageslocallinesite
1条回答
网友
1楼 · 发布于 2024-10-02 14:23:56
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>

这应该可以避免大矩阵中的内存错误。你知道吗

这些列上的矩阵积可能更快—尽管它生成了一个新的矩阵。你知道吗

相关问题 更多 >