{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Group notes by topic\n",
        "\n",
        "You have a set of short notes about sport, cooking and computing, with no labels. You will make a model sort them into groups without telling it the topics.\n",
        "\n",
        "**The data:** Nine short notes, three on each topic, written into the notebook.\n",
        "\n",
        "The steps follow https://mlcode.tech/projects/group-documents. 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.feature_extraction.text import TfidfVectorizer\n",
        "from sklearn.cluster import KMeans\n",
        "notes = [\n",
        "    'The football team scored a goal in the final match',\n",
        "    'Our team practiced passing the football before the match',\n",
        "    'The goalkeeper saved the shot and won the game',\n",
        "    'Bake the bread dough in a hot oven',\n",
        "    'Mix flour and water and knead the bread dough',\n",
        "    'The recipe calls for baking the cake in the oven',\n",
        "    'The Python program reads a file and sorts the data',\n",
        "    'Debug the Python function and run the program tests',\n",
        "    'The database stores data for the software program',\n",
        "]\n",
        "notes"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Step 1: Turn text into numbers\n",
        "\n",
        "Models work with numbers, so each note must become a vector.\n",
        "\n",
        "TF-IDF gives each word a score in each note. A word scores high when it appears often in that note but rarely in the others. Words such as \"the\" appear everywhere, so they score low and stop dominating.\n",
        "\n",
        "**Your task:** Convert the notes to TF-IDF vectors. Print the vocabulary and the top-scoring words for one note.\n",
        "\n",
        "Practice first: https://mlcode.tech/problems/tf-idf"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "# Write your implementation here.\n"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Step 2: Group similar notes\n",
        "\n",
        "k-means splits the data into k groups. It starts with k random centers, puts each note in the group of its nearest center, moves each center to the middle of its group, and repeats until nothing changes.\n",
        "\n",
        "You choose k. You know there are three topics, so use k = 3. Fix the random seed so you get the same groups on each run.\n",
        "\n",
        "**Your task:** Fit k-means with 3 clusters and a fixed seed. Print each note next to its cluster number.\n",
        "\n",
        "Practice first: https://mlcode.tech/problems/kmeans"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "# Write your implementation here.\n"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Step 3: Find what each group means\n",
        "\n",
        "The model gives each group a number, not a name. To find out what a group is about, look at the words with the highest weight in its center.\n",
        "\n",
        "Cosine similarity compares two notes by the angle between their vectors, so long and short notes compare fairly. Use it to see where a new, unclear note fits.\n",
        "\n",
        "**Your task:** Print the top words for each cluster. Add a note that mixes two topics and find the cluster it is closest to.\n",
        "\n",
        "Practice first: https://mlcode.tech/problems/cosine-similarity"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "# Write your implementation here.\n"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Check your work\n",
        "\n",
        "- Do the groups match the topics you see when you read the notes?\n",
        "- Why do the cluster numbers change when you change the seed?\n",
        "- Why do nine notes not prove that this works on a large archive?\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
}
