分配后,稀疏矩阵变为密集矩阵 [英] Scipy sparse matrix become dense matrix after assignment

查看:116
本文介绍了分配后,稀疏矩阵变为密集矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

alpha = csr_matrix((1000,1000),dtype=np.float32)
beta = csr_matrix((1,1000),dtype=np.float32)
alpha[0,:] = beta

启动后,alpha和beta应该是稀疏矩阵,其中不存储任何元素.但是在将beta分配给alpha的第一行后,alpha变为非稀疏,在alpha中存储了1000个零.我知道我可以使用exclude_zeros()将Alpha转换回稀疏矩阵,但是还有更好的方法吗?

After initiation, alpha and beta should be sparse matrixes with no element stored there. But after assigning beta to the first row of alpha, alpha become non-sparse, with 1000 zeros stored in alpha. I know I can use eliminate_zeros() to turn alpha back to sparse matrix but is there any better way to do this?

推荐答案

当我复制您的步骤时,我会得到

When I copy your steps I get

In [131]: alpha[0,:]=beta
/usr/lib/python3/dist-packages/scipy/sparse/compressed.py:730: 
   SparseEfficiencyWarning: Changing the sparsity structure of a
   csr_matrix is expensive. lil_matrix is more efficient.
   SparseEfficiencyWarning)

这是您正在执行开发人员认为不明智的事情的第一个指标.

So that's the first indicator that you are doing something that the developers consider unwise.

我们可以深入研究csr __setitem__代码,但是我的猜测是它将beta转换为密,然后进行赋值.并且不会自动执行eliminate_zeros步骤(在分配期间或分配之后).

We could dig into the csr __setitem__ code, but my guess is that it is converting your beta to dense, and then doing the assignment. And isn't automatically doing the eliminate_zeros step (either during or after the assignment).

通常人们为什么会做a[...]=...?通常是建立稀疏矩阵.可以将非零值清零,但频率不足以将其视为特殊情况.

Normally why would people be doing a[...]=...? Usually it's to build the sparse matrix. Zeroing out non-zero values is possible, but not frequent enough to treat as a special case.

出于各种原因,在稀疏矩阵中可能有0个值.您甚至可以将0直接插入到alpha.data中.这就是为什么存在诸如eliminate_zerosprune之类的清理"方法的原因.即使nonzero也会应用!=0蒙版

It's possible for a variety of reasons to have 0 values in a sparse matrix. You could even insert the 0s into alpha.data directly. That's why there are 'cleanup' methods like eliminate_zeros and prune. Even nonzero applies a !=0 mask

    # convert to COOrdinate format
    A = self.tocoo()
    nz_mask = A.data != 0
    return (A.row[nz_mask],A.col[nz_mask])

在通常的稀疏实践中,您以coo或其他格式构建数据,然后转换为csr进行计算.矩阵乘法是它的强项.这构造了一个新的稀疏矩阵.可以修改csr,但不建议修改.

In normal sparse practice you build the data in coo or other format, and then convert to csr for calculations. Matrix multiplication is it's strong point. That constructs a new sparse matrix. Modification of a csr is possible, but not encouraged.

===================

====================

alpha.__setitem__??(在Ipython中)显示

alpha.__setitem__?? (in Ipython) shows

def __setitem__(self, index, x):
    # Process arrays from IndexMixin
    i, j = self._unpack_index(index)
    i, j = self._index_to_arrays(i, j)

    if isspmatrix(x):
        x = x.toarray()
    ....
    self._set_many(i, j, x.ravel())

所以是的,它会在分配之前将RHS转换为密集数组.

So yes, it converts the RHS to a dense array before doing the assignment.

这篇关于分配后,稀疏矩阵变为密集矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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