[英]mutiple calls with same args to python function outputs different return values
从我的角度来看,这一定是一个非常基本的误解。 我有一个 function 根据随机值返回插值 function。
fct = create_interp_fct(x,y)
现在fct是一个可调用的插值 function。 例如:
fct([0,0,0,0,0,0])
返回
array([[ 0.75894378, 0.72761319, -0.23003647, -0.34790905, -0.51531125,
-0.91211147]])
function大致定义如下:
def create_interp_fct(x,y):
u,v = compute_some_random_values(x,y)
return RegularGridInterpolator(u,v) #from from scipy.interpolate
问题是,如果我再次调用fct([0,0,0,0,0,0]) ,我会得到不同的 output。 因此很明显,function 是根据新的随机值重新定义的。 我的问题是:我如何使用fct以便每次调用它时都不会重新定义它? 想象一下fct的计算非常昂贵,但不能称之为。 如何“保存”我当前计算的 function? 或者查看 scikit 中的所有机器学习内容(例如线性回归等)。 如果我打电话
lr = lm.LinearRegression() # import sklearn.linear_model as lm
lr.fit(M,n)
你不能说我每次调用 lr.score(M,n) 时都会重新计算线性回归?
以下示例中未复制来自给定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])
我怀疑,在你的程序逻辑的某个地方,你已经在调用之间刷新了fct
,通过再次调用fct = create_interp_fct(...)
而没有意识到它。 这自然会重新随机化事物。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.