I have to use an older scipy version, which can not extrapolate. In scipy 1.1.0 the extrapolation is working as expected, giving proper results. Since I cannot use this in my application I want to use numpy polyfit. but the results are different, while interpolation from scipy and bisect results are the same..
class InterExtraPolate(object):
def __init__(self, x_list, y_list):
if any(y - x <= 0 for x, y in zip(x_list, x_list[1:])):
raise ValueError("x_list must be in strictly ascending order!")
self.x_list = map(float, x_list)
self.y_list = map(float, y_list)
def __getitem__(self, x):
from scipy import interpolate
f=interpolate.interp1d(self.x_list,self.y_list, fill_value='extrapolate')
return f(x)
class npInterExraPolate(object):
def __init__(self, x_list, y_list):
import numpy as np
if any(y - x <= 0 for x, y in zip(x_list, x_list[1:])):
raise ValueError("x_list must be in strictly ascending order!")
self.x_list = map(float, x_list)
self.y_list = map(float, y_list)
self.fit=np.polyfit(self.x_list,self.y_list, 1)
def __getitem__(self, x):
import numpy as np
f=np.poly1d(self.fit)
return f(x)
##Main
ie = InterExtraPolate([1, 2.5, 3.4, 5.8, 6], [2, 4, 5.8, 4.3, 4])
npie = npInterExraPolate([1, 2.5, 3.4, 5.8, 6], [2, 4, 5.8, 4.3, 4])
my_xl = [0.5,1.1,6.3]
print ie[my_xl]
print npie[my_xl]
==== How can I make the numpy polyfit/poly1d results equal to the scipy extrapolate results?