I am trying to interpolate on 2D-data but keep getting the error could not convert string to float: 'extrapolate'
. I am pretty sure that the value for which I get the error is not outside the interval I used for the interpolation.
I just post my specific example because in order to help me with the error you don't need to understand the certain function I used.
from scipy.interpolate import interp2d
import numpy as np
import scipy.stats as si
import math as m
def C_prime_function(M,v):
return si.norm.cdf(M/v + v/2) - m.exp(-M)*si.norm.cdf(M/v - v/2)
C_prime_function_vectorized = np.vectorize(C_prime_function)
x_coarse = np.arange(0.000001,5.5,0.5)
y_coarse = np.arange(-0.5,0.6,0.1)
v_coarse, M_coarse = np.meshgrid(x_coarse,y_coarse)
C_prime_true = C_prime_function_vectorized(M_coarse,v_coarse)
f = interp2d(v_coarse,M_coarse,C_prime_true,kind='linear',fill_value='extrapolate')
print(f(0.000001,0.5))
So I thought for fill_value = 'extrapolate'
Python would automatically extrapolate data that is near/outside the interval. But even for fill_value = 'nan'
(which I read should lead to an automatic extrapolation), this doesn't work.
Greetings
Felix
Try this instead
f = interp2d(v_coarse,M_coarse,C_prime_true,kind='linear',bounds_error = False)
your code then prints
[0.39339549]
fill_value
should either be omitted or a number, not a string, per this
And one more thing -- if you want to explicitly pass the default value to fill-value
it is not fill_value = 'nan'
but fill_value = None
, which is the way to pass null values in Python (whereas 'nan'
is just a string). You still need bounds_error = False
though
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.