对 NumPy 数组执行操作,但从这些操作中屏蔽沿对角线的值
因为我可以对数组执行操作,所以对对角线不执行任何操作 进行计算,使得除对角线之外的所有响应都
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想知道掩码数组是否可以执行您想要的操作,例如,
执行此操作的另一种方法当然是首先将 NaN 转换为 0,然后
屏蔽 0:
最后,一步屏蔽和转换 NaN 通常很有效:
非常简单,只需记住“ma”可能尚未出现在您的命名空间中,而且这些函数处理“NaN”和“infs”,这通常是您想要的。
I wonder if masked arrays might do what you want, e.g.,
The other way to do this of is, of course, to first convert the NaNs to 0s then
mask the 0s:
Finally, it's often efficient to mask and convert the NaNs in one step:
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.
取决于你需要计算什么
Depends on what you need to calculate
正常计算然后
Do your calculation as normal and then
您可以像平常一样进行计算,然后将对角线设置回零吗?
Can you just do the calculation as normal, then afterwards set the diagonal back to zero?