{ "cells": [ { "cell_type": "markdown", "id": "a92ddba5", "metadata": {}, "source": [ "# Lesson 22 activity: unsupervised learning\n", "\n", "## Notebook setup\n", "\n", "### Imports" ] }, { "cell_type": "code", "execution_count": 1, "id": "7248ac92", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "id": "0320dae3", "metadata": {}, "source": [ "### Dataset" ] }, { "cell_type": "code", "execution_count": null, "id": "77b65c19", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
longitudelatitudehousing_median_agetotal_roomstotal_bedroomspopulationhouseholdsmedian_incomemedian_house_value
0-122.2337.8841.0880.0129.0322.0126.08.3252452600.0
1-122.2237.8621.07099.01106.02401.01138.08.3014358500.0
2-122.2437.8552.01467.0190.0496.0177.07.2574352100.0
3-122.2537.8552.01274.0235.0558.0219.05.6431341300.0
4-122.2537.8552.01627.0280.0565.0259.03.8462342200.0
\n", "
" ], "text/plain": [ " longitude latitude housing_median_age total_rooms total_bedrooms \\\n", "0 -122.23 37.88 41.0 880.0 129.0 \n", "1 -122.22 37.86 21.0 7099.0 1106.0 \n", "2 -122.24 37.85 52.0 1467.0 190.0 \n", "3 -122.25 37.85 52.0 1274.0 235.0 \n", "4 -122.25 37.85 52.0 1627.0 280.0 \n", "\n", " population households median_income median_house_value \n", "0 322.0 126.0 8.3252 452600.0 \n", "1 2401.0 1138.0 8.3014 358500.0 \n", "2 496.0 177.0 7.2574 352100.0 \n", "3 558.0 219.0 5.6431 341300.0 \n", "4 565.0 259.0 3.8462 342200.0 " ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv('https://media.githubusercontent.com/media/gperdrizet/fullstack-2605/refs/heads/main/data/housing.csv')\n", "df.head()" ] }, { "cell_type": "markdown", "id": "de1e8cc9", "metadata": {}, "source": [ "## Task 1: prepare data for clustering\n", "\n", "Prepare the housing dataset for k-means clustering by completing the following steps:\n", "\n", "- Check for and handle any missing values in the dataset\n", "- Standardize the features using `StandardScaler` to ensure all variables are on the same scale" ] }, { "cell_type": "code", "execution_count": 3, "id": "ed7fac23", "metadata": {}, "outputs": [], "source": [ "# Your code here" ] }, { "cell_type": "markdown", "id": "4541c37d", "metadata": {}, "source": [ "## Task 2: cluster data with k-means\n", "\n", "Apply k-means clustering to the prepared data:\n", "\n", "- Use the elbow method to determine the optimal number of clusters (k)\n", " - Test k values from 2 to 10\n", " - Plot the within-cluster sum of squares (inertia) for each k value\n", " - Choose an appropriate k value based on the elbow in the plot\n", "- Fit a `KMeans` model with the chosen k value using `random_state=315`\n", "- Add the cluster labels as a new column to the original dataframe\n", "- Create a visualization to show the distribution of data points across clusters\n", "\n", "**Hints:**\n", "- The `inertia_` attribute of a fitted KMeans model gives you the within-cluster sum of squares\n", "- A simple loop can help you test multiple k values and store their inertia values\n", "- Use `sklearn.manifold.TSNE` to collapse the features into two dimensions for plotting" ] }, { "cell_type": "code", "execution_count": 4, "id": "1f061fe1", "metadata": {}, "outputs": [], "source": [ "# Your code here" ] }, { "cell_type": "markdown", "id": "3dc46951", "metadata": {}, "source": [ "## Task 3: train a supervised model to assign cluster membership\n", "\n", "Build a supervised learning model that can predict cluster assignments for new data:\n", "\n", "- Split the data into training and testing sets`\n", "- Use the scaled features from task 1 as the input and the cluster labels from task 2 as the label\n", "- Train a classification model (such as `RandomForestClassifier` or `LogisticRegression`) to predict cluster membership\n", "- Evaluate the model's performance on the test set using appropriate metrics (F1 score, precision, recall, confusion matrix)" ] }, { "cell_type": "code", "execution_count": 5, "id": "b1f4da2e", "metadata": {}, "outputs": [], "source": [ "# Your code here" ] } ], "metadata": { "kernelspec": { "display_name": ".venv (3.12.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.12.3" } }, "nbformat": 4, "nbformat_minor": 5 }