<jupyter_start><jupyter_text>Pipeline Usage<jupyter_code>from sklearn.pipeline import Pipeline
from sklearn.svm import SVC
from sklearn.decomposition import PCA
estimators = [('reduce_dim', PCA()), ('clf', SVC())]
pipe = Pipeline(estimators)
pipe 

from sklearn.pipeline import make_pipeline
from sklearn.naive_bayes import MultinomialNB
from sklearn.preprocessing import Binarizer
make_pipeline(Binarizer(), MultinomialNB()) 
pipe.steps[0]  
pipe[0]  
pipe['reduce_dim']  
pipe.named_steps.reduce_dim is pipe['reduce_dim']
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

from sklearn import datasets
from sklearn.decomposition import PCA
from sklearn.linear_model import SGDClassifier
from sklearn.pipeline import Pipeline
from sklearn.model_selection import GridSearchCV

# Define a pipeline to search for the best combination of
# PCA truncation and classifier regularization
logistic = SGDClassifier(loss = 'log', penalty = 'l2', 
                       early_stopping=True, max_iter=10000,
                       tol = 1e-5, random_state = 0)

pca = PCA()
pipe = Pipeline(steps = [('pca', pca), ('logistic', logistic)])

digits = datasets.load_digits()
X_digits = digits.data
y_digits = digits.target

# Parameters of pipelines can be set using
# '__' separated parameter names:
param_grid = {
    'pca__n_components' : [5, 20, 30, 40, 50, 64],
    'logistic__alpha': np.logspace(-4, 4, 5),
}
search = GridSearchCV(pipe, param_grid, iid = False, cv = 5)
search.fit(X_digits, y_digits)
print('Best parameter (CV score = %0.3f)' % search.best_score_)
print(search.best_params_)<jupyter_output>Best parameter (CV score = 0.915)
{'logistic__alpha': 0.01, 'pca__n_components': 64}
{'fraction_non_alphanumeric': 0.1056338028169014, 'fraction_numerical': 0.02171361502347418, 'mean_word_length': 5.046099290780142, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '432627', 'license': 'MIT', 'n_tokens_mistral': 606, 'n_tokens_neox': 559, 'n_words': 159, 'path': 'pipeline/Pipeline.ipynb', 'repo_name': 'yanfeit/MLbook', 'size': '1704'}