Piecewise linear regression with scikit-learn predictors

The notebook illustrates an implementation of a piecewise linear regression based on scikit-learn. The bucketization can be done with a DecisionTreeRegressor or a KBinsDiscretizer. A linear model is then fitted on each bucket.

Piecewise data

Let's build a toy problem based on two linear models.

Piecewise Linear Regression with a decision tree

The first example is done with a decision tree.

The method transform_bins returns the bucket of each variables, the final leave from the tree.

Let's try with more buckets.

Piecewise Linear Regression with a KBinsDiscretizer

The model does not enforce continuity despite the fast it looks like so. Let's compare with a constant on each bucket.


PR Model trees (M5P and co) and issue Model trees (M5P) propose an implementation a piecewise regression with any kind of regression model. It is based on Building Model Trees. It fits many models to find the best splits.