{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Comparing various online solvers\nAn example showing how different online solvers perform\non the hand-written digits dataset.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Authors: The scikit-learn developers\n# SPDX-License-Identifier: BSD-3-Clause\n\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nfrom sklearn import datasets\nfrom sklearn.linear_model import (\n LogisticRegression,\n PassiveAggressiveClassifier,\n Perceptron,\n SGDClassifier,\n)\nfrom sklearn.model_selection import train_test_split\n\nheldout = [0.95, 0.90, 0.75, 0.50, 0.01]\n# Number of rounds to fit and evaluate an estimator.\nrounds = 10\nX, y = datasets.load_digits(return_X_y=True)\n\nclassifiers = [\n (\"SGD\", SGDClassifier(max_iter=110)),\n (\"ASGD\", SGDClassifier(max_iter=110, average=True)),\n (\"Perceptron\", Perceptron(max_iter=110)),\n (\n \"Passive-Aggressive I\",\n PassiveAggressiveClassifier(max_iter=110, loss=\"hinge\", C=1.0, tol=1e-4),\n ),\n (\n \"Passive-Aggressive II\",\n PassiveAggressiveClassifier(\n max_iter=110, loss=\"squared_hinge\", C=1.0, tol=1e-4\n ),\n ),\n (\n \"SAG\",\n LogisticRegression(max_iter=110, solver=\"sag\", tol=1e-1, C=1.0e4 / X.shape[0]),\n ),\n]\n\nxx = 1.0 - np.array(heldout)\n\nfor name, clf in classifiers:\n print(\"training %s\" % name)\n rng = np.random.RandomState(42)\n yy = []\n for i in heldout:\n yy_ = []\n for r in range(rounds):\n X_train, X_test, y_train, y_test = train_test_split(\n X, y, test_size=i, random_state=rng\n )\n clf.fit(X_train, y_train)\n y_pred = clf.predict(X_test)\n yy_.append(1 - np.mean(y_pred == y_test))\n yy.append(np.mean(yy_))\n plt.plot(xx, yy, label=name)\n\nplt.legend(loc=\"upper right\")\nplt.xlabel(\"Proportion train\")\nplt.ylabel(\"Test Error Rate\")\nplt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.21" } }, "nbformat": 4, "nbformat_minor": 0 }