<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Building a pipeline
Step1:  Finding the best model
<ASSISTANT_TASK:>
Python Code:
%pylab inline
import sklearn
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_digits
from sklearn.pipeline import Pipeline
from sklearn.decomposition import PCA
digits = load_digits()
X_digits = digits.data
y_digits = digits.target
logistic = LogisticRegression()
pca = PCA()
pipe = Pipeline(steps=[('pca', pca), ('logistic', logistic)])
pipe.fit(X_digits, y_digits)
pipe.predict(X_digits[:1])
Explanation: Building a pipeline
End of explanation
from sklearn.grid_search import GridSearchCV
n_components = [20, 40, 64] # number of compomentens in PCA 
Cs = np.logspace(-4, 0, 3, 4) # Inverse of regularization strength
penalty = ["l1", "l2"] # Norm used by the Logistic regression penalization
class_weight = [None, "balanced"] # Weights associatied with clases
estimator = GridSearchCV(pipe,
                         {"pca__n_components": n_components,
                          "logistic__C": Cs,
                          "logistic__class_weight": class_weight,
                          "logistic__penalty": penalty
                         }, n_jobs=8, cv=5)
estimator.fit(X_digits, y_digits)
estimator.grid_scores_
print(estimator.best_score_)
print(estimator.best_params_)
Explanation: Finding the best model
End of explanation
<END_TASK>