Pipeline: Data Preprocessing and Modeling

Data preprocessing and modeling is a standard workflow in machine learning. Scikit-learn provides a Pipeline utility to automate this process. Pipelines help prevent data leakage. Pipeline itself is treated like a merged estimator/algorithm.

Data leakage: Data preprocessing like Standardization on the entire test dataset before evaluating a model/algorithm will highly influence the result. Data preprocessing should be constrained to each fold of your cross-validation during model evaluation

In this example, we are working with:
Classification problem: Pima Indians Diabetes dataset
Data Preprocessing: Standardization using scikit-learn
Modeling: Linear Discriminant Analysis classification algorithm


This recipe includes the following topics:

  • Load classification problem dataset (Pima Indians diabetes) from github
  • Split columns into the usual feature columns(X) and target column(Y)
  • Data Preprocessing: Standardization using scikit-learn StandardScaler class
  • Modeling: Linear Discriminant Analysis (Classification Algorithm)
  • Create a pipeline of data preprocessing and modeling
  • Split data using KFold() class with K-fold count:10 and seed: 7
  • Evaluate the pipeline by calling cross_val_score() to run cross validation
  • Calculate mean from scores returned by cross_val_score()


# import modules
import pandas as pd
import numpy as np
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
from sklearn.preprocessing import StandardScaler
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.pipeline import Pipeline

# read data file from github
# dataframe: pimaDf
gitFileURL = 'https://raw.githubusercontent.com/andrewgurung/data-repository/master/pima-indians-diabetes.data.csv'
cols = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
pimaDf = pd.read_csv(gitFileURL, names = cols)

# convert into numpy array for scikit-learn
pimaArr = pimaDf.values

# Let's split columns into the usual feature columns(X) and target column(Y)
# Y represents the target 'class' column whose value is either '0' or '1'
X = pimaArr[:, 0:8]
Y = pimaArr[:, 8]

# set k-fold count
folds = 10

# set seed to reproduce the same random data each time
seed = 7

# initialize StandardScaler class
scaler = StandardScaler()

# initialize LinearDiscriminantAnalysis class
ldaModel = LinearDiscriminantAnalysis()

# create pipeline with data preprocessing and model
estimators = []
estimators.append(('standarize', scaler))
estimators.append(('lda', ldaModel))
model = Pipeline(estimators)

# split data using KFold
kfold = KFold(n_splits=folds, random_state=seed)

# Evaluate the pipeline by calling cross_val_score() to run cross validation
resultArr = cross_val_score(model, X, Y, cv=kfold)

# calculate mean of scores for all folds
meanAccuracy = resultArr.mean()

# display accuracy
print("Mean accuracy: %.5f" % meanAccuracy)
Mean accuracy: 0.77346

Leave a Reply

Your email address will not be published. Required fields are marked *