我正在尝试优化我建立的模型的参数。它是一个非常简单的基于预测山区径流的模型。大学课程的一部分:
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
我想更好地估计参数。雪比例和温度的形状为 (365,)
均方根误差:
将 numpy 导入为 np 导入 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