Tag: divide by zero

我如何捕捉像是一个exception(不只是为了testing)的一个numpy警告?

我必须在Python中为一个项目制作拉格朗日多项式。 为了避免使用明确的for循环而不是牛顿的不同的风格,我正在做一个重心式的风格。 我遇到的问题是我需要通过零来捕捉一个除法,但是Python(或者可能是numpy)只是使它成为警告而不是正常的exception。 所以,我需要知道怎么做就是把这个警告看作是一个例外。 我在这个网站上find的相关问题不是以我需要的方式回答的。 这是我的代码: import numpy as np import matplotlib.pyplot as plt import warnings class Lagrange: def __init__(self, xPts, yPts): self.xPts = np.array(xPts) self.yPts = np.array(yPts) self.degree = len(xPts)-1 self.weights = np.array([np.product([x_j – x_i for x_j in xPts if x_j != x_i]) for x_i in xPts]) def __call__(self, x): warnings.filterwarnings("error") try: bigNumerator = […]