<jupyter_start><jupyter_text>Machine learning is not always about applying a single machine learning algorithm. For a lot of machine learning applications, you will need to apply various data processing steps, data transformations, and potentially multiple machine learning algorithms. This can lead to a lot of code. The question becomes, how do you keep your code organized and as bug free as possible? In this video, I'll share with you how can use pipelines in scikit-learn to make your code cleaner and more resilient to bugs. To demonstrate the utility of pipelines, this notebook shows how much less code you need to chain together pca and logistic regression for image classification.  Import Libraries<jupyter_code>%matplotlib inline

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

from sklearn.pipeline import Pipeline

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

from sklearn.decomposition import PCA
from sklearn.linear_model import LogisticRegression<jupyter_output><empty_output><jupyter_text>Load the DatasetThe dataset is a modified version of the MNIST dataset that contains 2000 labeled images of each digit 0 and 1. The images are 28 pixels by 28 pixels. Parameters | Number--- | ---Classes | 2 (digits 0 and 1)Samples per class | 2000 samples per classSamples total | 4000Dimensionality | 784 (28 x 28 images)Features | integers values from 0 to 255For convenience, I have arranged the data into csv file.<jupyter_code>df = pd.read_csv('data/MNISTonly0_1.csv')
df.head()<jupyter_output><empty_output><jupyter_text>Without PipelineNotice how many steps this takes. There are quite a few places where an error could occur.<jupyter_code># Train Test Split
X_train, X_test, y_train, y_test = train_test_split(df[df.columns[:-1]], df['label'], random_state=0)

# Standardize Data
scaler = StandardScaler()
scaler.fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)

# Apply PCA
pca = PCA(n_components = .90, random_state=0)
pca.fit(X_train)
X_train = pca.transform(X_train)
X_test = pca.transform(X_test)

# Apply Logistic Regression
clf = LogisticRegression()
clf.fit(X_train, y_train)

# Get Model Performance
print(clf.score(X_test, y_test))<jupyter_output><empty_output><jupyter_text>With Pipelines<jupyter_code># Train Test Split
X_train, X_test, y_train, y_test = train_test_split(df[df.columns[:-1]], df['label'], random_state=0)

# Create a pipeline
pipe = Pipeline([('scaler', StandardScaler()),
                 ('pca', PCA(n_components = .90, random_state=0)),
                 ('logistic', LogisticRegression())])

pipe.fit(X_train, y_train)

# Get Model Performance
print(pipe.score(X_test, y_test))<jupyter_output><empty_output><jupyter_text>Visualize Pipeline<jupyter_code>from sklearn import set_config

set_config(display='diagram')
pipe<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08133150308853809, 'fraction_numerical': 0.015099519560741249, 'mean_word_length': 5.907582938388626, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '589011', 'license': 'MIT', 'n_tokens_mistral': 929, 'n_tokens_neox': 875, 'n_words': 306, 'path': 'scikitlearn/Ex_Files_ML_SciKit_Learn/Exercise Files/03_05_Pipelines.ipynb', 'repo_name': 'aswinjose89/MachineLearningTutorial', 'size': '2914'}