【问题标题】:Why is np.sum not outputting anything?为什么 np.sum 没有输出任何东西?
【发布时间】:2020-09-07 19:31:57
【问题描述】:

我有两个大小相同的二维数组,我试图将一个除以另一个,然后在一个轴上求和? 但是,我有一些被零除的 nan,但是当我尝试对分割后的数组求平方,然后在其中一个轴上求和时,我只得到一个空白图:

east_rad = projections(np.real(inv_east_annual), 180)

rt_maha = projections(maha,180)

#dividing east_rad by rt_maha, rt_maha and east_rad both have shapes (1092, 180)

divide = east_rad/rt_maha

print(divide)

[[ 2.10160938e-17             nan -1.32855114e-02 ...             nan
  -1.24334979e-02             nan]
 [-2.59884274e-18 -6.19052022e-03 -1.11166036e-02 ... -1.27420254e-02
  -1.19839709e-02 -7.97509023e-03]
 [-1.34877182e-16 -5.03856340e-03 -5.29428667e-03 ... -1.80663486e-02
  -1.57691530e-02 -1.44670544e-02]


var = np.sum(divide**2,axis=0)

plt.plot(var)
plt.show()

我该如何解决这个问题?所以它实际上显示了一个情节......或者至少忽略了nans?

我也试过 np.where(divide == np.nan, divide, 0),但我只得到零!

【问题讨论】:

  • 你试过打印 var 吗?

标签: python arrays numpy scipy


【解决方案1】:

不确定在这种情况下除以零是什么意思,但你可以试试

divide = east_rad/rt_maha
divide[np.isnan(divide)] = 0

将你的 nans 归零以进行绘图。

【讨论】:

  • 在stackoverflow上我在哪里标记这个问题正确?
  • 不知道 :) 也是一个 stackoverflow 菜鸟。点击upvote按钮可能会让你这样做?
【解决方案2】:

您可以执行以下操作:

divsq = divide**2
np.sum(divsq, where = ~np.isnan(divsq), axis = 0)

在计算总和时忽略NaN 元素。

【讨论】:

    猜你喜欢
    • 2021-05-10
    • 1970-01-01
    • 2021-01-16
    • 2023-03-07
    • 2016-04-02
    • 2017-01-28
    • 2019-08-17
    • 2019-05-26
    • 1970-01-01
    相关资源
    最近更新 更多