0

我想找到最小的函数值scipy.optimize.minimize_scalar

功能:

def error(w0, w1):
    dataset = data
    total_error = 0
    for i in range(1, 25000):
        meta = dataset['Height'][i] - ((w0 + w1 * dataset['Weight'][i]))**2
        total_error = total_error + meta
    return total_error

我想要w0 = 50并且w1 = [-5,5] 当我试图将函数置于 scipy 方法下时,我看到了不同的错误:

res = minimize_scalar(error)
TypeError: error() missing 1 required positional argument: 'w1'
w0 = 50
w1 = 0
res = minimize_scalar(error (w0, w1))
'numpy.float64' object is not callable
w0 = 50
w1 = range(-5,5)
res = minimize_scalar(error, w0, w1)
TypeError: object of type 'int' has no len()
4

1 回答 1

1

优化问题的关键是区分变量及其用途。此外,在调用函数时参考文档并使用正确的变量标签是明智的。根据文档,这应该可以解决问题:

from scipy.optimize import minimize_scalar

def error(x, w0):
    dataset = data
    total_error = 0
    for i in range(1, 25000):
        meta = dataset['Height'][i] - ((w0 + x * dataset['Weight'][i]))**2
        total_error = total_error + meta
    return total_error

res = minimize_scalar(fun=error, bounds=(-5, 5), args=(50,))
于 2020-02-12T13:27:41.327 回答