在scipy.sparse中将.data属性中的元素设置为零不愉快行为 [英] Setting elements in .data attribute to zero unpleasant behaivor in scipy.sparse

查看:135
本文介绍了在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)具有

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属性中的元素设置为零不愉快行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆