gpt4 book ai didi

python - scipy interp1d 外推法 fill_value = tuple 不工作

转载 作者:太空狗 更新时间:2023-10-30 02:38:29 25 4
gpt4 key购买 nike

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

使用:python 2.7.12、numpy 1.13.3、scipy 0.19.1

fill_value : array-like or (array-like, array_like) or "extrapolate", optional - if a ndarray (or float), this value will be used to fill in for requested points outside of the data range. If not provided, then the default is NaN. The array-like must broadcast properly to the dimensions of the non-interpolation axes. - If a two-element tuple, then the first element is used as a fill value for x_new < x[0] and the second element is used for x_new > x[-1]. Anything that is not a 2-element tuple (e.g., list or ndarray, regardless of shape) is taken to be a single array-like argument meant to be used for both bounds as 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

最佳答案

根据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)

关于python - scipy interp1d 外推法 fill_value = tuple 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46858877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com