mojolearn.SplineTransformer#

class mojolearn.SplineTransformer(n_knots=5, degree=3, *, knots='uniform', extrapolation='constant', include_bias=True, order='C', handle_missing='error', sparse_output=False)[source]#

Bases: _PrepBase

Generate B-spline basis features for each feature.

Follows scikit-learn’s SplineTransformer. Each column is expanded into B-splines of degree on n_knots base knots, extended by degree knots at each end at the edge spacing (or wrapped, for periodic splines). The output is always a dense float32 array.

Parameters:
  • n_knots (int, default 5) – Number of base knots, at least 2 (ValueError otherwise). Ignored when knots is an array.

  • degree (int, default 3) – Polynomial degree of the splines, from 0 to 7 (ValueError otherwise).

  • knots ({‘uniform’, ‘quantile’} or array-like of shape (n_knots, n_features), default ‘uniform’) – 'uniform' spaces the knots evenly over the training range, 'quantile' places them at numpy’s linear percentiles, and an array gives them explicitly (sorted, without duplicates).

  • extrapolation ({‘constant’, ‘continue’, ‘linear’, ‘periodic’, ‘error’}, default ‘constant’) – Behavior outside the knot range. 'error' makes transform raise ValueError. 'periodic' requires degree < n_knots.

  • include_bias (bool, default True) – Keep the last spline of each feature; when False it is dropped.

  • order ({‘C’, ‘F’}, default ‘C’) – Memory order of the output array.

  • handle_missing ({‘error’, ‘zeros’}, default ‘error’) – 'error' raises ValueError on NaN; 'zeros' leaves NaN out of the knots and encodes it as a block of zeros.

  • sparse_output (bool, default False) – Must be False; True raises NotImplementedError.

Variables:
  • bsplines_ (list of arrays) – Per feature, the full knot vector of its splines (scikit-learn stores BSpline objects here).

  • n_features_in_ (int) – Number of columns seen in fit.

  • n_features_out_ (int) – Number of output columns.

  • numeric_mode_ (str) – Numeric mode the estimator was fitted in; transform runs in the same mode.

Notes

fit and fit_transform accept sample_weight (nonnegative), which weights the knot placement: the weighted percentile for 'quantile', and the range over rows of nonzero weight for 'uniform'. Fit needs at least two rows.

Examples

>>> import numpy as np, mojolearn
>>> X = np.random.default_rng(0).random((1000, 3), dtype=np.float32)
>>> Xt = mojolearn.SplineTransformer(n_knots=6, degree=3).fit_transform(X)
__init__(n_knots=5, degree=3, *, knots='uniform', extrapolation='constant', include_bias=True, order='C', handle_missing='error', sparse_output=False)[source]#

Methods

fit(X[, y, sample_weight])

fit_transform(X[, y, sample_weight])

Fit to X, then transform X.

get_params([deep])

Return the constructor parameters as a dict.

set_params(**params)

Set constructor parameters.

transform(X)

fit_transform(X, y=None, sample_weight=None)[source]#

Fit to X, then transform X.

Returns:

array – The transformed data.