python scipy fmin未成功完成

nhn9ugyo  于 2022-11-09  发布在  Python
关注(0)|答案(1)|浏览(179)

我有一个函数,我试图最小化多个值。对于某些值,它成功终止,但对于其他值,错误

Warning: Maximum number of function evaluations has been exceeded.

是给出的错误。我不确定maxiter和maxfun的作用,以及如何增加或减少这些值以成功地达到最小值。我的理解是这些值是可选的,所以我不确定默认值是什么。


# create starting parameters, parameters equal to sin(x)

a = 1
k = 0
h = 0
wave_params = [a, k, h]

def wave_func(func_params):
    """This function calculates the difference between a sinewave (sin(x)) and raw_data (different sin wave)
    This is the function that will be minimized by modulating a, b, k, and h parameters in order to minimize
    the difference between curves."""
    a = func_params[0]
    b = 1
    k = func_params[1]
    h = func_params[2]

    y_wave = a * np.sin((x_vals-h)/b) + k
    error = np.sum((y_wave - raw_data) * (y_wave - raw_data))

    return error

wave_optimized = scipy.optimize.fmin(wave_func, wave_params)
btqmn9zl

btqmn9zl1#

您可以尝试使用scipy.optimize.minimize方法='Nelder-Mead' https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html
https://docs.scipy.org/doc/scipy/reference/optimize.minimize-neldermead.html#optimize-minimize-neldermead
那你就

minimum = scipy.optimize.minimize(wave_func, wave_params, method='Nelder-Mead')
n_function_evaluations = minimum.nfev
n_iterations = minimum.nit

或者您可以自定义搜索算法,如下所示:

minimum = scipy.optimize.minimize(
    wave_func, wave_params, method='Nelder-Mead',
    options={'maxiter': 10000, 'maxfev': 8000}
)

我对fmin一无所知,但我猜它的行为非常相似。

相关问题