Exercises
Pipelines
3 tasks. Write the code, press Check, and the page runs it against a real Python interpreter.
Exercise 1Passed
Chain a scaler and a logistic regression into one model, and print its accuracy.
Python needs scikit-learn, downloaded on first run
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=0, stratify=y
)
# build a pipeline, fit it, print the score rounded to three places
make_pipeline takes the steps in order.
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=0, stratify=y
)
model = make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000))
model.fit(X_train, y_train)
print(round(model.score(X_test, y_test), 3))Exercise 2Passed
The scaler is fitted outside the folds, which leaks. Put it inside a pipeline instead.
Python needs scikit-learn, downloaded on first run
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import cross_val_score
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
X, y = load_breast_cancer(return_X_y=True)
leaked = StandardScaler().fit_transform(X)
scores = cross_val_score(LogisticRegression(max_iter=5000), leaked, y, cv=5)
print(round(scores.mean(), 3))Pass the pipeline to cross_val_score and the raw X.
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import cross_val_score
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline
X, y = load_breast_cancer(return_X_y=True)
model = make_pipeline(StandardScaler(), LogisticRegression(max_iter=5000))
scores = cross_val_score(model, X, y, cv=5)
print(round(scores.mean(), 3))Exercise 3Passed
Search three values of C on the pipeline, and print the best one.
Python needs scikit-learn, downloaded on first run
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
X, y = load_breast_cancer(return_X_y=True)
model = Pipeline([
("scale", StandardScaler()),
("classify", LogisticRegression(max_iter=5000)),
])
# search classify__C over 0.01, 1.0 and 100.0, then print the best
Double underscore reaches into a named step.
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
X, y = load_breast_cancer(return_X_y=True)
model = Pipeline([
("scale", StandardScaler()),
("classify", LogisticRegression(max_iter=5000)),
])
search = GridSearchCV(model, {"classify__C": [0.01, 1.0, 100.0]}, cv=5)
search.fit(X, y)
print(search.best_params_["classify__C"])