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

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

Error during curve_fit minimize, when approximating a function

问题

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

试图使用curve_fit来逼近函数以下是代码部分

```python
def rsk_alpha_function(x,a,b,c):
    return a* math.sqrt(x - b) + c
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。尝试其他函数逼近(如指数或多项式)时没有问题。

我一直遇到这个错误:

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

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

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

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

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

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

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

这次我会得到:

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

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


<details>
<summary>英文:</summary>

Am trying to approximate the function using the curve_fit: here are the code below.

```python
def rsk_alpha_function(x,a,b,c):
	return a* math.sqrt(x - b) + c
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:

Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  File &quot;C:\Users\Miniconda3\lib\site-packages\scipy\optimize\_minpack_py.py&quot;, line 859, in curve_fit     
    res = leastsq(func, p0, Dfun=jac, full_output=1, **kwargs)
  File &quot;C:\Users\Miniconda3\lib\site-packages\scipy\optimize\_minpack_py.py&quot;, line 413, in leastsq       
    shape, dtype = _check_func(&#39;leastsq&#39;, &#39;func&#39;, func, x0, args, n)
  File &quot;C:\Users\Miniconda3\lib\site-packages\scipy\optimize\_minpack_py.py&quot;, line 26, in _check_func    
    res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
  File &quot;C:\Users\Miniconda3\lib\site-packages\scipy\optimize\_minpack_py.py&quot;, line 501, in func_wrapped  
    return func(xdata, *params) - ydata
  File &quot;&lt;stdin&gt;&quot;, line 2, in risk_lambda_function
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:

def rsk_alpha_function(x,a,b,c):
    print(x)
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

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

This time I would get

  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  File &quot;C:\Users\Miniconda3\lib\site-packages\scipy\optimize\_minpack_py.py&quot;, line 859, in curve_fit     
    res = leastsq(func, p0, Dfun=jac, full_output=1, **kwargs)
  File &quot;C:\Users\Miniconda3\lib\site-packages\scipy\optimize\_minpack_py.py&quot;, line 413, in leastsq       
    shape, dtype = _check_func(&#39;leastsq&#39;, &#39;func&#39;, func, x0, args, n)
  File &quot;C:\Users\Miniconda3\lib\site-packages\scipy\optimize\_minpack_py.py&quot;, line 26, in _check_func    
    res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
  File &quot;C:\Users\Miniconda3\lib\site-packages\scipy\optimize\_minpack_py.py&quot;, line 501, in func_wrapped  
    return func(xdata, *params) - ydata
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.

import numpy as np

def rsk_alpha_function(x,a,b,c):
    return a * np.sqrt(x - b) + c
英文:

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

import numpy as np

def rsk_alpha_function(x,a,b,c):
    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:

确定