英文:
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)
ydata
和zdata
被设置为从导入的csv文件中分别获取的df['y'].values
和df['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['y'].values
, df['z'].values
respectively from csv file that I imported. FYI, df['y'].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 "<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
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 < 0:
continue
else:
return a* math.sqrt(x - b) + c`
This time I would get
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'
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论