Python: numpy 和 scipy minimize: 设置一个带有序列最小化的数组元素

Python: numpy and scipy minimize: setting an array element with a sequence minimize

我正在尝试最小化该函数,但得到一个

ValueError: setting an array element with a sequence.

在以下代码中:

import numpy as np
from scipy import optimize as opt

def f(x):
    return np.sin(x / 5.) * np.exp(x / 10.) + 5 * np.exp( -x / 2.)

aprox_0 = np.array([range(1, 10), range(11, 20), range(21, 30)])
min_0 = opt.minimize(f, aprox_0[0])

有人能帮忙吗?

检查函数 f() 中 x 的维度及其在最小化过程中的返回值,当您发现问题时,函数 flatten() 可能会有所帮助。