稀疏矩阵的元素除法,忽略0/0 [英] Elementwise division of sparse matrices, ignoring 0/0

查看:149
本文介绍了稀疏矩阵的元素除法,忽略0/0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个稀疏矩阵E和D,它们在相同位置具有非零条目.现在,我想将E/D作为稀疏矩阵,仅在D不为零的情况下进行定义.

I have two sparse matrices E and D, which have non-zero entries at the same places. Now I want to have E/D as a sparse matrix, defined only where D is non-zero.

例如,采用以下代码:

import numpy as np
import scipy

E_full = np.matrix([[1.4536000e-02, 0.0000000e+00, 0.0000000e+00, 1.7914321e+00, 2.6854320e-01, 4.1742600e-01, 0.0000000e+00],
                    [9.8659000e-03, 0.0000000e+00, 0.0000000e+00, 1.9106752e+00, 5.7283640e-01, 1.4840370e-01, 0.0000000e+00],
                    [1.3920000e-04, 0.0000000e+00, 0.0000000e+00, 9.4346500e-02, 2.8285900e-02, 4.3967800e-02, 0.0000000e+00],
                    [0.0000000e+00, 4.5182676e+00, 0.0000000e+00, 0.0000000e+00, 7.3000000e-06, 1.5100000e-05, 4.0746900e-02],
                    [0.0000000e+00, 0.0000000e+00, 3.4002088e+00, 4.6826200e-02, 0.0000000e+00, 2.4246900e-02, 3.4529236e+00]])
D_full = np.matrix([[0.36666667, 0.        , 0.        , 0.33333333, 0.2       , 0.1       , 0.        ],
                    [0.23333333, 0.        , 0.        , 0.33333333, 0.4       , 0.03333333, 0.        ],
                    [0.06666667, 0.        , 0.        , 0.33333333, 0.4       , 0.2       , 0.        ],
                    [0.        , 0.63636364, 0.        , 0.        , 0.04545455, 0.03030303, 0.28787879],
                    [0.        , 0.        , 0.33333333, 0.33333333, 0.        , 0.22222222, 0.11111111]])
E = scipy.sparse.dok_matrix(E_full)
D = scipy.sparse.dok_matrix(D_full)

然后除法E/D产生一个完整的矩阵.

Then division E/D yields a full matrix.

matrix([[3.96436360e-02,            nan,            nan, 5.37429635e+00, 1.34271600e+00, 4.17426000e+00,            nan],
        [4.22824292e-02,            nan,            nan, 5.73202566e+00, 1.43209100e+00, 4.45211145e+00,            nan],
        [2.08799990e-03,            nan,            nan, 2.83039503e-01, 7.07147500e-02, 2.19839000e-01,            nan],
        [           nan, 7.10013476e+00,            nan,            nan, 1.60599984e-04, 4.98300005e-04, 1.41541862e-01],
        [           nan,            nan, 1.02006265e+01, 1.40478601e-01,            nan, 1.09111051e-01, 3.10763127e+01]])

我还尝试了其他包装.

import sparse
sparse.COO(E) / sparse.COO(D)

这让我出错了.

ValueError: Performing this operation would produce a dense result: <ufunc 'true_divide'>

因此它也会尝试创建一个密集矩阵.

So it tries to create a dense matrix as well.

我了解这是由于0/0 = nan这一事实.但无论如何,我对这些价值观不感兴趣.那么如何避免计算它们?

I understand this is due to the fact that 0/0 = nan. But I am not interested in these values anyway. So how can I avoid computing them?

推荐答案

更新 :(受sacul启发)创建一个空的dok_matrix并仅使用nonzero修改D的非零部分. (这对于dok_matrix以外的稀疏矩阵也应适用.)

Update: (Inspired by sacul) Create an empty dok_matrix and modify only D's nonzero part with nonzero. (This should work for sparse matrices other than dok_matrix as well.)

F = scipy.sparse.dok_matrix(E.shape)
F[D.nonzero()] = E[D.nonzero()] / D[D.nonzero()]


您可以尝试 update +


You can try the update + nonzero method for dok_matrix.

nonzero_idx = [tuple(l) for l in np.transpose(D.nonzero())]
D.update({k: E[k]/D[k] for k in nonzero_idx})

首先,我们使用nonzero来确定矩阵D中不为0的索引.然后,将索引放入提供字典的update方法中

First, we use nonzero to nail down the indices in the matrix D that is not 0. Then, we put the indices in the update method where we supply a dictionary

{k: E[k]/D[k] for k in nonzero_idx}

,以便D中的值将根据此字典进行更新.

such that the values in D will be updated according to this dictionary.

说明:

D.update({k: E[k]/D[k] for k in nonzero_idx})的作用是

for k in {k: E[k]/D[k] for k in nonzero_idx}.keys():
    D[k] = E[k]/D[k]

请注意,这会更改D的位置.如果要创建新的稀疏矩阵而不是在原位置修改D,请将D复制到另一个矩阵,例如ret.

Note that this changes D in place. If you want to create a new sparse matrix rather than modifying D in place, copy D to another matrix, say ret.

nz = [tuple(l) for l in np.transpose(D.nonzero())]
ret = D.copy()
ret.update({k: E[k]/D[k] for k in nz})

这篇关于稀疏矩阵的元素除法,忽略0/0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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