指定范围scipy.optimize.brute()

2024-05-29 01:36:00 发布

您现在位置:Python中文网/ 问答频道 /正文

当我尝试使用scipy.minimize.brute()最小化函数时,请使用以下代码:

import scipy
scipy.optimize.brute(lambda x: x**2, ranges=(-2,3))

我得到以下错误:

^{pr2}$

我想这和范围的规格有关,但我不明白为什么。文件上说

Each component of the ranges tuple must be either a “slice object” or a range tuple of the form (low, high).

我的错误在哪里?在


Tags: ofthelambda函数代码import错误scipy
1条回答
网友
1楼 · 发布于 2024-05-29 01:36:00

如文件所述:

Each component of the ranges tuple must be either a “slice object” or a range tuple of the form (low, high).

因此,函数需要一个元组,每个维度的形式为(low, high)。你只有一个维度,所以在你的例子中正确的调用应该是

scipy.optimize.brute(lambda x: x**2, ranges=((-2,3),) )

相关问题 更多 >

    热门问题