曲线拟合最小化过程中出现错误,当近似一个函数。

huangapple go评论76阅读模式
英文:

Error during curve_fit minimize, when approximating a function

问题

Here is the translated code section without the need for further translation:

  1. 试图使用curve_fit来逼近函数以下是代码部分
  2. ```python
  3. def rsk_alpha_function(x,a,b,c):
  4. return a* math.sqrt(x - b) + c
  5. popt, pcov = curve_fit(risk_alpha_function, ydata, zdata, maxfev = 5000)

ydatazdata被设置为从导入的csv文件中分别获取的df['y'].valuesdf['z'].values。值得一提的是,df['y'].values范围从0.01到0.9。尝试其他函数逼近(如指数或多项式)时没有问题。

我一直遇到这个错误:

  1. Traceback (most recent call last):
  2. File "<stdin>", line 1, in <module>
  3. File "C:\Users\Miniconda3\lib\site-packages\scipy\optimize\_minpack_py.py", line 859, in curve_fit
  4. res = leastsq(func, p0, Dfun=jac, full_output=1, **kwargs)
  5. File "C:\Users\Miniconda3\lib\site-packages\scipy\optimize\_minpack_py.py", line 413, in leastsq
  6. shape, dtype = _check_func('leastsq', 'func', func, x0, args, n)
  7. File "C:\Users\Miniconda3\lib\site-packages\scipy\optimize\_minpack_py.py", line 26, in _check_func
  8. res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
  9. File "C:\Users\Miniconda3\lib\site-packages\scipy\optimize\_minpack_py.py", line 501, in func_wrapped
  10. return func(xdata, *params) - ydata
  11. File "<stdin>", line 2, in risk_lambda_function
  12. TypeError: only size-1 arrays can be converted to Python scalars

我不明白为什么一直会遇到这个错误。

为了调试目的,我尝试了一些其他方法,所以尝试了:

  1. def rsk_alpha_function(x,a,b,c):
  2. print(x)
  3. return [a*math.sqrt(x_i - b) + c for x_i in i]

但这次我会得到"math domain error"错误。

所以我尝试忽略了x_i - b为负数的情况,其中:

  1. def rsk_alpha_function(x,a,b,c):
  2. for x_i in x:
  3. if x_i - b < 0:
  4. continue
  5. else:
  6. return a* math.sqrt(x - b) + c

这次我会得到:

  1. File "<stdin>", line 1, in <module>
  2. File "C:\Users\Miniconda3\lib\site-packages\scipy\optimize\_minpack_py.py", line 859, in curve_fit
  3. res = leastsq(func, p0, Dfun=jac, full_output=1, **kwargs)
  4. File "C:\Users\Miniconda3\lib\site-packages\scipy\optimize\_minpack_py.py", line 413, in leastsq
  5. shape, dtype = _check_func('leastsq', 'func', func, x0, args, n)
  6. File "C:\Users\Miniconda3\lib\site-packages\scipy\optimize\_minpack_py.py", line 26, in _check_func
  7. res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
  8. File "C:\Users\Miniconda3\lib\site-packages\scipy\optimize\_minpack_py.py", line 501, in func_wrapped
  9. return func(xdata, *params) - ydata
  10. TypeError: unsupported operand type(s) for -: 'NoneType' and 'float'

我认为初始错误在一定程度上通过按照链接中的方法解决了(如果不是,请告诉我),但我不明白为什么一直发生这些其他错误。

  1. <details>
  2. <summary>英文:</summary>
  3. Am trying to approximate the function using the curve_fit: here are the code below.
  4. ```python
  5. def rsk_alpha_function(x,a,b,c):
  6. return a* math.sqrt(x - b) + c
  7. popt, pcov = curve_fit(risk_alpha_function, ydata, zdata, maxfev = 5000)

ydata and zdata are set as df[&#39;y&#39;].values, df[&#39;z&#39;].values respectively from csv file that I imported. FYI, df[&#39;y&#39;].values range from 0.01 to 0.9. Has no problem with trying with other function approximation like exponential or polynomial.

I keep getting this error:

  1. Traceback (most recent call last):
  2. File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  3. File &quot;C:\Users\Miniconda3\lib\site-packages\scipy\optimize\_minpack_py.py&quot;, line 859, in curve_fit
  4. res = leastsq(func, p0, Dfun=jac, full_output=1, **kwargs)
  5. File &quot;C:\Users\Miniconda3\lib\site-packages\scipy\optimize\_minpack_py.py&quot;, line 413, in leastsq
  6. shape, dtype = _check_func(&#39;leastsq&#39;, &#39;func&#39;, func, x0, args, n)
  7. File &quot;C:\Users\Miniconda3\lib\site-packages\scipy\optimize\_minpack_py.py&quot;, line 26, in _check_func
  8. res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
  9. File &quot;C:\Users\Miniconda3\lib\site-packages\scipy\optimize\_minpack_py.py&quot;, line 501, in func_wrapped
  10. return func(xdata, *params) - ydata
  11. File &quot;&lt;stdin&gt;&quot;, line 2, in risk_lambda_function
  12. TypeError: only size-1 arrays can be converted to Python scalars

I don't understand why I am keep getting this error.

I tried to refer to some other method for debugging purpose.
so tried:

  1. def rsk_alpha_function(x,a,b,c):
  2. print(x)
  3. return [a*math.sqrt(x_i - b) + c for x_i in i]

But this time I would get error: math domain error

So I tried to ignore the case where x_i - b is negative, where

  1. def rsk_alpha_function(x,a,b,c):
  2. for x_i in x:
  3. if x_i - b &lt; 0:
  4. continue
  5. else:
  6. return a* math.sqrt(x - b) + c`

This time I would get

  1. File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  2. File &quot;C:\Users\Miniconda3\lib\site-packages\scipy\optimize\_minpack_py.py&quot;, line 859, in curve_fit
  3. res = leastsq(func, p0, Dfun=jac, full_output=1, **kwargs)
  4. File &quot;C:\Users\Miniconda3\lib\site-packages\scipy\optimize\_minpack_py.py&quot;, line 413, in leastsq
  5. shape, dtype = _check_func(&#39;leastsq&#39;, &#39;func&#39;, func, x0, args, n)
  6. File &quot;C:\Users\Miniconda3\lib\site-packages\scipy\optimize\_minpack_py.py&quot;, line 26, in _check_func
  7. res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
  8. File &quot;C:\Users\Miniconda3\lib\site-packages\scipy\optimize\_minpack_py.py&quot;, line 501, in func_wrapped
  9. return func(xdata, *params) - ydata
  10. TypeError: unsupported operand type(s) for -: &#39;NoneType&#39; and &#39;float&#39;

I think the initial error is somehow solved by following the link's method?(if not please tell me) but I can't get why these other errors are keep happening.

答案1

得分: 2

You need to work with a vectorized function, replace math.sqrt by np.sqrt the numpy equivalent.

  1. import numpy as np
  2. def rsk_alpha_function(x,a,b,c):
  3. return a * np.sqrt(x - b) + c
英文:

You need to work with a vectorized function, replace math.sqrt by np.sqrt the numpy equivalent.

  1. import numpy as np
  2. def rsk_alpha_function(x,a,b,c):
  3. return a* np.sqrt(x - b) + c

huangapple
  • 本文由 发表于 2023年5月22日 20:50:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76306381.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定