-
-
Notifications
You must be signed in to change notification settings - Fork 25.8k
[MRG] Fix FutureWarnings in logistic regression examples #12114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
amueller
merged 19 commits into
scikit-learn:master
from
ogrisel:fix-logistic-regression-examples
Sep 24, 2018
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
d189026
Be more specific about logistic regression solver in examples
ogrisel 4b7483f
Use early stopped SGD (faster) and plot cross-validated error for bes…
ogrisel 447d6f2
Fix LR solver in /plot_voting_probas.pyexamples/ensemble/plot_voting_…
ogrisel c54c342
Fix LR solver & scale data in plot_digits_classification_exercise.py
ogrisel c4bba06
Use saga solver in plot_logistic_l1_l2_sparsity.py
ogrisel dfb94e2
Use LBFGS solver in plot_iris_logistic.py
ogrisel c856d9e
Use LBFGS in plot_logistic.py
ogrisel 03102e0
Use SAGA solver for Logistic Regression Path example
ogrisel 3509ac1
Use LBFGS solver in plot_classifier_chain_yeast.py
ogrisel ec2918e
Use LBFGS solver in plot_rbm_logistic_classification.py
ogrisel 30b56e5
typo
ogrisel e76d5fc
typo
ogrisel 7658268
Bump up pandas dependency to 0.17.1
ogrisel 0801606
Bump up examples minimal deps to match pandas 0.17.1
ogrisel 8134fa8
Fix figure layout for plot_digits_pipe.py
ogrisel 2539fc5
Version numbers are not decimal numbers
ogrisel c7a5c05
Set multinomial, no scaling to keep example simple, fix formatting of…
ogrisel 2bbc6cd
Missing plt.tight_layout() in plot_voting_probas.py
ogrisel 79a97f9
Missing plt.tight_layout() in plot_logistic.py
ogrisel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,42 +22,58 @@ | |
|
||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
import pandas as pd | ||
|
||
from sklearn import linear_model, decomposition, datasets | ||
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 | ||
|
||
logistic = linear_model.LogisticRegression() | ||
|
||
pca = decomposition.PCA() | ||
# 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I use SGD classifier here because it's much faster thanks to the early stopping and we are only interested in generalization (not optimization and coef values) in this specific example. |
||
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, | ||
return_train_score=False) | ||
search.fit(X_digits, y_digits) | ||
print("Best parameter (CV score=%0.3f):" % search.best_score_) | ||
print(search.best_params_) | ||
|
||
# Plot the PCA spectrum | ||
pca.fit(X_digits) | ||
|
||
plt.figure(1, figsize=(4, 3)) | ||
plt.clf() | ||
plt.axes([.2, .2, .7, .7]) | ||
plt.plot(pca.explained_variance_, linewidth=2) | ||
plt.axis('tight') | ||
plt.xlabel('n_components') | ||
plt.ylabel('explained_variance_') | ||
fig, (ax0, ax1) = plt.subplots(nrows=2, sharex=True, figsize=(6, 6)) | ||
ax0.plot(pca.explained_variance_ratio_, linewidth=2) | ||
ax0.set_ylabel('PCA explained variance') | ||
|
||
ax0.axvline(search.best_estimator_.named_steps['pca'].n_components, | ||
linestyle=':', label='n_components chosen') | ||
ax0.legend(prop=dict(size=12)) | ||
|
||
# Prediction | ||
n_components = [20, 40, 64] | ||
Cs = np.logspace(-4, 4, 3) | ||
# For each number of components, find the best classifier results | ||
results = pd.DataFrame(search.cv_results_) | ||
components_col = 'param_pca__n_components' | ||
best_clfs = results.groupby(components_col).apply( | ||
lambda g: g.nlargest(1, 'mean_test_score')) | ||
|
||
# Parameters of pipelines can be set using ‘__’ separated parameter names: | ||
estimator = GridSearchCV(pipe, | ||
dict(pca__n_components=n_components, | ||
logistic__C=Cs), cv=5) | ||
estimator.fit(X_digits, y_digits) | ||
best_clfs.plot(x=components_col, y='mean_test_score', yerr='std_test_score', | ||
legend=False, ax=ax1) | ||
ax1.set_ylabel('Classification accuracy (val)') | ||
ax1.set_xlabel('n_components') | ||
|
||
plt.axvline(estimator.best_estimator_.named_steps['pca'].n_components, | ||
linestyle=':', label='n_components chosen') | ||
plt.legend(prop=dict(size=12)) | ||
plt.tight_layout() | ||
plt.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to use
DataFrame.nlargest
to make it easy to plot the results of a grid search. This causes a general bump up in the dependency versions for the documentation build. But I think this is fine.