{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Classify flowers\n",
        "\n",
        "A botanist measured 150 iris flowers: the length and width of the petals and of the sepals. Each flower is one of three species. You will predict the species from the four measurements.\n",
        "\n",
        "**The data:** The Iris dataset that comes with scikit-learn: 150 flowers, 50 of each species.\n",
        "\n",
        "The steps follow https://mlcode.tech/projects/classify-flowers. Run the two setup cells first."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "%pip -q install numpy pandas scikit-learn matplotlib"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "from sklearn.datasets import load_iris\n",
        "from sklearn.model_selection import train_test_split\n",
        "from sklearn.pipeline import make_pipeline\n",
        "from sklearn.preprocessing import StandardScaler\n",
        "from sklearn.neighbors import KNeighborsClassifier\n",
        "from sklearn.dummy import DummyClassifier\n",
        "from sklearn.metrics import accuracy_score, ConfusionMatrixDisplay\n",
        "iris = load_iris(as_frame=True)\n",
        "data = iris.frame\n",
        "data.head()"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Step 1: Split fairly and scale\n",
        "\n",
        "With three species, a random split can put too few of one species in the test set. A stratified split keeps the same share of each species in both sets.\n",
        "\n",
        "Nearest-neighbor models use distances, so a feature with bigger numbers counts for more. Scaling puts every feature on the same range. Learn the scaling from the training data only. Otherwise information from the test set leaks into training.\n",
        "\n",
        "**Your task:** Make a stratified 80/20 split. Put a StandardScaler in a pipeline so it learns from the training data only.\n",
        "\n",
        "Practice first: https://mlcode.tech/problems/feature-scaling"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "# Write your implementation here.\n"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Step 2: Classify by nearest neighbors\n",
        "\n",
        "k-nearest neighbors (k-NN) does not learn weights. To classify a new flower, it finds the k training flowers closest to it and takes a vote.\n",
        "\n",
        "It is simple and easy to inspect, so it is a good first classifier. Compare it with a baseline that always predicts the most common species.\n",
        "\n",
        "**Your task:** Train a k-NN classifier with k = 5 and a most-frequent-class baseline. Predict the test set with both.\n",
        "\n",
        "Practice first: https://mlcode.tech/problems/knn-classifier"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "# Write your implementation here.\n"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Step 3: Look at the mistakes\n",
        "\n",
        "Accuracy gives one number, but it does not show which species get mixed up.\n",
        "\n",
        "A confusion matrix has one row for each true species and one column for each predicted species. The diagonal holds the correct answers. Every other cell is a mistake, and its position tells you which two species the model confused.\n",
        "\n",
        "**Your task:** Compute accuracy and a confusion matrix. Print the measurements of each flower the model got wrong.\n",
        "\n",
        "Practice first: https://mlcode.tech/problems/confusion-matrix"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "# Write your implementation here.\n"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Check your work\n",
        "\n",
        "- Which two species are hardest to tell apart?\n",
        "- Does a different k help? Choose k without looking at the test set.\n",
        "- How much better than the baseline is the model?\n",
        "\n",
        "Write your answers here."
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3.11"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
