python valueerror函数使用_Python:scipy.optimize.minimize在使用x和y args调用函数时失败并显示“ValueError:设置带序列的数组元素 . ”...

如 Headers 中所述,scipy.optimize.minimize失败并显示“ValueError:使用序列设置数组元素” . 当调用最小化时 .

我正在将scipy.optimize.minimize应用于使用变量coef(我正在优化的系数)和xData和yData(数据变量)的函数 .

我'll provide an example code below. I am aware through searching on how to use minimize that the error stems from the function being minimized returning an array when it should return a scalar. I'我不知道为什么它会返回一个数组 .

重要的是,scipy.optimize.least_squares可以工作,它似乎与scipy.optimize.minimize共享相同的语法 . scipy.optimize.fmin也不起作用,它也包括在内 - 它与Nelder-Mead方法的最小化相同,我正在调用它 .

这是一些在Python 3上有错误的通用示例代码:

import numpy as np

from scipy.optimize import least_squares

from scipy.optimize import minimize

from scipy.optimize import fmin

import matplotlib.pyplot as plt

xData = np.linspace(50,94,334);

yData = (xData-75)**2 + (np.random.random((334,))-.5)*600;

fun = lambda coef, x : coef[0] + coef[1]*x + coef[2]*x**2 ; #create a "lambda" function whatever that is that has a tuple for the polynomial coefficients in it

#function is y = coef0 + coef1*x + coef2*x^2 where y is lambda

funError = lambda coef, x, y: fun(coef,x) - y; #create a "lambda" function for the error between the real data y and the fit data y

#function is yError = y(coef,x) - yReal where yError is the lambda now

#expanded fully: yError = coef0 + coef1*x + coef2*x^2 - yReal

coef_init = (5,10,15); #initial coefficient guess

#coef0 is const (order 0)

#coef1 is order 1 coef

#coef2 is order 2 coef

coef = least_squares(funError,coef_init, args=(xData,yData) ); #calculate the polynomial coefficients to fit the data

yFit_lq = fun(coef.x,xData); #calc the guessed values

plt.figure();

plt.scatter( xData , yData , 20 , "r" );

plt.scatter( xData , yFit_lq , 20 );

plt.title("Least Squares");

plt.show();

coef = minimize(funError,coef_init, args=(xData,yData),method="Nelder-Mead" ); #calculate the polynomial coefficients to fit the data

yFit_min = fun(coef.x,xData); #calc the guessed values

plt.figure();

plt.scatter( xData , yData , 20 , "r" );

plt.scatter( xData , yFit_min , 20 );

plt.title("Minimize with Nelder-Mead");

plt.show();

coef = fmin(funError,coef_init, args=(xData,yData) ); #calculate the polynomial coefficients to fit the data

yFit_fmin = fun(coef.x,xData); #calc the guessed values

plt.figure();

plt.scatter( xData , yData , 20 , "r" );

plt.scatter( xData , yFit_fmin , 20 );

plt.title("fmin, equiv to min. w/ neldy");

plt.show();

我以相同的方式调用least_squares,minimize和fmin,他们的页面只需要args =() . 我不确定在调用minim和fmin时出现的错误是“ValueError:用一个序列设置一个数组元素” . 当least_squares对格式化完全满意时会发生错误 .

我也希望避免过多的函数defs - 干净简单的lambda函数应该能够处理这个简单的情况 .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值