{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Logistic function\n\nShown in the plot is how the logistic regression would, in this\nsynthetic dataset, classify values as either 0 or 1,\ni.e. class one or two, using the logistic curve.\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\nfrom scipy.special import expit\n\nfrom sklearn.linear_model import LinearRegression, LogisticRegression\n\n# Generate a toy dataset, it's just a straight line with some Gaussian noise:\nxmin, xmax = -5, 5\nn_samples = 100\nnp.random.seed(0)\nX = np.random.normal(size=n_samples)\ny = (X > 0).astype(float)\nX[X > 0] *= 4\nX += 0.3 * np.random.normal(size=n_samples)\n\nX = X[:, np.newaxis]\n\n# Fit the classifier\nclf = LogisticRegression(C=1e5)\nclf.fit(X, y)\n\n# and plot the result\nplt.figure(1, figsize=(4, 3))\nplt.clf()\nplt.scatter(X.ravel(), y, label=\"example data\", color=\"black\", zorder=20)\nX_test = np.linspace(-5, 10, 300)\n\nloss = expit(X_test * clf.coef_ + clf.intercept_).ravel()\nplt.plot(X_test, loss, label=\"Logistic Regression Model\", color=\"red\", linewidth=3)\n\nols = LinearRegression()\nols.fit(X, y)\nplt.plot(\n X_test,\n ols.coef_ * X_test + ols.intercept_,\n label=\"Linear Regression Model\",\n linewidth=1,\n)\nplt.axhline(0.5, color=\".5\")\n\nplt.ylabel(\"y\")\nplt.xlabel(\"X\")\nplt.xticks(range(-5, 10))\nplt.yticks([0, 0.5, 1])\nplt.ylim(-0.25, 1.25)\nplt.xlim(-4, 10)\nplt.legend(\n loc=\"lower right\",\n fontsize=\"small\",\n)\nplt.tight_layout()\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 }