{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "source": [ "pip install --upgrade scikit-learn" ], "metadata": { "id": "-x8MxrAMbOyM" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "63GSckRspoZJ" }, "source": [ "# Logistic Regression\n", "\n", "In this demo, we will train and test a logistic regression model on a toy classification dataset." ] }, { "cell_type": "markdown", "metadata": { "id": "h9MDqr8IxNuh" }, "source": [ "### 1. Generate dataset\n", "\n", "We can use the [make_classification](https://scikit-learn.org/stable/modules/generated/sklearn.datasets.make_classification.html) function from sklearn to generate the data." ] }, { "cell_type": "code", "metadata": { "id": "MefuasTHjBvI" }, "source": [ "from sklearn.datasets import make_classification\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "X, Y = make_classification(n_samples=1500, \n", " n_features=2, \n", " n_informative=2, \n", " n_redundant=0, \n", " n_classes=2\n", " )" ], "execution_count": 2, "outputs": [] }, { "cell_type": "markdown", "source": [ "# 2. Split dataset into train/test sets" ], "metadata": { "id": "sGo3Xru3ct9l" } }, { "cell_type": "code", "source": [ "from sklearn.model_selection import train_test_split\n", "\n", "X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.4)\n" ], "metadata": { "id": "gHey3Ca_ZJym" }, "execution_count": 3, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "v5_wyZRu6CF_" }, "source": [ "### 3. Define and train a logistic regression model on the train set" ] }, { "cell_type": "code", "metadata": { "id": "34kjAIGf6Ghn", "colab": { "base_uri": "https://localhost:8080/", "height": 93 }, "outputId": "b4c1ac94-31c3-4fd1-d66d-72e58c804459" }, "source": [ "from sklearn.linear_model import LogisticRegression\n", "\n", "model = LogisticRegression()\n", "model.fit(X_train, Y_train)" ], "execution_count": 4, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "LogisticRegression()" ], "text/html": [ "
LogisticRegression()In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
LogisticRegression()