question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SplineTransformer raises when passing knots explicity for n_knots=None

See original GitHub issue

On master,

import numpy as np

from sklearn.preprocessing import SplineTransformer

X = np.linspace(0, 1, 10)[:, None]
splt = SplineTransformer(
   knots=[[0], [1], [2], [3], [4]],
   n_knots=None,
   degree=3
)

splt.fit(X)

currently raises

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-86bffa93d84e> in <module>
     10 )
     11 
---> 12 splt.fit(X)

~/code/scikit-learn/sklearn/preprocessing/_polynomial.py in fit(self, X, y)
    226             isinstance(self.n_knots, numbers.Integral) and self.n_knots >= 2
    227         ):
--> 228             raise ValueError("n_knots must be a positive integer >= 2.")
    229 
    230         if isinstance(self.knots, str) and self.knots in [

ValueError: n_knots must be a positive integer >= 2.

IMO the n_knots argument should be allowed to be None (or any other value) if the knots are passed explicity.

cc: @lorentzenchr @ogrisel xref #18368 and #19483

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
lorentzenchrcommented, Mar 21, 2021

If knots in ["uniform", "quantile"], then n_knots specifies the number if knots. If knots=2d array specifying the knot positions manually, then n_knots is ignored. So they can’t be collapsed, but that would have been nice!

1reaction
mlondschiencommented, Mar 19, 2021

Currently

import numpy as np

from sklearn.preprocessing import SplineTransformer

X = np.linspace(0, 1, 10)[:, None]
splt = SplineTransformer(
   knots=[[0], [1], [2], [3], [4]],
   n_knots=2,
   degree=3
)

splt.fit(X)

works. IMO this is “more incorrect” than n_knots=None.

Raising an error that n_knots needs to be in integer when knots is passed explicitly is confusing. This suggests that the value in n_knots get’s used somewhere. I would generally pass n_knots=None explicitly to make sure that the argument does not get used.

Read more comments on GitHub >

github_iconTop Results From Across the Web

sklearn.preprocessing.SplineTransformer
Number of knots of the splines if knots equals one of {'uniform', 'quantile'}. Must be larger or equal 2. Ignored if knots is...
Read more >
ENH Add periodic extrapolation to SplineTransformer #19483
Maybe, one should more clearly state, that the periodicity is given either by the manually specified knots or by the range of the...
Read more >
Basis Expansions and Smoothing Splines - Branch Energy
The points at which one cubic polynomial hands over to the next are called knots. For this work, we're going to use symfit,...
Read more >
Proper terminology for what happens at knots in a cubic spline ...
This answer on math.stackexchange.com suggests one way to proceed. In particular: The typical mathematical definition of "smooth" says ...
Read more >
Time-related feature engineering with scikit-learn - Neuraxle
SplineTransformer class and its extrapolation=”periodic” option. ... we pass the list of categorical values explicitly to use a logical order when encoding ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found