|
24 | 24 | from sklearn.linear_model import LogisticRegression
|
25 | 25 | from sklearn.pipeline import Pipeline
|
26 | 26 | from sklearn.model_selection import GridSearchCV
|
27 |
| - |
| 27 | +from sklearn.preprocessing import StandardScaler |
28 | 28 |
|
29 | 29 | # Define a pipeline to search for the best combination of PCA truncation
|
30 | 30 | # and classifier regularization.
|
31 | 31 | pca = PCA()
|
| 32 | +# Define a Standard Scaler to normalize inputs |
| 33 | +scaler = StandardScaler() |
| 34 | + |
32 | 35 | # set the tolerance to a large value to make the example faster
|
33 | 36 | logistic = LogisticRegression(max_iter=10000, tol=0.1)
|
34 |
| -pipe = Pipeline(steps=[("pca", pca), ("logistic", logistic)]) |
| 37 | +pipe = Pipeline(steps=[("scaler", scaler), ("pca", pca), ("logistic", logistic)]) |
35 | 38 |
|
36 | 39 | X_digits, y_digits = datasets.load_digits(return_X_y=True)
|
37 |
| - |
38 | 40 | # Parameters of pipelines can be set using ‘__’ separated parameter names:
|
39 | 41 | param_grid = {
|
40 |
| - "pca__n_components": [5, 15, 30, 45, 64], |
| 42 | + "pca__n_components": [5, 15, 30, 45, 60], |
41 | 43 | "logistic__C": np.logspace(-4, 4, 4),
|
42 | 44 | }
|
43 | 45 | search = GridSearchCV(pipe, param_grid, n_jobs=2)
|
|
0 commit comments