How to create pipeline in sklearn

This recipe helps you create pipeline in sklearn. The purpose of the pipeline is to assemble several steps that can be cross-validated together while setting different parameters.

Recipe Objective - How to create a pipeline in sklearn?

A pipeline of transforms with a final estimator.

The purpose of the pipeline is to assemble several steps that can be cross-validated together while setting different parameters.

Apply a list of transforms and a final estimator. Intermediate steps of the pipeline must be ‘transforms’ which can be implemented with "fit" and "transform" methods.

The final estimator only needs to implement "fit". The transformers in the pipeline can be cached using the "memory" argument.

 

Links for the more related projects:-

https://www.projectpro.io/projects/data-science-projects/deep-learning-projects
https://www.projectpro.io/projects/data-science-projects/neural-network-projects

Example:-

Step:1 Import libraries

from sklearn.svm import SVC
# StandardScaler subtracts the mean from each features and then scale to unit variance.
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.pipeline import Pipeline

Step:2 Data Preparation

X, y = make_classification(random_state=0)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)

 

Step:3 Create Pipeline

pipeline = [('scaler', StandardScaler()), ('svc', SVC())]

# pipeline object
pipe = Pipeline(pipeline)
# The pipeline can be used as any other estimator
# and avoids leaking the test set into the train set
print(pipe.fit(X_train, y_train))
#
print(pipe.score(X_test, y_test))

Pipeline(steps=[('scaler', StandardScaler()), ('svc', SVC())])
0.88

scale = StandardScaler().fit(X_train)
X_train_scaled = scale.transform(X_train)
parameters = {'kernel':('linear', 'rbf'), 'C':[1, 10]}
grid = GridSearchCV(SVC(), param_grid=parameters, cv=5)
grid.fit(X_train_scaled, y_train)

GridSearchCV(cv=5, estimator=SVC(),
             param_grid={'C': [1, 10], 'kernel': ('linear', 'rbf')})

 

What Users are saying..

profile image

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

NLP Project for Multi Class Text Classification using BERT Model
In this NLP Project, you will learn how to build a multi-class text classification model using using the pre-trained BERT model.

NLP Project on LDA Topic Modelling Python using RACE Dataset
Use the RACE dataset to extract a dominant topic from each document and perform LDA topic modeling in python.

Build a Face Recognition System in Python using FaceNet
In this deep learning project, you will build your own face recognition system in Python using OpenCV and FaceNet by extracting features from an image of a person's face.

Detectron2 Object Detection and Segmentation Example Python
Object Detection using Detectron2 - Build a Dectectron2 model to detect the zones and inhibitions in antibiogram images.

A/B Testing Approach for Comparing Performance of ML Models
The objective of this project is to compare the performance of BERT and DistilBERT models for building an efficient Question and Answering system. Using A/B testing approach, we explore the effectiveness and efficiency of both models and determine which one is better suited for Q&A tasks.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

AWS MLOps Project to Deploy a Classification Model [Banking]
In this AWS MLOps project, you will learn how to deploy a classification model using Flask on AWS.

Deep Learning Project for Text Detection in Images using Python
CV2 Text Detection Code for Images using Python -Build a CRNN deep learning model to predict the single-line text in a given image.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

Deep Learning Project for Time Series Forecasting in Python
Deep Learning for Time Series Forecasting in Python -A Hands-On Approach to Build Deep Learning Models (MLP, CNN, LSTM, and a Hybrid Model CNN-LSTM) on Time Series Data.