本文介绍了在scipy.sparse中将.data属性中的元素设置为零不愉快行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我将csr_matrix
的.data
中的值设置为零时,我得到了不愉快的行为.这是一个示例:
I getting unpleasant behavior when I set values in .data
of csr_matrix
to zero. Here is an example:
from scipy import sparse
a = sparse.csr_matrix([[0,0,2,0], [1,1,0,0],[0,3,0,0]])
输出:
>>> a.A
array([[0, 0, 2, 0],
[1, 1, 0, 0],
[0, 3, 0, 0]])
>>> a.data
array([2, 1, 1, 3])
>>> a.data[3] = 0 # setting one element to zero
>>> a.A
array([[0, 0, 2, 0],
[1, 1, 0, 0],
[0, 0, 0, 0]])
>>> a.data
array([2, 1, 1, 0]) # however, this zero is still considered part of data
# what I would like to see is:
# array([2, 1, 1])
>>> a.nnz # also `nnz` tells me that there 4 non-zero elements
# which is incorrect, I would like 3 as an output
4
>>> a.nonzero() # nonzero method does follow the behavior I expected
(array([0, 1, 1], dtype=int32), array([2, 0, 1], dtype=int32))
在上述情况下的最佳做法是什么?应该避免将.data
的元素设置为零吗? .nnz
是不可靠的方法来找到零个数吗?
What is the best practice in the above situation? Should setting elements of .data
to zero be avoided? Is .nnz
unreliable way find number of zeros?
推荐答案
scipy中的稀疏矩阵(至少CSC和CSR)具有 .eliminate_zeros()
方法来处理这种情况.运行
Sparse matrices in scipy (at least CSC and CSR) have an .eliminate_zeros()
method to handle this situations. Run
a.eliminate_zeros()
每次您弄乱a.data
时,它都应该照顾好它.
every time you mess with a.data
, and it should take care of it.
这篇关于在scipy.sparse中将.data属性中的元素设置为零不愉快行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!