对 NumPy 数组执行操作,但从这些操作中屏蔽沿对角线的值

发布于 2024-08-12 04:23:02 字数 462 浏览 3 评论 0原文

因为我可以对数组执行操作,所以对对角线不执行任何操作 进行计算,使得除对角线之外的所有响应都

array ([[0.,  1.37, 1.,   1.37, 1.,   1.37, 1.]
       [1.37, 0. ,  1.37, 1.73, 2.37, 1.73, 1.37]
       [1. ,  1.37, 0. ,  1.37, 2. ,  2.37, 2. ]
       [1.37, 1.73, 1.37, 0. ,  1.37, 1.73, 2.37]
       [1. ,  2.37, 2. ,  1.37, 0. ,  1.37, 2. ]
       [1.37, 1.73, 2.37, 1.73, 1.37, 0. ,  1.37]
       [1. ,  1.37, 2. ,  2.37, 2. ,  1.37, 0. ]])

避免出现 NaN 值,但在所有响应中保留对角线上的值零

as I can perform operations on arrays so that does nothing on the diagonal
is calculated such that all but the diagonal

array ([[0.,  1.37, 1.,   1.37, 1.,   1.37, 1.]
       [1.37, 0. ,  1.37, 1.73, 2.37, 1.73, 1.37]
       [1. ,  1.37, 0. ,  1.37, 2. ,  2.37, 2. ]
       [1.37, 1.73, 1.37, 0. ,  1.37, 1.73, 2.37]
       [1. ,  2.37, 2. ,  1.37, 0. ,  1.37, 2. ]
       [1.37, 1.73, 2.37, 1.73, 1.37, 0. ,  1.37]
       [1. ,  1.37, 2. ,  2.37, 2. ,  1.37, 0. ]])

to avoid the NaN value, but retained the value zero on the diagonal in all responses

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

要走干脆点 2024-08-19 04:23:02

我想知道掩码数组是否可以执行您想要的操作,例如,

import numpy as NP
A = NP.random.random_integers(0, 9, 16).reshape(4, 4)
dg = NP.r_[ [NP.nan] * 4 ]  # proper syntax is 'nan' not 'NaN'
dg = NP.diag(dg)
A += dg                     # a 4x4 array w/ NaNs down the main diagonal
NP.sum(A, axis=1)           # doesn't work, gives: array([ NaN,  NaN,  NaN,  NaN])  
from numpy import ma as MA
Am = **MA.masked_invalid**(A)
NP.sum(Am, axis=1)         # now it works (treats 'nan' as 0)

执行此操作的另一种方法当然是首先将 NaN 转换为 0,然后
屏蔽 0:

NP.nan_to_num(A)
MA.masked_equal(A, 0)

最后,一步屏蔽和转换 NaN 通常很有效:

MA.fix_invalid(A)

非常简单,只需记住“ma”可能尚未出现在您的命名空间中,而且这些函数处理“NaN”和“infs”,这通常是您想要的。

I wonder if masked arrays might do what you want, e.g.,

import numpy as NP
A = NP.random.random_integers(0, 9, 16).reshape(4, 4)
dg = NP.r_[ [NP.nan] * 4 ]  # proper syntax is 'nan' not 'NaN'
dg = NP.diag(dg)
A += dg                     # a 4x4 array w/ NaNs down the main diagonal
NP.sum(A, axis=1)           # doesn't work, gives: array([ NaN,  NaN,  NaN,  NaN])  
from numpy import ma as MA
Am = **MA.masked_invalid**(A)
NP.sum(Am, axis=1)         # now it works (treats 'nan' as 0)

The other way to do this of is, of course, to first convert the NaNs to 0s then
mask the 0s:

NP.nan_to_num(A)
MA.masked_equal(A, 0)

Finally, it's often efficient to mask and convert the NaNs in one step:

MA.fix_invalid(A)

Pretty straightforward, just keep in mind that 'ma' might not yet be in your namespace and also that these functions deal with 'NaNs' and 'infs', which is usually what you want.

萌逼全场 2024-08-19 04:23:02
>>> arr = [
... [0.,  1.37, 1.,   1.37, 1.,   1.37, 1.],
... [1.37, 0. ,  1.37, 1.73, 2.37, 1.73, 1.37],
... [1. ,  1.37, 0. ,  1.37, 2. ,  2.37, 2. ],
... [1.37, 1.73, 1.37, 0. ,  1.37, 1.73, 2.37],
... [1. ,  2.37, 2. ,  1.37, 0. ,  1.37, 2. ],
... [1.37, 1.73, 2.37, 1.73, 1.37, 0. ,  1.37],
... [1. ,  1.37, 2. ,  2.37, 2. ,  1.37, 0. ]
... ]
>>> for i in range(6):
...     for y in range(6):
...             if (i <> y):
...                     print arr[i][y]*arr[y][i]
...
1.8769
1.0
1.8769
1.0
1.8769
1.8769
1.8769
2.9929
5.6169
2.9929
1.0
1.8769
1.8769
4.0
5.6169
1.8769
2.9929
1.8769
1.8769
2.9929
1.0
5.6169
4.0
1.8769
1.8769
1.8769
2.9929
5.6169
2.9929
1.8769

取决于你需要计算什么

>>> arr = [
... [0.,  1.37, 1.,   1.37, 1.,   1.37, 1.],
... [1.37, 0. ,  1.37, 1.73, 2.37, 1.73, 1.37],
... [1. ,  1.37, 0. ,  1.37, 2. ,  2.37, 2. ],
... [1.37, 1.73, 1.37, 0. ,  1.37, 1.73, 2.37],
... [1. ,  2.37, 2. ,  1.37, 0. ,  1.37, 2. ],
... [1.37, 1.73, 2.37, 1.73, 1.37, 0. ,  1.37],
... [1. ,  1.37, 2. ,  2.37, 2. ,  1.37, 0. ]
... ]
>>> for i in range(6):
...     for y in range(6):
...             if (i <> y):
...                     print arr[i][y]*arr[y][i]
...
1.8769
1.0
1.8769
1.0
1.8769
1.8769
1.8769
2.9929
5.6169
2.9929
1.0
1.8769
1.8769
4.0
5.6169
1.8769
2.9929
1.8769
1.8769
2.9929
1.0
5.6169
4.0
1.8769
1.8769
1.8769
2.9929
5.6169
2.9929
1.8769

Depends on what you need to calculate

影子是时光的心 2024-08-19 04:23:02

正常计算然后

myarray[arange(len(array)), arange(len(array))] = 0.

Do your calculation as normal and then

myarray[arange(len(array)), arange(len(array))] = 0.
也只是曾经 2024-08-19 04:23:02

您可以像平常一样进行计算,然后将对角线设置回零吗?

Can you just do the calculation as normal, then afterwards set the diagonal back to zero?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文