{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Training a Machine Learning model with scikit-learn ([video #4](https://www.youtube.com/watch?v=RlQuVL6-qe8&list=PL5-da3qGB5ICeMbQuqbbCOQWcS6OYBr5A&index=4))\n", "\n", "Created by [Data School](https://www.dataschool.io). Watch all 10 videos on [YouTube](https://www.youtube.com/playlist?list=PL5-da3qGB5ICeMbQuqbbCOQWcS6OYBr5A). Download the notebooks from [GitHub](https://github.com/justmarkham/scikit-learn-videos).\n", "\n", "**Note:** This notebook uses Python 3.9.1 and scikit-learn 0.23.2. The original notebook (shown in the video) used Python 2.7 and scikit-learn 0.16." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Agenda\n", "\n", "- What is the **K-nearest neighbors** classification model?\n", "- What are the four steps for **model training and prediction** in scikit-learn?\n", "- How can I apply this pattern to **other Machine Learning models**?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reviewing the iris dataset" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# added empty cell so that the cell numbering matches the video" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.display import IFrame\n", "IFrame('https://www.dataschool.io/files/iris.txt', width=300, height=200)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 150 **observations**\n", "- 4 **features** (sepal length, sepal width, petal length, petal width)\n", "- **Response** variable is the iris species\n", "- **Classification** problem since response is categorical\n", "- More information in the [UCI Machine Learning Repository](http://archive.ics.uci.edu/ml/datasets/Iris)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## K-nearest neighbors (KNN) classification" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. Pick a value for K.\n", "2. Search for the K observations in the training data that are \"nearest\" to the measurements of the unknown iris.\n", "3. Use the most popular response value from the K nearest neighbors as the predicted response value for the unknown iris." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example training data\n", "\n", "![Training data](images/04_knn_dataset.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### KNN classification map (K=1)\n", "\n", "![1NN classification map](images/04_1nn_map.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### KNN classification map (K=5)\n", "\n", "![5NN classification map](images/04_5nn_map.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Image Credits: [Data3classes](https://commons.wikimedia.org/wiki/File:Data3classes.png#/media/File:Data3classes.png), [Map1NN](https://commons.wikimedia.org/wiki/File:Map1NN.png#/media/File:Map1NN.png), [Map5NN](https://commons.wikimedia.org/wiki/File:Map5NN.png#/media/File:Map5NN.png) by Agor153. Licensed under CC BY-SA 3.0*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Loading the data" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# import load_iris function from datasets module\n", "from sklearn.datasets import load_iris\n", "\n", "# save \"bunch\" object containing iris dataset and its attributes\n", "iris = load_iris()\n", "\n", "# store feature matrix in \"X\"\n", "X = iris.data\n", "\n", "# store response vector in \"y\"\n", "y = iris.target" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(150, 4)\n", "(150,)\n" ] } ], "source": [ "# print the shapes of X and y\n", "print(X.shape)\n", "print(y.shape)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## scikit-learn 4-step modeling pattern" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Step 1:** Import the class you plan to use" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "from sklearn.neighbors import KNeighborsClassifier" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Step 2:** \"Instantiate\" the \"estimator\"\n", "\n", "- \"Estimator\" is scikit-learn's term for model\n", "- \"Instantiate\" means \"make an instance of\"" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "knn = KNeighborsClassifier(n_neighbors=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Name of the object does not matter\n", "- Can specify tuning parameters (aka \"hyperparameters\") during this step\n", "- All parameters not specified are set to their defaults" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "KNeighborsClassifier(n_neighbors=1)\n" ] } ], "source": [ "print(knn)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Step 3:** Fit the model with data (aka \"model training\")\n", "\n", "- Model is learning the relationship between X and y\n", "- Occurs in-place" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "KNeighborsClassifier(n_neighbors=1)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "knn.fit(X, y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Step 4:** Predict the response for a new observation\n", "\n", "- New observations are called \"out-of-sample\" data\n", "- Uses the information it learned during the model training process" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "knn.predict([[3, 5, 4, 2]])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Returns a NumPy array\n", "- Can predict for multiple observations at once" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2, 1])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X_new = [[3, 5, 4, 2], [5, 4, 3, 2]]\n", "knn.predict(X_new)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using a different value for K" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 1])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# instantiate the model (using the value K=5)\n", "knn = KNeighborsClassifier(n_neighbors=5)\n", "\n", "# fit the model with data\n", "knn.fit(X, y)\n", "\n", "# predict the response for new observations\n", "knn.predict(X_new)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using a different classification model" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2, 0])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# import the class\n", "from sklearn.linear_model import LogisticRegression\n", "\n", "# instantiate the model\n", "logreg = LogisticRegression(solver='liblinear')\n", "\n", "# fit the model with data\n", "logreg.fit(X, y)\n", "\n", "# predict the response for new observations\n", "logreg.predict(X_new)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Resources\n", "\n", "- [Nearest Neighbors](https://scikit-learn.org/stable/modules/neighbors.html) (user guide), [KNeighborsClassifier](https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsClassifier.html) (class documentation)\n", "- [Logistic Regression](https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression) (user guide), [LogisticRegression](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html) (class documentation)\n", "- [Videos from An Introduction to Statistical Learning](https://www.dataschool.io/15-hours-of-expert-machine-learning-videos/)\n", " - Classification Problems and K-Nearest Neighbors (Chapter 2)\n", " - Introduction to Classification (Chapter 4)\n", " - Logistic Regression and Maximum Likelihood (Chapter 4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Comments or Questions?\n", "\n", "- Email: \n", "- Website: https://www.dataschool.io\n", "- Twitter: [@justmarkham](https://twitter.com/justmarkham)\n", "\n", "© 2021 [Data School](https://www.dataschool.io). All rights reserved." ] } ], "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.4" } }, "nbformat": 4, "nbformat_minor": 1 }