This must be a quite basic misunderstanding from my part. I have a function that returns an interpolation function based on random values.
fct = create_interp_fct(x,y)
Now fct is a callable interpolation function. For example:
fct([0,0,0,0,0,0])
returns
array([[ 0.75894378, 0.72761319, -0.23003647, -0.34790905, -0.51531125,
-0.91211147]])
The function is defined roughly as follows:
def create_interp_fct(x,y):
u,v = compute_some_random_values(x,y)
return RegularGridInterpolator(u,v) #from from scipy.interpolate
The problem is that if I call fct([0,0,0,0,0,0]) again I get a different output. So it is evident that the function gets redefined based on new random values. My question is: how can I use fct so that it doesn't get redefined each time I call it? Imagine the computation of fct is very expensive but to call it not. How can I "save" my currently computed function? Or look at all the machine learning stuff from scikit for example(linear regression, etc...). If I call
lr = lm.LinearRegression() # import sklearn.linear_model as lm
lr.fit(M,n)
you cannot say me that every time I call lr.score(M,n), the linear regression gets recomputed?
The problem of non-deterministic output from a given instance of fct
is not replicated in the following example:
In [13]: paste
import numpy
from scipy.interpolate import RegularGridInterpolator
def compute_some_random_values(shape):
return [numpy.arange(extent) for extent in shape], numpy.random.uniform(0, 1, size=shape)
def create_interp_fct(shape):
u,v = compute_some_random_values(shape)
return RegularGridInterpolator(u,v) #from from scipy.interpolate
## -- End pasted text --
In [24]: fct = create_interp_fct([2,3,4,5,6,7])
In [25]: fct([0,0,0,0,0,0])
Out[25]: array([0.24572906])
In [26]: fct([0,0,0,0,0,0])
Out[26]: array([0.24572906])
In [27]: fct([0,0,0,0,0,0])
Out[27]: array([0.24572906])
In [28]: fct([0,0,0,0,0,0])
Out[28]: array([0.24572906])
I suspect that, somewhere in your program logic, you have refreshed fct
between calls, by calling fct = create_interp_fct(...)
again without realizing it. That will naturally re-randomize things.
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.