SplineTransformer¶
yohou.preprocessing.sklearn_wrappers.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.
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 such that first and last knots are the 1st percentile and 99th percentile of the data respectively. |
'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 True, then the last spline element inside each bin is dropped. |
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', 'missing-as-zero')
|
How to handle missing values during transform. If 'error', a ValueError is raised if missing values are present. If 'missing-as-zero', 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¶
Show/Hide source
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 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 | |