SplineTransformer¶
yohou.preprocessing.SplineTransformer
¶
Bases: SklearnTransformer
Generate univariate B-spline bases for features.
Generate a new feature matrix consisting of n_splines=n_knots + degree - 1
spline basis functions (B-splines) of polynomial order degree for each
feature (n_splines=n_knots - 1 when extrapolation='periodic').
This is a Yohou wrapper that preserves the polars DataFrame structure and "time" column.
Parameters ¶
| Name | Type | Description | Default |
|---|---|---|---|
n_knots
|
int
|
Number of knots of the splines if |
5
|
degree
|
int
|
The polynomial degree of the spline basis. Must be a non-negative integer. |
3
|
knots
|
('uniform', 'quantile')
|
Set knot positions. For 'uniform', knots are distributed uniformly from the minimum to the maximum of the training features. For 'quantile', knots are spaced along the quantiles of the features. An array-like sets the knot positions explicitly. |
'uniform'
|
extrapolation
|
('error', 'constant', 'linear', 'continue', 'periodic')
|
If 'error', values outside the min and max values of the training features will raise an error. |
'error'
|
include_bias
|
bool
|
If False, then the last spline element inside the data range of each feature is dropped. Set to True (default) to keep it and include a redundant basis column. |
True
|
order
|
('C', 'F')
|
Order of output array. |
'C'
|
sparse_output
|
bool
|
If True, transform will return sparse CSC format. Otherwise, transform will return dense array. |
False
|
handle_missing
|
('error', 'zeros')
|
How to handle missing values during transform. If 'error', a ValueError is raised if missing values are present. If 'zeros', missing values are treated as zeros in the spline basis. |
'error'
|
Attributes ¶
| Name | Type | Description |
|---|---|---|
instance_ |
SplineTransformer
|
The fitted sklearn SplineTransformer instance. |
bsplines_ |
list of shape (n_features,)
|
List of BSplines objects, one for each feature. |
n_features_out_ |
int
|
Number of output features. |
Examples ¶
>>> import polars as pl
>>> from datetime import datetime
>>> from yohou.preprocessing import SplineTransformer
>>> X = pl.DataFrame({
... "time": [datetime(2024, 1, i) for i in range(1, 11)],
... "value": [float(i) for i in range(10)],
... })
>>> spline = SplineTransformer(n_knots=4, degree=3)
>>> spline.fit(X)
SplineTransformer(...)
>>> X_spline = spline.transform(X)
>>> # Generates spline basis features
>>> len(X_spline.columns) > len(X.columns)
True
See Also ¶
PolynomialFeatures : Generate polynomial and interaction features.
Source Code ¶
Source code in src/yohou/preprocessing/sklearn_wrappers.py
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 | |