[英]Problem with scipy optimize defining initial guess
I guess I'm not right at defining x0
(Initial guess)我想我不
x0
定义x0
(最初的猜测)
With minimize
from scipy.optimize
I looking for min of my fun error
and I want 𝑤0
to be in bettween [-100,100] and 𝑤1
in [-5, 5].使用
scipy.optimize
minimize
,我寻找我的有趣error
最小值,我希望𝑤0
介于 [-100,100] 和𝑤1
[-5, 5]。 Starting point– ( 𝑤0 , 𝑤1 ) = (0, 0).起点– ( 𝑤0 , 𝑤1 ) = (0, 0)。
Function功能
def error(w0, w1):
dataset = data
total_error = 0
for i in range(1, 10):
total_error = total_error + (dataset['Height'][i] - (w0 + w1 * dataset['Weight'][i]))**2
return total_error
Problem问题
bnds = ((-100, 100), (-5, 5))
x0 = (0), (0)
res = minimize(error, (0), (0), method = "L-BFGS-B", bounds = bnds)
ValueError: length of x0 != length of bounds ValueError:x0 的长度!= 边界的长度
WTF跆拳道
print (len(x0))
print (len(bnds))
2 2 2 2
Hey i reproduced your error, i had also some issues here.嘿,我重现了您的错误,我在这里也遇到了一些问题。 But i think i found the main problem:
但我想我发现了主要问题:
from scipy.optimize import minimize
import numpy as np
def error(w0,w1):
dataset = np.array([1,2,3,4,5,6,777,7,77,7,7,7])
total_error = 0
for i in range(1, 10):
total_error = total_error + (dataset[i] - (w0+ w1 * dataset[i]))**2
return total_error
x0 = np.array([[12],[22]])
res =minimize(error, x0[0],x0[1], method = "L-BFGS-B",bounds=[[0,10]])
You just can choose 2 boundarys, because your optimization problem is 2 dimensional.您只能选择 2 个边界,因为您的优化问题是二维的。 You define your boundaries for your "x-axis" over the input Data points, and you just can maybe wrapp or crop the output to your boundary "bnds".
您可以在输入数据点上为“x 轴”定义边界,并且您可以将输出包装或裁剪到边界“bnds”。 Hope i could help
希望我能帮上忙
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.