我正在尝试优化我构建的模型的参数。它是一个非常简单的模型预测山的水径流。大学课程的一部分:
def model(params, snowProportion,temperature):
'''
Calculates predicted runoff.
'''
K = params[0]
p = params[1]
tempThresh = params[2]
meltDays = np.where(temperature > tempThresh)[0]
accum = snowProportion*0.
for d in meltDays:
water = K * snowProportion[d]
n = np.arange(len(snowProportion)) - d
m = p ** n
m[np.where(n<0)]=0
accum += m * water
np.savetxt('2005predicted.dat', accum)
params = [2000, 0.96, 9]
我被告知要使用scipy.optimize.fmin_cg;
所以我认为我做了一些事情:
x = scipy.optimize.fmin_cg(model, params, args=[snowProportion, temperature])
我不断遇到错误:
TypeError: 'numpy.ndarray' object is not callable
所以我假设我需要他们在列表中 - 但我遇到了同样的问题:
TypeError: 'list' object is not callable
我想更好地估计这些参数。 SnowProportion和温度的形状(365,)
RMSE:
将numpy导入为np import scipy.optimize
def RMSE(params,temperature, snowProportion):
'''
Calculates the RMSE of a model from measured and predicted.
'''
measured = np.loadtxt('/home/david/Documents/HydroM/runoff2005.dat')
K = params[0]
p = params[1]
tempThresh = params[2]
meltDays = np.where(temperature > tempThresh)[0]
predicted = snowProportion*0.
for d in meltDays:
water = K * snowProportion[d]
n = np.arange(len(snowProportion)) - d
m = p ** n
m[np.where(n<0)]=0
predicted += m * water
err = np.sqrt((measured - predicted) ** 2).mean()
return err
答案 0 :(得分:0)
scipy.optimize.fmin_cg的第一个参数 - 在您的代码中命名为model
- 应该代表您想要最小化的函数(可能是您的模型估计的错误与给定的集合参数与某些参考值相比 ...)
具体来说,它应该是返回标量值的函数
算法需要知道模型在给定的一组参数下的“良好”程度。