{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Predict bike demand\n",
        "\n",
        "A bike rental shop wants to know how many bikes to have ready each day. With too few, customers leave. With too many, bikes sit unused. You will predict daily rentals from the weather and the day of the week.\n",
        "\n",
        "**The data:** 360 days of simulated data: temperature, rain, weekend, and the number of bikes rented.\n",
        "\n",
        "The steps follow https://mlcode.tech/projects/predict-demand. 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": [
        "import numpy as np\n",
        "import pandas as pd\n",
        "from sklearn.model_selection import train_test_split\n",
        "from sklearn.linear_model import LinearRegression\n",
        "from sklearn.dummy import DummyRegressor\n",
        "from sklearn.metrics import mean_absolute_error\n",
        "import matplotlib.pyplot as plt\n",
        "rng = np.random.default_rng(42)\n",
        "n = 360\n",
        "data = pd.DataFrame({'temperature': rng.uniform(5, 32, n), 'rain': rng.binomial(1, .25, n), 'weekend': rng.binomial(1, 2/7, n)})\n",
        "data['rentals'] = np.maximum(0, 80 + 8 * data.temperature - 70 * data.rain + 35 * data.weekend + rng.normal(0, 25, n)).round()\n",
        "data.head()"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Step 1: Keep some days for testing\n",
        "\n",
        "A model can memorize the days it learned from. To find out how it does on new days, put some days aside before you train anything, and use them only at the end.\n",
        "\n",
        "A common split is 80% for training and 20% for testing. Shuffle the rows first, so the test days do not all come from one part of the year.\n",
        "\n",
        "**Your task:** Separate the features (temperature, rain, weekend) from the target (rentals). Hold out 20% of the rows as a test set.\n",
        "\n",
        "Practice first: https://mlcode.tech/problems/train-test-split"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "# Write your implementation here.\n"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Step 2: Beat a simple guess\n",
        "\n",
        "Before you build a model, find out how well a lazy guess does. Here the lazy guess is the average number of rentals in the training data, used for every day. A model is only useful if it beats this.\n",
        "\n",
        "Linear regression gives each feature a weight. For example, each extra degree adds some rentals and rain removes some. It picks the weights that make the squared errors on the training data as small as possible.\n",
        "\n",
        "**Your task:** Predict the training mean for every test day. Then fit a linear regression on the training data and predict the test days.\n",
        "\n",
        "Practice first: https://mlcode.tech/problems/linear-regression-normal-equation"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "# Write your implementation here.\n"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Step 3: Measure the errors\n",
        "\n",
        "Mean absolute error (MAE) is the average size of a miss, counted in bikes. It is easy to explain to the shop owner.\n",
        "\n",
        "R² is the share of the day-to-day change that the model explains. 0 means no better than the average, and 1 means perfect. Also look at the residuals (actual minus predicted). If they grow on hot days, the model misses something.\n",
        "\n",
        "**Your task:** Report MAE for the baseline and for the model. Plot predicted rentals against actual rentals.\n",
        "\n",
        "Practice first: https://mlcode.tech/problems/r-squared"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "# Write your implementation here.\n"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Check your work\n",
        "\n",
        "- Does your model beat the average on the test days?\n",
        "- Which feature changes the prediction most?\n",
        "- Why can simulated data not tell you how the model would do at a real shop?\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
}
