【问题标题】:Retrieve scipy minimize function lowest error检索 scipy 最小化函数最小错误
【发布时间】:2026-02-11 19:30:01
【问题描述】:

有没有办法在 scipy.minimize 收敛后直接检索最小化错误,或者必须直接将其编码到成本函数中?

我只能检索似乎收敛到的系数。

def errorFunction(params,series,loss_function,slen = 12):
    alpha, beta, gamma = params
    breakUps = int(len(series) / slen)
    end = breakUps * slen
    test = series[end:]
    errors = []

    for i in range(2,breakUps+1):
        model = HoltWinters(series=series[:i * 12], slen=slen,
                            alpha=alpha, beta=beta, gamma=gamma, n_preds=len(test))
        model.triple_exponential_smoothing()
        predictions = model.result[-len(test):]
        actual = test
        error = loss_function(predictions, actual)
        errors.append(error)
    return np.mean(np.array(errors))

opt = scipy.optimize.minimize(errorFunction, x0=x,
                   args=(train, mean_squared_log_error),
                   method="L-BFGS-B", bounds = ((0, 1), (0, 1), (0, 1))
                  )
#gets the converged values
optimal values = opt.x
#I would like to know what the error with errorFunction is when using opt.x values, without having to manually run the script again
#Is the minimum error stored somewhere in the returned object opt

【问题讨论】:

  • 您能添加您的代码吗?没有它很难帮助你:)
  • 刚刚添加了代码

标签: python scipy minimize


【解决方案1】:

根据我从函数scipy.optimize.minimize 的文档中了解到,结果作为OptimizeResult 对象返回。

根据此类 (here) 的文档,它有一个属性 fun,即“目标函数的值”。

所以如果你做opt.fun,你应该得到你正在寻找的结果。 (您可以检索更多值,例如 Jacobian opt.jac、Hessian opt.hess,...如文档中所述)

【讨论】: