0

我正在尝试优化我建立的模型的参数。它是一个非常简单的基于预测山区径流的模型。大学课程的一部分:

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
4

1 回答 1

0

scipy.optimize.fmin_cg的第一个参数——在你的代码中命名model——应该代表你想要最小化的函数(大概是你的模型估计的误差与给定的一组参数相比一些参考值......)。
具体来说,它应该是一个返回标量值的函数。
算法需要知道模型在给定参数集下的表现有多好。

于 2013-01-01T15:31:22.467 回答