{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Indicators of Heavy Traffic on I-94\n",
    "\n",
    "In this project, we're going to analyze a dataset about the westbound traffic on the [I-94 Interstate highway](https://en.wikipedia.org/wiki/Interstate_94).\n",
    "\n",
    "The goal of our analysis is to determine a few indicators of heavy traffic on I-94. These indicators can be weather type, time of the day, time of the week, etc.\n",
    "\n",
    "## The I-94 Traffic Dataset\n",
    "\n",
    "John Hogue made the dataset available that we'll be working with, and you can download it from the [UCI Machine Learning Repository](https://archive.ics.uci.edu/ml/datasets/Metro+Interstate+Traffic+Volume)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>holiday</th>\n",
       "      <th>temp</th>\n",
       "      <th>rain_1h</th>\n",
       "      <th>snow_1h</th>\n",
       "      <th>clouds_all</th>\n",
       "      <th>weather_main</th>\n",
       "      <th>weather_description</th>\n",
       "      <th>date_time</th>\n",
       "      <th>traffic_volume</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>None</td>\n",
       "      <td>288.28</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>40</td>\n",
       "      <td>Clouds</td>\n",
       "      <td>scattered clouds</td>\n",
       "      <td>2012-10-02 09:00:00</td>\n",
       "      <td>5545</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>None</td>\n",
       "      <td>289.36</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>75</td>\n",
       "      <td>Clouds</td>\n",
       "      <td>broken clouds</td>\n",
       "      <td>2012-10-02 10:00:00</td>\n",
       "      <td>4516</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>None</td>\n",
       "      <td>289.58</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>90</td>\n",
       "      <td>Clouds</td>\n",
       "      <td>overcast clouds</td>\n",
       "      <td>2012-10-02 11:00:00</td>\n",
       "      <td>4767</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>None</td>\n",
       "      <td>290.13</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>90</td>\n",
       "      <td>Clouds</td>\n",
       "      <td>overcast clouds</td>\n",
       "      <td>2012-10-02 12:00:00</td>\n",
       "      <td>5026</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>None</td>\n",
       "      <td>291.14</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>75</td>\n",
       "      <td>Clouds</td>\n",
       "      <td>broken clouds</td>\n",
       "      <td>2012-10-02 13:00:00</td>\n",
       "      <td>4918</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  holiday    temp  rain_1h  snow_1h  clouds_all weather_main  \\\n",
       "0    None  288.28      0.0      0.0          40       Clouds   \n",
       "1    None  289.36      0.0      0.0          75       Clouds   \n",
       "2    None  289.58      0.0      0.0          90       Clouds   \n",
       "3    None  290.13      0.0      0.0          90       Clouds   \n",
       "4    None  291.14      0.0      0.0          75       Clouds   \n",
       "\n",
       "  weather_description            date_time  traffic_volume  \n",
       "0    scattered clouds  2012-10-02 09:00:00            5545  \n",
       "1       broken clouds  2012-10-02 10:00:00            4516  \n",
       "2     overcast clouds  2012-10-02 11:00:00            4767  \n",
       "3     overcast clouds  2012-10-02 12:00:00            5026  \n",
       "4       broken clouds  2012-10-02 13:00:00            4918  "
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "i_94 = pd.read_csv('Metro_Interstate_Traffic_Volume.csv')\n",
    "i_94.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>holiday</th>\n",
       "      <th>temp</th>\n",
       "      <th>rain_1h</th>\n",
       "      <th>snow_1h</th>\n",
       "      <th>clouds_all</th>\n",
       "      <th>weather_main</th>\n",
       "      <th>weather_description</th>\n",
       "      <th>date_time</th>\n",
       "      <th>traffic_volume</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>48199</th>\n",
       "      <td>None</td>\n",
       "      <td>283.45</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>75</td>\n",
       "      <td>Clouds</td>\n",
       "      <td>broken clouds</td>\n",
       "      <td>2018-09-30 19:00:00</td>\n",
       "      <td>3543</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>48200</th>\n",
       "      <td>None</td>\n",
       "      <td>282.76</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>90</td>\n",
       "      <td>Clouds</td>\n",
       "      <td>overcast clouds</td>\n",
       "      <td>2018-09-30 20:00:00</td>\n",
       "      <td>2781</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>48201</th>\n",
       "      <td>None</td>\n",
       "      <td>282.73</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>90</td>\n",
       "      <td>Thunderstorm</td>\n",
       "      <td>proximity thunderstorm</td>\n",
       "      <td>2018-09-30 21:00:00</td>\n",
       "      <td>2159</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>48202</th>\n",
       "      <td>None</td>\n",
       "      <td>282.09</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>90</td>\n",
       "      <td>Clouds</td>\n",
       "      <td>overcast clouds</td>\n",
       "      <td>2018-09-30 22:00:00</td>\n",
       "      <td>1450</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>48203</th>\n",
       "      <td>None</td>\n",
       "      <td>282.12</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>90</td>\n",
       "      <td>Clouds</td>\n",
       "      <td>overcast clouds</td>\n",
       "      <td>2018-09-30 23:00:00</td>\n",
       "      <td>954</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      holiday    temp  rain_1h  snow_1h  clouds_all  weather_main  \\\n",
       "48199    None  283.45      0.0      0.0          75        Clouds   \n",
       "48200    None  282.76      0.0      0.0          90        Clouds   \n",
       "48201    None  282.73      0.0      0.0          90  Thunderstorm   \n",
       "48202    None  282.09      0.0      0.0          90        Clouds   \n",
       "48203    None  282.12      0.0      0.0          90        Clouds   \n",
       "\n",
       "          weather_description            date_time  traffic_volume  \n",
       "48199           broken clouds  2018-09-30 19:00:00            3543  \n",
       "48200         overcast clouds  2018-09-30 20:00:00            2781  \n",
       "48201  proximity thunderstorm  2018-09-30 21:00:00            2159  \n",
       "48202         overcast clouds  2018-09-30 22:00:00            1450  \n",
       "48203         overcast clouds  2018-09-30 23:00:00             954  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i_94.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 48204 entries, 0 to 48203\n",
      "Data columns (total 9 columns):\n",
      " #   Column               Non-Null Count  Dtype  \n",
      "---  ------               --------------  -----  \n",
      " 0   holiday              48204 non-null  object \n",
      " 1   temp                 48204 non-null  float64\n",
      " 2   rain_1h              48204 non-null  float64\n",
      " 3   snow_1h              48204 non-null  float64\n",
      " 4   clouds_all           48204 non-null  int64  \n",
      " 5   weather_main         48204 non-null  object \n",
      " 6   weather_description  48204 non-null  object \n",
      " 7   date_time            48204 non-null  object \n",
      " 8   traffic_volume       48204 non-null  int64  \n",
      "dtypes: float64(3), int64(2), object(4)\n",
      "memory usage: 3.3+ MB\n"
     ]
    }
   ],
   "source": [
    "i_94.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The dataset has 48,204 rows and 9 columns, and there are no null values. Each row describes traffic and weather data for a specific hour — we have data from 2012-10-02 09:00:00 until 2018-09-30 23:00:00.\n",
    "\n",
    "A station located approximately midway between Minneapolis and Saint Paul records the traffic data (see the [dataset documentation](https://archive.ics.uci.edu/ml/datasets/Metro+Interstate+Traffic+Volume)). For this station, the direction of the route is westbound (i.e., cars moving from east to west). This means that the results of our analysis will be about the westbound traffic in the proximity of the station. In other words, we should avoid generalizing our results for the entire I-94 highway.\n",
    "\n",
    "## Analyzing Traffic Volume\n",
    "\n",
    "We're going to start our analysis by examining the distribution of the `traffic_volume` column."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 8516 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "i_94['traffic_volume'].plot.hist()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    48204.000000\n",
       "mean      3259.818355\n",
       "std       1986.860670\n",
       "min          0.000000\n",
       "25%       1193.000000\n",
       "50%       3380.000000\n",
       "75%       4933.000000\n",
       "max       7280.000000\n",
       "Name: traffic_volume, dtype: float64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i_94['traffic_volume'].describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Between 2012-10-02 09:00:00 and 2018-09-30 23:00:00, the hourly traffic volume varied from 0 to 7,280 cars, with an average of 3,260 cars.\n",
    "\n",
    "About 25% of the time, there were only 1,193 cars or fewer passing the station each hour — this probably occurs during the night, or when a road is under construction. However, about 25% of the time, the traffic volume was four times as much (4,933 cars or more).\n",
    "\n",
    "This observation gives our analysis an interesting direction: comparing daytime data with nighttime data.\n",
    "\n",
    "## Traffic Volume: Day vs. Night\n",
    "\n",
    "We'll start by dividing the dataset into two parts:\n",
    "\n",
    "- Daytime data: hours from 7 AM to 7 PM (12 hours)\n",
    "- Nighttime data: hours from 7 PM to 7 AM (12 hours)\n",
    "\n",
    "While this is not a perfect criterion for distinguishing between nighttime and daytime, it's a good starting point."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(23877, 9)\n",
      "(24327, 9)\n"
     ]
    }
   ],
   "source": [
    "i_94['date_time'] = pd.to_datetime(i_94['date_time'])\n",
    "\n",
    "day = i_94.copy()[(i_94['date_time'].dt.hour >= 7) & (i_94['date_time'].dt.hour < 19)]\n",
    "print(day.shape)\n",
    "\n",
    "night = i_94.copy()[(i_94['date_time'].dt.hour >= 19) | (i_94['date_time'].dt.hour < 7)]\n",
    "print(night.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This significant difference in row numbers between `day` and `night` is due to a few hours of missing data. For instance, if you look at rows 176 and 177 (`i_94.iloc[176:178]`), you'll notice there's no data for two hours (4 and 5).\n",
    "\n",
    "## Traffic Volume: Day vs. Night (II)\n",
    "\n",
    "Now that we've isolated `day` and `night`, we're going to look at the histograms of traffic volume side-by-side by using a grid chart."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 13128 characters]\n",
      "text/plain": [
       "<Figure size 792x252 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(11,3.5))\n",
    "\n",
    "plt.subplot(1, 2, 1)\n",
    "plt.hist(day['traffic_volume'])\n",
    "plt.xlim(-100, 7500)\n",
    "plt.ylim(0, 8000)\n",
    "plt.title('Traffic Volume: Day')\n",
    "plt.ylabel('Frequency')\n",
    "plt.xlabel('Traffic Volume')\n",
    "\n",
    "plt.subplot(1, 2, 2)\n",
    "plt.hist(night['traffic_volume'])\n",
    "plt.xlim(-100, 7500)\n",
    "plt.ylim(0, 8000)\n",
    "plt.title('Traffic Volume: Night')\n",
    "plt.ylabel('Frequency')\n",
    "plt.xlabel('Traffic Volume')\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    23877.000000\n",
       "mean      4762.047452\n",
       "std       1174.546482\n",
       "min          0.000000\n",
       "25%       4252.000000\n",
       "50%       4820.000000\n",
       "75%       5559.000000\n",
       "max       7280.000000\n",
       "Name: traffic_volume, dtype: float64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "day['traffic_volume'].describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    24327.000000\n",
       "mean      1785.377441\n",
       "std       1441.951197\n",
       "min          0.000000\n",
       "25%        530.000000\n",
       "50%       1287.000000\n",
       "75%       2819.000000\n",
       "max       6386.000000\n",
       "Name: traffic_volume, dtype: float64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "night['traffic_volume'].describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The histogram that shows the distribution of traffic volume during the day is left skewed. This means that most of the traffic volume values are high — there are 4,252 or more cars passing the station each hour 75% of the time (because 25% of values are less than 4,252).\n",
    "\n",
    "The histogram displaying the nighttime data is right skewed. This means that most of the traffic volume values are low — 75% of the time, the number of cars that passed the station each hour was less than 2,819.\n",
    "\n",
    "Although there are still measurements of over 5,000 cars per hour, the traffic at night is generally light. Our goal is to find indicators of heavy traffic, so we'll only focus on the daytime data moving forward.\n",
    "\n",
    "## Time Indicators\n",
    "\n",
    "One of the possible indicators of heavy traffic is time. There might be more people on the road in a certain month, on a certain day, or at a certain time of day.\n",
    "\n",
    "We're going to look at a few line plots showing how the traffic volume changes according to the following:\n",
    "\n",
    "- Month\n",
    "- Day of the week\n",
    "- Time of day"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 16916 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "day['month'] = day['date_time'].dt.month\n",
    "by_month = day.groupby('month').mean()\n",
    "by_month['traffic_volume'].plot.line()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The traffic looks less heavy during cold months (November–February) and more intense during warm months (March–October), with one interesting exception: July. Is there anything special about July? Is traffic significantly less heavy in July each year?\n",
    "\n",
    "To answer the last question, let's see how the traffic volume changed each year in July."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 15608 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "day['year'] = day['date_time'].dt.year\n",
    "only_july = day[day['month'] == 7]\n",
    "only_july.groupby('year').mean()['traffic_volume'].plot.line()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Typically, the traffic is pretty heavy in July, similar to the other warm months. The only exception we see is 2016, which had a high decrease in traffic volume. One possible reason for this is road construction — [this article from 2016](https://www.crainsdetroit.com/article/20160728/NEWS/160729841/weekend-construction-i-96-us-23-bridge-work-i-94-lane-closures-i-696) supports this hypothesis.\n",
    "\n",
    "As a tentative conclusion here, we can say that warm months generally show heavier traffic compared to cold months. In a warm month, you can can expect for each hour of daytime a traffic volume close to 5,000 cars.\n",
    "\n",
    "## Time Indicators (II)\n",
    "\n",
    "Let's now look at a more granular indicator: day number."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 14656 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "day['dayofweek'] = day['date_time'].dt.dayofweek\n",
    "by_dayofweek = day.groupby('dayofweek').mean()\n",
    "by_dayofweek['traffic_volume'].plot.line()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Traffic volume is significantly heavier on business days (Monday – Friday). Except for Monday, we only see values over 5,000 during business days. Traffic is lighter on weekends, with values below 4,000 cars.\n",
    "\n",
    "## Time Indicators (III)\n",
    "\n",
    "Let's now see what values we have based on time of the day. The weekends, however, will drag down the average values, so we're going to look only at the averages separately."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 21312 characters]\n",
      "text/plain": [
       "<Figure size 792x252 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "day['hour'] = day['date_time'].dt.hour\n",
    "bussiness_days = day.copy()[day['dayofweek'] <= 4] # 4 == Friday\n",
    "weekend = day.copy()[day['dayofweek'] >= 5] # 5 = Saturday\n",
    "by_hour_business = bussiness_days.groupby('hour').mean()\n",
    "by_hour_weekend = weekend.groupby('hour').mean()\n",
    "\n",
    "\n",
    "plt.figure(figsize=(11,3.5))\n",
    "\n",
    "plt.subplot(1, 2, 1)\n",
    "by_hour_business['traffic_volume'].plot.line()\n",
    "plt.xlim(6,20)\n",
    "plt.ylim(1500,6500)\n",
    "plt.title('Traffic Volume By Hour: Monday–Friday')\n",
    "\n",
    "plt.subplot(1, 2, 2)\n",
    "by_hour_weekend['traffic_volume'].plot.line()\n",
    "plt.xlim(6,20)\n",
    "plt.ylim(1500,6500)\n",
    "plt.title('Traffic Volume By Hour: Weekend')\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "At each hour of the day, the traffic volume is generally higher during business days compared to the weekends. As somehow expected, the rush hours are around 7 and 16 — when most people travel from home to work and back. We see volumes of over 6,000 cars at rush hours.\n",
    "\n",
    "To summarize, we found a few time-related indicators of heavy traffic:\n",
    "\n",
    "- The traffic is usually heavier during warm months (March–October) compared to cold months (November–February).\n",
    "- The traffic is usually heavier on business days compared to weekends.\n",
    "- On business days, the rush hours are around 7 and 16.\n",
    "\n",
    "## Weather Indicators\n",
    "\n",
    "Another possible indicator of heavy traffic is weather. The dataset provides us with a few useful columns about weather: `temp`, `rain_1h`, `snow_1h`, `clouds_all`, `weather_main`, `weather_description`.\n",
    "\n",
    "A few of these columns are numerical, so let's start by looking up their correlation values with `traffic_volume`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "temp              0.128317\n",
       "rain_1h           0.003697\n",
       "snow_1h           0.001265\n",
       "clouds_all       -0.032932\n",
       "traffic_volume    1.000000\n",
       "month            -0.022337\n",
       "year             -0.003557\n",
       "dayofweek        -0.416453\n",
       "hour              0.172704\n",
       "Name: traffic_volume, dtype: float64"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "day.corr()['traffic_volume']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Temperature shows the strongest correlation with a value of just +0.13. The other relevant columns (`rain_1h`, `snow_1h`, `clouds_all`) don't show any strong correlation with `traffic_value`.\n",
    "\n",
    "Let's generate a scatter plot to visualize the correlation between `temp` and `traffic_volume`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 31556 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "day.plot.scatter('traffic_volume', 'temp')\n",
    "plt.ylim(230, 320) # two wrong 0K temperatures mess up the y-axis\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can conclude that temperature doesn't look like a solid indicator of heavy traffic.\n",
    "\n",
    "Let's now look at the other weather-related columns: `weather_main` and `weather_description`.\n",
    "\n",
    "\n",
    "## Weather Types\n",
    "\n",
    "To start, we're going to group the data by `weather_main` and look at the `traffic_volume` averages."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 10240 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "by_weather_main = day.groupby('weather_main').mean()\n",
    "by_weather_main['traffic_volume'].plot.barh()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It looks like there's no weather type where traffic volume exceeds 5,000 cars. This makes finding a heavy traffic indicator more difficult. Let's also group by `weather_description`, which has a more granular weather classification."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 37604 characters]\n",
      "text/plain": [
       "<Figure size 360x720 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "by_weather_description = day.groupby('weather_description').mean()\n",
    "by_weather_description['traffic_volume'].plot.barh(figsize=(5,10))\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It looks like there are three weather types where traffic volume exceeds 5,000:\n",
    "\n",
    "- Shower snow\n",
    "- Light rain and snow\n",
    "- Proximity thunderstorm with drizzle\n",
    "\n",
    "It's not clear why these weather types have the highest average traffic values — this is bad weather, but not that bad. Perhaps more people take their cars out of the garage when the weather is bad instead of riding a bike or walking.\n",
    "\n",
    "## Conclusion\n",
    "\n",
    "In this project, we tried to find a few indicators of heavy traffic on the I-94 Interstate highway. We managed to find two types of indicators:\n",
    "\n",
    "- Time indicators\n",
    "    - The traffic is usually heavier during warm months (March–October) compared to cold months (November–February).\n",
    "    - The traffic is usually heavier on business days compared to the weekends.\n",
    "    - On business days, the rush hours are around 7 and 16.\n",
    "- Weather indicators\n",
    "    - Shower snow\n",
    "    - Light rain and snow\n",
    "    - Proximity thunderstorm with drizzle"
   ]
  }
 ],
 "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.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
