[英]mutiple calls with same args to python function outputs different return values
This must be a quite basic misunderstanding from my part.从我的角度来看,这一定是一个非常基本的误解。 I have a function that returns an interpolation function based on random values.
我有一个 function 根据随机值返回插值 function。
fct = create_interp_fct(x,y)
Now fct is a callable interpolation function.现在fct是一个可调用的插值 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: function大致定义如下:
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.问题是,如果我再次调用fct([0,0,0,0,0,0]) ,我会得到不同的 output。 So it is evident that the function gets redefined based on new random values.
因此很明显,function 是根据新的随机值重新定义的。 My question is: how can I use fct so that it doesn't get redefined each time I call it?
我的问题是:我如何使用fct以便每次调用它时都不会重新定义它? Imagine the computation of fct is very expensive but to call it not.
想象一下fct的计算非常昂贵,但不能称之为。 How can I "save" my currently computed function?
如何“保存”我当前计算的 function? Or look at all the machine learning stuff from scikit for example(linear regression, etc...).
或者查看 scikit 中的所有机器学习内容(例如线性回归等)。 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?你不能说我每次调用 lr.score(M,n) 时都会重新计算线性回归?
The problem of non-deterministic output from a given instance of fct
is not replicated in the following example:以下示例中未复制来自给定
fct
实例的非确定性 output 问题:
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.我怀疑,在你的程序逻辑的某个地方,你已经在调用之间刷新了
fct
,通过再次调用fct = create_interp_fct(...)
而没有意识到它。 That will naturally re-randomize things.这自然会重新随机化事物。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.