gpt4 book ai didi

python - scipy.optimize.lsq_linear 边界错误

转载 作者:太空宇宙 更新时间:2023-11-04 00:53:13 28 4
gpt4 key购买 nike

我正在尝试计算以下公式:

http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.lsq_linear.html#scipy.optimize.lsq_linear

我有以下代码,但它不会接受 6 个单独变量的边界,它只会接受一个元组,这使得所有变量的边界都相同。

## cut benchmark out of data
benchmark_ser = data_df.ix[:,0]
## get out the stocks data
stocks_df = data_df.ix[:,1:]

## create/get bounds
bds = (0,.2) # <- i want to change this line
# to something that will provide
# a bound for each variable like
# [(0,.1),(0,.2),(0,.5),(0,.6)]

wgt_arr = sp.optimize.lsq_linear(stocks_df, benchmark_ser, bds)

我目前收到以下错误:

ValueError: 'bounds' must contain 2 elements.

谢谢

最佳答案

来自docs (强调我的):

bounds : 2-tuple of array_like, optional

Lower and upper bounds on independent variables. Defaults to no bounds. Each array must have shape (n,) or be a scalar, in the latter case a bound will be the same for all variables. Use np.inf with an appropriate sign to disable bounds on all or some variables.

因此,与其给它一个 (lower_bound, upper_bound) 元组列表,您需要一个包含两个类数组对象的元组:(1) 所有下限和 (2) 所有上限

尝试改变

bds = [(0,.1),(0,.2),(0,.5),(0,.6)]

bds = ([0., 0., 0., 0.], [.1, .2, .5, .6])

关于python - scipy.optimize.lsq_linear 边界错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36197272/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com