正确使用scipy.optimize.leastsq

时间:2019-04-06 04:46:25

标签: python scipy curve-fitting least-squares

因此,根据对问题[python nonlinear least squares fitting

的回答

我修改了答案以估算三个参数kd,p0,l0

    N = 10
    kd_guess = 7.0  # <-- You have to supply a guess for kd
    p0_guess = 8.0
    l0_guess = 15.0
    p0 = np.linspace(0,10,N)
    l0 = np.linspace(0,10,N)

    PLP = func(4.0,5.0,6.0)+(np.random.random(N)-0.5)*2.0
    # The target should be (4.0,5.0,6.0)

    kd,p0,l0,cov = scp.optimize.leastsq(residuals,[kd_guess,p0_guess,l0_guess,PLP])

我想避免以下错误,

Traceback (most recent call last):
  File "Main.py", line 40, in <module>
    kd,p0,l0,cov = scp.optimize.leastsq(residuals,[kd_guess,p0_guess,l0_guess,PLP])
  File "/home/arvaldez/anaconda3/lib/python3.6/site-packages/scipy/optimize/minpack.py", line 380, in leastsq
    x0 = asarray(x0).flatten()
  File "/home/arvaldez/anaconda3/lib/python3.6/site-packages/numpy/core/numeric.py", line 501, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence.

1 个答案:

答案 0 :(得分:1)

这是一个使用scipy的curve_fit()例程的绘图示例,该例程调用了minimumsq()-我个人发现scipy curve_fit例程比minimumsq更易于使用。

function checkFactors(factors, num) {
   return factors.every(element => num % element === 0);
}
console.log(checkFactors([1, 2, 3, 8], 12)); 
console.log(checkFactors([1, 2], 2));
console.log(checkFactors([2, 4, 3, 6, 9], 12));
console.log(checkFactors([3, 5, 2, 6, 9], 15));
console.log(checkFactors([4, 2, 8, 1], 16));