【问题标题】:scipy interp1d extrapolation method fill_value = tuple not workingscipy interp1d 外推方法 fill_value = tuple 不起作用
【发布时间】:2018-04-02 04:51:21
【问题描述】:

我想推断一个函数拟合。 scipy.interpolate.interp1d 应该能够做到这一点(参见 doc sn-p)。 相反,我得到“ValueError:x_new 中的值低于插值范围。”

使用:python 2.7.12、numpy 1.13.3、scipy 0.19.1

fill_value:类数组或(类数组,类数组)或“外推”,可选 - 如果是ndarray(或float),这个值将用于填充for 请求的数据范围之外的点。如果没有提供,那么 默认值为 NaN。类数组必须正确广播到 非插值轴的尺寸。 - 如果是二元素元组,则第一个元素用作 x_new < x[0] 的填充值,第二个元素用于 x_new > x[-1]。任何不是 2 元素元组的东西(例如, list 或 ndarray,无论形状如何)被视为单个 用于两个边界的类似数组的参数 below, above = fill_value, fill_value.

import numpy as np
from scipy.interpolate import interp1d
# make a time series
nobs = 10
t = np.sort(np.random.random(nobs))
x = np.random.random(nobs)
# compute linear interp (with ability to extrapolate too)
f1 = interp1d(t, x, kind='linear', fill_value='extrapolate') # this works
f2 = interp1d(t, x, kind='linear', fill_value=(0.5, 0.6)) # this doesn't

【问题讨论】:

    标签: python scipy interpolation


    【解决方案1】:

    根据documentationinterp1d默认为ValueError外推,除了fill_value='extrapolate'或当您指定bounds_error=False

    In [1]: f1 = interp1d(t, x, kind='linear', fill_value=(0.5, 0.6), bounds_error=False)
    
    In [2]: f1(0)
    Out[2]: array(0.5)
    

    【讨论】:

    • 谢谢,克雷格。我假设向Fill_Value提供元组值会导致它被使用(也许除非您也出于某种原因明确地设置BRINES_ERROR = FALSE)。显然这不是Interp1d的行为。 span>
    猜你喜欢
    • 1970-01-01
    • 2016-01-05
    • 2016-12-25
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-05
    • 1970-01-01
    相关资源
    最近更新 更多