|
26 | 26 |
|
27 | 27 | from sklearn import datasets |
28 | 28 | from sklearn.decomposition import PCA |
29 | | -from sklearn.linear_model import SGDClassifier |
| 29 | +from sklearn.linear_model import LogisticRegression |
30 | 30 | from sklearn.pipeline import Pipeline |
31 | 31 | from sklearn.model_selection import GridSearchCV |
32 | 32 |
|
33 | 33 |
|
34 | 34 | # Define a pipeline to search for the best combination of PCA truncation |
35 | 35 | # and classifier regularization. |
36 | | -logistic = SGDClassifier(loss='log', penalty='l2', early_stopping=True, |
37 | | - max_iter=10000, tol=1e-5, random_state=0) |
38 | 36 | pca = PCA() |
| 37 | +# set the tolerance to a large value to make the example faster |
| 38 | +logistic = LogisticRegression(max_iter=10000, tol=0.1) |
39 | 39 | pipe = Pipeline(steps=[('pca', pca), ('logistic', logistic)]) |
40 | 40 |
|
41 | 41 | X_digits, y_digits = datasets.load_digits(return_X_y=True) |
42 | 42 |
|
43 | 43 | # Parameters of pipelines can be set using ‘__’ separated parameter names: |
44 | 44 | param_grid = { |
45 | 45 | 'pca__n_components': [5, 20, 30, 40, 50, 64], |
46 | | - 'logistic__alpha': np.logspace(-4, 4, 5), |
| 46 | + 'logistic__C': np.logspace(-4, 4, 5), |
47 | 47 | } |
48 | | -search = GridSearchCV(pipe, param_grid) |
| 48 | +search = GridSearchCV(pipe, param_grid, n_jobs=-1) |
49 | 49 | search.fit(X_digits, y_digits) |
50 | 50 | print("Best parameter (CV score=%0.3f):" % search.best_score_) |
51 | 51 | print(search.best_params_) |
|
0 commit comments