0
我想找到some_arg
值,將盡量減少功能。蟒蛇scipy蠻力優化
1)出口
from scipy import optimize
import math as m
2)計算原始2 * SIN(t)的數據
time_steps = list(range(0,20))
def my_sin(time_steps):
sin_data = list()
for time_step in time_steps:
sin_data.append(2*m.sin(time_step))
return sin_data
my_sin_data = my_sin(time_steps)
3)設定錯誤我希望儘量減少
def fun_to_brute(z, *params):
some_argument = z
sum_of_errors = 0
for time_step in params[0]:
sum_of_errors = sum_of_errors + some_argument*m.sin(time_step) - params[1][time_step]
return sum_of_errors
4)最後開始蠻力
rranges = [slice(-4, 4, 0.25)]
params = (time_steps, my_sin_data)
resbrute = optimize.brute(fun_to_brute, rranges, args = params, full_output=True)
但我得到的結果是錯誤的。我想蠻力使some_arg
值2,在這種情況下,sum_of_errors
_必須等於零。
但resbrute [0]會返回一些奇怪的值
爲什麼那裏是熊貓嗎?它只是無處不在... – Benjamin
忘記刪除 – Demaunt
嘗試添加'finish = None'到otpimize.brute – Xevaquor