votes up 1

n_knots must be a positive integer >= 2.

Package:
Exception Class:
ValueError

Raise code

            isinstance(self.degree, numbers.Integral) and self.degree >= 0
        ):
            raise ValueError("degree must be a non-negative integer.")

        if not (
            isinstance(self.n_knots, numbers.Integral) and self.n_knots >= 2
        ):
            raise ValueError("n_knots must be a positive integer >= 2.")

        if isinstance(self.knots, str) and self.knots in [
            "uniform",
            "quantile",
        ]:
            base_knots = self._get_base_knot_positions(
                X, n_knots=self.n_knots, knots=self.knots
😲 Agile task management is now easier than calling a taxi. #Tracklify
🙏 Scream for help to Ukraine
Today, 25th May 2022, Russia continues bombing and firing Ukraine. Don't trust Russia, they are bombing us and brazenly lying in same time they are not doing this 😠, civilians and children are dying too! We are screaming and asking exactly you to help us, we want to survive, our families, children, older ones.
Please spread the information, and ask your governemnt to stop Russia by any means. We promise to work extrahard after survival to make the world safer place for all.

Ways to fix

votes up 1 votes down

Caused by a negative value of n_knots.

Reproducing and fixing this error is shown below.

How to reproduce it.

$ pipenv install scikit-learn numpy

$ pipenv shell

import numpy as np
from sklearn.preprocessing import SplineTransformer
X = np.arange(6).reshape(6, 1)
spline = SplineTransformer(degree=2, n_knots=-3)
spline.fit_transform(X)

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-2-70f5ff9f8e34> in <module>()  3 X = np.arange(6).reshape(6, 1)  4 spline = SplineTransformer(degree=2, n_knots=-3) ----> 5 spline.fit_transform(X) 
/usr/local/lib/python3.7/dist-packages/sklearn/base.py in fit_transform(self, X, y, **fit_params)  845 if y is None:  846 # fit method of arity 1 (unsupervised transformation) --> 847 return self.fit(X, **fit_params).transform(X)  848 else:  849 # fit method of arity 2 (supervised transformation) 
/usr/local/lib/python3.7/dist-packages/sklearn/preprocessing/_polynomial.py in fit(self, X, y, sample_weight)  776 if not (isinstance(self.n_knots, numbers.Integral) and self.n_knots >= 2):  777 raise ValueError( --> 778 f"n_knots must be a positive integer >= 2, got: {self.n_knots}"  779 )  780  
ValueError: n_knots must be a positive integer >= 2, got: -3

Fix:

import numpy as np
from sklearn.preprocessing import SplineTransformer
X = np.arange(6).reshape(6, 1)
spline = SplineTransformer(degree=2, n_knots=3)
spline.fit_transform(X)

Oct 28, 2021 kellemnegasi answer
kellemnegasi 29.0k

Add a possible fix

Please authorize to post fix