将scipy.interpolate.interp1d包装成一个函数

问题描述 投票:0回答:1

我正在努力将scipy.interpolate.interp1d包装成一个函数。我的输入pd.df:

In [108]: rates.tail()
Out[108]: 
               28      91      182     364
Date                                      
2017-12-18  0.0125  0.0138  0.0151  0.0169
2017-12-19  0.0125  0.0137  0.0151  0.0170
2017-12-20  0.0122  0.0138  0.0151  0.0171
2017-12-21  0.0120  0.0135  0.0154  0.0172
2017-12-22  0.0114  0.0133  0.0154  0.0172

这有效,但需要将它包装成一个以date为参数的函数:

x = np.array(rates.columns[0:4])
y = np.array(rates.loc[date])
f = interp1d(x, y, kind='cubic', fill_value='extrapolate')

感谢任何帮助!

scipy python-3.6
1个回答
1
投票

似乎函数应该采用日期和数值(在下面称为t)。所以它可能是这样的:

def interpolated(t, date): 
    x = np.array(rates.columns[0:4])
    y = np.array(rates.loc[date])
    f = interp1d(x, y, kind='cubic', fill_value='extrapolate')
    return f(t)

(如果将治疗作为一个参数而不是从全球范围内获取,那么也可以通过rates)。

© www.soinside.com 2019 - 2024. All rights reserved.