scipy
scipy copied to clipboard
DOC: optimize: fix doc that curve_fit xdata should be float convertible.
Reference issue
Close #12632
What does this implement/fix?
As reported in #12632, optimize.curve_fit
converts xdata
to a float array if it is an array like object.
https://github.com/scipy/scipy/blob/8692c887ac341ace4183f115c55290ec9daf470b/scipy/optimize/minpack.py#L738-L744
However, the curve_fit
doc says:
https://github.com/scipy/scipy/blob/8692c887ac341ace4183f115c55290ec9daf470b/scipy/optimize/minpack.py#L549
I think this but can actually be any object
is confusing, because an error will be thrown when a string array is passed:
In [6]: from scipy.optimize import curve_fit
In [12]: xdata = np.linspace(0, 4, 5)
In [14]: ydata = func(xdata, 2.5, 1.3, 0.5)
In [15]: curve_fit(func, xdata, ydata)
Out[15]:
(array([2.5, 1.3, 0.5]),
array([[ 2.84539581e-32, -1.18219014e-32, -1.07893311e-32],
[-1.18219014e-32, 6.43888987e-32, 1.94671101e-32],
[-1.07893311e-32, 1.94671101e-32, 1.15640501e-32]]))
In [17]: xdata_str = np.array(["a", "b", "c", "d", "e"])
In [18]: curve_fit(func, xdata_str, ydata)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-18-da598f831819> in <cell line: 1>()
----> 1 curve_fit(func, xdata_str, ydata)
/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/scipy/optimize/_minpack_py.py in curve_fit(f, xdata, ydata, p0, sigma, absolute_sigma, check_finite, bounds, method, jac, **kwargs)
741 # non-array_like `xdata`.
742 if check_finite:
--> 743 xdata = np.asarray_chkfinite(xdata, float)
744 else:
745 xdata = np.asarray(xdata, float)
/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/numpy/lib/function_base.py in asarray_chkfinite(a, dtype, order)
623
624 """
--> 625 a = asarray(a, dtype=dtype, order=order)
626 if a.dtype.char in typecodes['AllFloat'] and not np.isfinite(a).all():
627 raise ValueError(
ValueError: could not convert string to float: 'a'
So I clarified the doc.
[skip azp] [skip actions]