From 87bd64a9407a1a36b3ebdfbf3b2ba631681ac0bc Mon Sep 17 00:00:00 2001 From: johgreen <61171945+johgreen@users.noreply.github.com> Date: Wed, 10 Nov 2021 13:20:05 -0500 Subject: [PATCH 1/3] speed up sgdclassifier plotting example --- examples/linear_model/plot_sgd_early_stopping.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/examples/linear_model/plot_sgd_early_stopping.py b/examples/linear_model/plot_sgd_early_stopping.py index 6e41c961ebb93..9b2546cc1843d 100644 --- a/examples/linear_model/plot_sgd_early_stopping.py +++ b/examples/linear_model/plot_sgd_early_stopping.py @@ -48,7 +48,7 @@ import numpy as np import matplotlib.pyplot as plt -from sklearn import linear_model +from sklearn.linear_model import SGDClassifier from sklearn.datasets import fetch_openml from sklearn.model_selection import train_test_split from sklearn.utils._testing import ignore_warnings @@ -56,6 +56,7 @@ from sklearn.utils import shuffle + def load_mnist(n_samples=None, class_0="0", class_1="8"): """Load MNIST, select two classes, shuffle and return only n_samples.""" # Load data from http://openml.org/d/554 @@ -70,6 +71,9 @@ def load_mnist(n_samples=None, class_0="0", class_1="8"): return X, y + + + @ignore_warnings(category=ConvergenceWarning) def fit_and_score(estimator, max_iter, X_train, X_test, y_train, y_test): """Fit the estimator on the train set and score it on both sets""" @@ -89,17 +93,17 @@ def fit_and_score(estimator, max_iter, X_train, X_test, y_train, y_test): # Define the estimators to compare estimator_dict = { - "No stopping criterion": linear_model.SGDClassifier(n_iter_no_change=3), - "Training loss": linear_model.SGDClassifier( - early_stopping=False, n_iter_no_change=3, tol=0.1 + "No stopping criterion": SGDClassifier(n_iter_no_change=3), + "Training loss": SGDClassifier( + early_stopping = False, n_iter_no_change=3, tol=0.1 ), - "Validation score": linear_model.SGDClassifier( + "Validation score": SGDClassifier( early_stopping=True, n_iter_no_change=3, tol=0.0001, validation_fraction=0.2 ), } # Load the dataset -X, y = load_mnist(n_samples=10000) +X, y = load_mnist(n_samples=5000) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=0) results = [] From 7cf91c89b72377f423ff334481d2ee44e0a3fc0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=20Dupr=C3=A9=20la=20Tour?= Date: Tue, 16 Nov 2021 09:48:31 -0800 Subject: [PATCH 2/3] Update plot_sgd_early_stopping.py --- examples/linear_model/plot_sgd_early_stopping.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/examples/linear_model/plot_sgd_early_stopping.py b/examples/linear_model/plot_sgd_early_stopping.py index 9b2546cc1843d..ca6a842afa13e 100644 --- a/examples/linear_model/plot_sgd_early_stopping.py +++ b/examples/linear_model/plot_sgd_early_stopping.py @@ -56,7 +56,6 @@ from sklearn.utils import shuffle - def load_mnist(n_samples=None, class_0="0", class_1="8"): """Load MNIST, select two classes, shuffle and return only n_samples.""" # Load data from http://openml.org/d/554 @@ -71,9 +70,6 @@ def load_mnist(n_samples=None, class_0="0", class_1="8"): return X, y - - - @ignore_warnings(category=ConvergenceWarning) def fit_and_score(estimator, max_iter, X_train, X_test, y_train, y_test): """Fit the estimator on the train set and score it on both sets""" @@ -95,7 +91,7 @@ def fit_and_score(estimator, max_iter, X_train, X_test, y_train, y_test): estimator_dict = { "No stopping criterion": SGDClassifier(n_iter_no_change=3), "Training loss": SGDClassifier( - early_stopping = False, n_iter_no_change=3, tol=0.1 + early_stopping=False, n_iter_no_change=3, tol=0.1 ), "Validation score": SGDClassifier( early_stopping=True, n_iter_no_change=3, tol=0.0001, validation_fraction=0.2 From 5c1e83abdc01c55881db11663f2accbeff46ca1c Mon Sep 17 00:00:00 2001 From: adrinjalali Date: Tue, 23 Nov 2021 18:01:04 +0100 Subject: [PATCH 3/3] apply black --- examples/linear_model/plot_sgd_early_stopping.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/examples/linear_model/plot_sgd_early_stopping.py b/examples/linear_model/plot_sgd_early_stopping.py index ca6a842afa13e..ae3137c673893 100644 --- a/examples/linear_model/plot_sgd_early_stopping.py +++ b/examples/linear_model/plot_sgd_early_stopping.py @@ -90,9 +90,7 @@ def fit_and_score(estimator, max_iter, X_train, X_test, y_train, y_test): # Define the estimators to compare estimator_dict = { "No stopping criterion": SGDClassifier(n_iter_no_change=3), - "Training loss": SGDClassifier( - early_stopping=False, n_iter_no_change=3, tol=0.1 - ), + "Training loss": SGDClassifier(early_stopping=False, n_iter_no_change=3, tol=0.1), "Validation score": SGDClassifier( early_stopping=True, n_iter_no_change=3, tol=0.0001, validation_fraction=0.2 ),