简体   繁体   English

scipy优化定义初始猜测的问题

[英]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.

相关问题 Scipy优化使用SLSQP最小化初始猜测 - Scipy optimize minimize initial guess using SLSQP scipy最接近初始猜测的优化解决方案 - scipy optimize solution closest to initial guess 使用Scipy.optimize方法='SLSQP'返回初始猜测 - Using Scipy.optimize method='SLSQP' returns initial guess Scipy优化最小化总是返回初始猜测(SLSQP) - Scipy optimize minimize always returns initial guess (SLSQP) 确定scipy.optimize的合理初始猜测的功能? - Function to determine a reasonable initial guess for scipy.optimize? Scipy.optimize.leastsq返回初始猜测而不是优化参数 - Scipy.optimize.leastsq returns the initial guess not optimization parameters pyOpt的Scipy.optimize.minimize的x0(初始猜测)是否相等? - Scipy.optimize.minimize's x0 (initial guess) equivalent for pyOpt? Python:如何防止 Scipy 的 optimize.minimize 函数改变初始猜测 x0 的形状? - Python: How to prevent Scipy's optimize.minimize function from changing the shape of initial guess x0? 为什么 scipy.optimize.curve_fit 重复评估初始猜测(并且可能代价高昂)? - Why is scipy.optimize.curve_fit evaluating the initial guess repeatedly (and probably costly)? Scipy优化返回初始猜测作为解决方案 - Scipy optimize return the innitial guess as solution
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM