问题
I have a very large datafile, where x= time and y= distance. I would like to figure out what the speed is in different segments. Ideally, I would like Python to calculate the segments and the corresponding linear regression functions. I googled this and think my best option is using the numpy.piecewise to get segmented linear regression.
I only keep getting this error
# Remove full_output from kwargs, otherwise we're passing it in twice'.
The code is use is as follows:
y = cleandata["Distance (um)"]
def piecewise_linear(x, x0, x1, b, k1, k2, k3):
condlist = [x < x0, (x >= x0) & (x < x1), x >= x1]
funclist = [lambda x: k1*x + b, lambda x: k1*x + b + k2*(x-x0), lambda x: k1*x + b + k2*(x-x0) + k3*(x - x1)]
return np.piecewise(x, condlist, funclist)
p , e = sp.optimize.curve_fit(piecewise_linear, x, y)
xd = np.linspace(0, 1429228.0, 285845)
plt.plot(x, y, "o")
plt.plot(xd, piecewise_linear(xd, *p))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-60-a059f3aec3cb> in <module>
6 return np.piecewise(x, condlist, funclist)
7
----> 8 p , e = sp.optimize.curve_fit(piecewise_linear, x, y)
9
10 xd = np.linspace(0, 1429228.0, 285845)
~/opt/anaconda3/lib/python3.7/site-packages/scipy/optimize/minpack.py in curve_fit(f, xdata, ydata, p0, sigma, absolute_sigma, check_finite, bounds, method, jac, **kwargs)
761 # Remove full_output from kwargs, otherwise we're passing it in twice.
762 return_full = kwargs.pop('full_output', False)
--> 763 res = leastsq(func, p0, Dfun=jac, full_output=1, **kwargs)
764 popt, pcov, infodict, errmsg, ier = res
765 ysize = len(infodict['fvec'])
~/opt/anaconda3/lib/python3.7/site-packages/scipy/optimize/minpack.py in leastsq(func, x0, args, Dfun, full_output, col_deriv, ftol, xtol, gtol, maxfev, epsfcn, factor, diag)
386 if not isinstance(args, tuple):
387 args = (args,)
--> 388 shape, dtype = _check_func('leastsq', 'func', func, x0, args, n)
389 m = shape[0]
390
~/opt/anaconda3/lib/python3.7/site-packages/scipy/optimize/minpack.py in _check_func(checker, argname, thefunc, x0, args, numinputs, output_shape)
24 def _check_func(checker, argname, thefunc, x0, args, numinputs,
25 output_shape=None):
---> 26 res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
27 if (output_shape is not None) and (shape(res) != output_shape):
28 if (output_shape[0] != 1):
~/opt/anaconda3/lib/python3.7/site-packages/scipy/optimize/minpack.py in func_wrapped(params)
461 if transform is None:
462 def func_wrapped(params):
--> 463 return func(xdata, *params) - ydata
464 elif transform.ndim == 1:
465 def func_wrapped(params):
<ipython-input-60-a059f3aec3cb> in piecewise_linear(x, x0, x1, b, k1, k2, k3)
4 condlist = [x < x0, (x >= x0) & (x < x1), x >= x1]
5 funclist = [lambda x: k1*x + b, lambda x: k1*x + b + k2*(x-x0), lambda x: k1*x + b + k2*(x-x0) + k3*(x - x1)]
----> 6 return np.piecewise(x, condlist, funclist)
7
8 p , e = sp.optimize.curve_fit(piecewise_linear, x, y)
<__array_function__ internals> in piecewise(*args, **kwargs)
~/opt/anaconda3/lib/python3.7/site-packages/numpy/lib/function_base.py in piecewise(x, condlist, funclist, *args, **kw)
615 raise ValueError(
616 "with {} condition(s), either {} or {} functions are expected"
--> 617 .format(n, n, n+1)
618 )
619
ValueError: with 1 condition(s), either 1 or 2 functions are expected
I don't understand the error and have no idea what is wrong with this code.
This is my first try with three segments. Eventually I would like to use more segments.
Anyone knows what's wrong with this code?
来源:https://stackoverflow.com/questions/63814704/keep-getting-this-error-using-numpy-piecewise-to-get-segmented-linear-regression