【问题标题】:error in scipy interp1dscipy interp1d 中的错误
【发布时间】:2018-10-30 15:21:03
【问题描述】:

我不明白 interp1d 报告的结果。我在应该收到号码的地方收到 NAN。

In [131]: bb
Out[131]: 
array([ 0.        ,  1.80286595,  1.87443683,  2.70410611,  3.02764722,
        3.11305985,  3.11534355,  3.18695351,  3.20693444])

In [132]: alphas1
Out[134]: 
array([  3.80918778e+00,   2.06547222e+00,   1.99234191e+00,
         7.55942418e-01,   2.56971574e-01,   1.05144676e-01,
         9.30852046e-02,   1.52574183e-02,   1.23664407e-07])

In [135]: bb.shape
Out[135]: (9,)

In [136]: alphas1.shape
Out[140]: (9,)

In [141]: pol = interp1d(alphas1, bb, bounds_error=False)

In [149]: pol(pol.x)
Out[149]: array([ nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan]) # I was expecting to receive nan only at the borders.

【问题讨论】:

  • 哪个版本?在scipy 0.14.0numpy 1.8.1中给出np.array([ 3.20693444, 3.18695351, ...的正确答案
  • 在 [25]: np.version.version 输出[29]: '1.8.1' 在 [30]: scipy.version.version 输出[44]: '0.13.3'跨度>
  • 更新scipy它可能会消失。
  • 谢谢它没有工作!

标签: python scipy


【解决方案1】:

我认为,如果您检查interp1d类的source code,即_check_bounds方法,可以看出问题:

def _check_bounds(self, x_new):

    ...

    below_bounds = x_new < self.x[0]
    above_bounds = x_new > self.x[-1]

    # !! Could provide more information about which values are out of bounds
    if self.bounds_error and below_bounds.any():
        raise ValueError("A value in x_new is below the interpolation "
            "range.")
    if self.bounds_error and above_bounds.any():
        raise ValueError("A value in x_new is above the interpolation "
            "range.")

该方法检查您尝试输入的 x 的值是否小于 self.x[0]x 的第一个元素(在您的情况下为 alphas1)。由于alphas1[0]x 列表中的最大 元素,因此之后的每个元素都将“超出范围”,即小于第一个元素。

解决此问题的方法是反转您的 xy 列表:

bb = bb[::-1]
alphas1 = alphas[::-1]
pol = interp1d(alphas1, bb, bounds_error=False)

现在alphas1 将增加,正如 scipy 预期的那样,pol(pol.x) 将按预期返回 bb(现在反转)。

【讨论】:

    猜你喜欢
    • 2016-01-05
    • 2016-12-25
    • 1970-01-01
    • 1970-01-01
    • 2016-11-01
    • 1970-01-01
    • 2021-07-28
    • 2015-06-23
    • 1970-01-01
    相关资源
    最近更新 更多