【发布时间】:2020-07-08 19:24:45
【问题描述】:
我是 Python 新手,我在代码中遇到以下错误:
import numpy as np
A = np.empty([2, 2], dtype = float)
Z = np.empty([2, 2], dtype = float)
Z = [[2.0, 0.0], [0.0, 3.0]]
A = [[1, -1], [1, -2]]
print(np.divide(A, np.power(Z, 2)))
这些代码给出以下结果:
[[ 0.25 -inf], [inf -0.22222222]]
但我知道,使用计算器,正确答案应该是:
[[ 0.25 -0.25], [ 0.111111 -0.22222222]]
简而言之,这个操作给了我以下错误: RuntimeWarning:在 true_divide 中遇到除以零
【问题讨论】:
-
你是如何得到计算器的输出的?似乎计算器正在计算其他东西。
-
当您使用
Z = [[2.0, 0.0], [0.0, 3.0]]和A = [[1, -1], [1, -2]]时,您将A和Z设置为类型列表。我宁愿使用np.array([[...], [...]], dtype=float)初始化数组 -
嗨@MichaelRichardson。我听从了你的建议,但错误仍然存在。
标签: numpy matrix divide-by-zero