TransWikia.com

how to create sklearn pipeline object using predtrained standardscalar object

Data Science Asked by Mangesh Divate on September 5, 2021

I am having pretrained Sklearn model and pre-trained Standard scalar object saved as pickle . And now I want to create Sklearn pipeline using both of it.

I need sklearn pipeline to convert it into ONNX format.

I couldnt do it as pipeline takes standard scalar class and then we need fit pipeline using data but in my case models and scalar both are already fitted.

One Answer

This works the way you would want out of the box.

pipeline takes standard scaler class

No, pipelines get initialized with estimator instances, not the classes. (This is why you need the parentheses in the steps, e.g. StandardScaler().)

That is, the following works:

from sklearn.datasets import load_breast_cancer
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline

X, y = load_breast_cancer(return_X_y=True)

scaler = StandardScaler()
lr = LogisticRegression()

X_sc = scaler.fit_transform(X)
lr.fit(X_sc, y)

pipe = Pipeline(steps=[('scale', scaler),
                       ('lr', lr)])

# Predicting would fail if the pipeline had unfitted estimators:
pipe.predict_proba(X)

Correct answer by Ben Reiniger on September 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP