votes up 1

Knots must be given for task=-1

Package:
scipy
github stars 8546
Exception Class:
TypeError

Raise code

    if not (-1 <= task <= 1):
        raise TypeError('task must be -1, 0 or 1')
    if (not len(w) == m) or (ipar == 1 and (not len(u) == m)):
        raise TypeError('Mismatch of input dimensions')
    if s is None:
        s = m - sqrt(2*m)
    if t is None and task == -1:
        raise TypeError('Knots must be given for task=-1')
    if t is not None:
        _parcur_cache['t'] = atleast_1d(t)
    n = len(_parcur_cache['t'])
    if task == -1 and n < 2*k + 2:
        raise TypeError('There must be at least 2*k+2 knots for task=-1')
    if m <= k:
        raise TypeError('m > k must hold')

Ways to fix

votes up 1 votes down

The parameter t can not be None for task=-1. This means if task=-1 then t must be given. More explanation about the function scipy.interpolate.splrep and its parameters is given in the documentation. You should check it out detailed use cases and valid values.

Let's see how to recreate and fix this exception.

  • Install scipy
$ pipenv install scipy

$ pipenv shell

import matplotlib.pyplot as plt


from scipy.interpolate import splev, splrep
x = np.linspace(0, 10, 10)
y = np.sin(x)
spl = splrep(x, y,task=-1,)

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-41-c01200e9a49b> in <module>()  4 x = np.linspace(0, 10, 10)  5 y = np.sin(x) ----> 6 spl = splrep(x, y,task=-1,) 
/usr/local/lib/python3.7/dist-packages/scipy/interpolate/fitpack.py in splrep(x, y, w, xb, xe, k, task, s, t, full_output, per, quiet)  289   290 """ --> 291 res = _impl.splrep(x, y, w, xb, xe, k, task, s, t, full_output, per, quiet)  292 return res  293  
/usr/local/lib/python3.7/dist-packages/scipy/interpolate/_fitpack_impl.py in splrep(x, y, w, xb, xe, k, task, s, t, full_output, per, quiet)  470 if task == -1:  471 if t is None: --> 472 raise TypeError('Knots must be given for task=-1')  473 numknots = len(t)  474 _curfit_cache['t'] = empty((numknots + 2*k + 2,), float) 
TypeError: Knots must be given for task=-1

Fixed version of the code:

import matplotlib.pyplot as plt


from scipy.interpolate import splev, splrep
x = np.linspace(0, 10, 10)
y = np.sin(x)
spl = splrep(x, y,task=-1,t=[1,2,3,4]) 
Oct 13, 2021 kellemnegasi answer
kellemnegasi 13.5k

Add a possible fix

Please authorize to post fix