Frequently Asked Questions
How to use Prince with sklearn pipelines?
Most Prince estimators consume and produce pandas DataFrames. PCA also accepts NumPy arrays, and all estimators with two-dimensional tabular input accept Polars DataFrames through Narwhals; see DataFrame support. In a sklearn pipeline, use sklearn’s set_output API to choose the DataFrame type exchanged between compatible steps.
from __future__ import annotations
from sklearn import datasets, impute, pipeline
import prince
pipe = pipeline.make_pipeline(impute.SimpleImputer(), prince.PCA())
pipe.set_output(transform="pandas")
dataset = datasets.load_iris()
pipe.fit_transform(dataset.data).head()
| 0 | 1 | |
|---|---|---|
| 0 | -2.264703 | 0.480027 |
| 1 | -2.080961 | -0.674134 |
| 2 | -2.364229 | -0.341908 |
| 3 | -2.299384 | -0.597395 |
| 4 | -2.389842 | 0.646835 |
