{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "258c9132", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "import scipy as sp\n", "import sklearn as sk\n", "import datetime\n", "import calendar\n", "from jupyter_dash import JupyterDash\n", "import dash\n", "from dash import Dash, html, dcc, Input, Output\n", "import plotly.express as px\n", "from plotly.subplots import make_subplots\n", "import plotly.graph_objects as go\n", "import requests\n", "import io\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "e7a1d236", "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", "
datetimeviewcarmotorcyclelarge_vehicle
02023-02-1422:36:03View_from_Second_Link_at_Tuas001
12023-02-1422:36:03View_from_Tuas_Checkpoint200
22023-02-1422:36:03View_from_Woodlands_Causeway_Towards_Johor200
32023-02-1422:36:03View_from_Woodlands_Checkpoint_Towards_BKE301
42023-02-1423:14:34View_from_Second_Link_at_Tuas006
\n", "
" ], "text/plain": [ " date time view car \\\n", "0 2023-02-14 22:36:03 View_from_Second_Link_at_Tuas 0 \n", "1 2023-02-14 22:36:03 View_from_Tuas_Checkpoint 2 \n", "2 2023-02-14 22:36:03 View_from_Woodlands_Causeway_Towards_Johor 2 \n", "3 2023-02-14 22:36:03 View_from_Woodlands_Checkpoint_Towards_BKE 3 \n", "4 2023-02-14 23:14:34 View_from_Second_Link_at_Tuas 0 \n", "\n", " motorcycle large_vehicle \n", "0 0 1 \n", "1 0 0 \n", "2 0 0 \n", "3 0 1 \n", "4 0 6 " ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "(6960, 6)\n" ] } ], "source": [ "url = \"https://raw.githubusercontent.com/tappyness1/causion/main/data/counts_dataset.csv\"\n", "\n", "download = requests.get(url).content\n", "df = pd.read_csv(io.StringIO(download.decode('utf-8')))\n", "display(df.head())\n", "print(df.shape)" ] }, { "cell_type": "code", "execution_count": 3, "id": "dff31b99", "metadata": {}, "outputs": [], "source": [ "#Data manipulation\n", "\n", "df['date'] = pd.to_datetime(df['date'], format = \"%Y-%m-%d\")\n", "df['day'] = df['date'].dt.day_name()\n", "df['hour'] = df['time'].str[:2] + ':00'\n", "df.drop(columns=['motorcycle'], axis=1, inplace=True)\n", "df['vehicle'] = df['car'] + df['large_vehicle']" ] }, { "cell_type": "code", "execution_count": 4, "id": "8545eff0", "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", "
datetimeviewcarlarge_vehicledayhourvehicle
02023-02-1422:36:03View_from_Second_Link_at_Tuas01Tuesday22:001
12023-02-1422:36:03View_from_Tuas_Checkpoint20Tuesday22:002
22023-02-1422:36:03View_from_Woodlands_Causeway_Towards_Johor20Tuesday22:002
32023-02-1422:36:03View_from_Woodlands_Checkpoint_Towards_BKE31Tuesday22:004
42023-02-1423:14:34View_from_Second_Link_at_Tuas06Tuesday23:006
\n", "
" ], "text/plain": [ " date time view car \\\n", "0 2023-02-14 22:36:03 View_from_Second_Link_at_Tuas 0 \n", "1 2023-02-14 22:36:03 View_from_Tuas_Checkpoint 2 \n", "2 2023-02-14 22:36:03 View_from_Woodlands_Causeway_Towards_Johor 2 \n", "3 2023-02-14 22:36:03 View_from_Woodlands_Checkpoint_Towards_BKE 3 \n", "4 2023-02-14 23:14:34 View_from_Second_Link_at_Tuas 0 \n", "\n", " large_vehicle day hour vehicle \n", "0 1 Tuesday 22:00 1 \n", "1 0 Tuesday 22:00 2 \n", "2 0 Tuesday 22:00 2 \n", "3 1 Tuesday 22:00 4 \n", "4 6 Tuesday 23:00 6 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "display(df.head())" ] }, { "cell_type": "code", "execution_count": 5, "id": "e567f58d", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\Users\\neoce\\AppData\\Local\\Temp\\ipykernel_26408\\1176758689.py:2: FutureWarning: The default value of numeric_only in DataFrameGroupBy.sum is deprecated. In a future version, numeric_only will default to False. Either specify numeric_only or select only columns which should be valid for the function.\n", " new_df = df.groupby(['day']).sum().reset_index()\n" ] }, { "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", "
daycarlarge_vehiclevehicle
1Monday240610643470
5Tuesday20038112814
6Wednesday19428642806
4Thursday19769032879
0Friday20707622832
2Saturday21175782695
3Sunday15154281943
\n", "
" ], "text/plain": [ " day car large_vehicle vehicle\n", "1 Monday 2406 1064 3470\n", "5 Tuesday 2003 811 2814\n", "6 Wednesday 1942 864 2806\n", "4 Thursday 1976 903 2879\n", "0 Friday 2070 762 2832\n", "2 Saturday 2117 578 2695\n", "3 Sunday 1515 428 1943" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cat = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday','Saturday', 'Sunday']\n", "new_df = df.groupby(['day']).sum().reset_index()\n", "new_df = new_df.reindex([1,5,6,4,0,2,3])\n", "new_df.head(10)" ] }, { "cell_type": "code", "execution_count": 6, "id": "b2fc096f", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\Users\\neoce\\AppData\\Local\\Temp\\ipykernel_26408\\2911116693.py:1: FutureWarning: The default value of numeric_only in DataFrameGroupBy.sum is deprecated. In a future version, numeric_only will default to False. Either specify numeric_only or select only columns which should be valid for the function.\n", " new = df.groupby(['hour','day']).sum().drop(columns=['car', \"large_vehicle\"]).reset_index()\n" ] }, { "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", "
hourdayvehicle
000:00Friday44
100:00Monday52
200:00Saturday50
300:00Sunday61
400:00Thursday22
............
16323:00Saturday57
16423:00Sunday62
16523:00Thursday51
16623:00Tuesday49
16723:00Wednesday80
\n", "

168 rows × 3 columns

\n", "
" ], "text/plain": [ " hour day vehicle\n", "0 00:00 Friday 44\n", "1 00:00 Monday 52\n", "2 00:00 Saturday 50\n", "3 00:00 Sunday 61\n", "4 00:00 Thursday 22\n", ".. ... ... ...\n", "163 23:00 Saturday 57\n", "164 23:00 Sunday 62\n", "165 23:00 Thursday 51\n", "166 23:00 Tuesday 49\n", "167 23:00 Wednesday 80\n", "\n", "[168 rows x 3 columns]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "new = df.groupby(['hour','day']).sum().drop(columns=['car', \"large_vehicle\"]).reset_index()\n", "display(new)" ] }, { "cell_type": "code", "execution_count": 7, "id": "b7d4291c", "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", " \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", " \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", "
hourday00:0001:0002:0003:0004:0005:0006:0007:0008:00...14:0015:0016:0017:0018:0019:0020:0021:0022:0023:00
1Monday5282354029607723334...26814822725321425669583035
5Tuesday5830191415358514447...18620224320726516849404649
6Wednesday284118171626579623...18222619228027116335423280
4Thursday2238181836447524966...11113019722518416357454551
0Friday443731332836651430...28124525521821519158455669
2Saturday5043242130485318937...17021417020927116461544857
3Sunday613816211528451494...132100171989610550505662
\n", "

7 rows × 25 columns

\n", "
" ], "text/plain": [ "hour day 00:00 01:00 02:00 03:00 04:00 05:00 06:00 07:00 \\\n", "1 Monday 52 82 35 40 29 60 77 233 \n", "5 Tuesday 58 30 19 14 15 35 85 144 \n", "6 Wednesday 28 41 18 17 16 26 57 96 \n", "4 Thursday 22 38 18 18 36 44 75 249 \n", "0 Friday 44 37 31 33 28 36 65 143 \n", "2 Saturday 50 43 24 21 30 48 53 189 \n", "3 Sunday 61 38 16 21 15 28 45 149 \n", "\n", "hour 08:00 ... 14:00 15:00 16:00 17:00 18:00 19:00 20:00 21:00 \\\n", "1 34 ... 268 148 227 253 214 256 69 58 \n", "5 47 ... 186 202 243 207 265 168 49 40 \n", "6 23 ... 182 226 192 280 271 163 35 42 \n", "4 66 ... 111 130 197 225 184 163 57 45 \n", "0 0 ... 281 245 255 218 215 191 58 45 \n", "2 37 ... 170 214 170 209 271 164 61 54 \n", "3 4 ... 132 100 171 98 96 105 50 50 \n", "\n", "hour 22:00 23:00 \n", "1 30 35 \n", "5 46 49 \n", "6 32 80 \n", "4 45 51 \n", "0 56 69 \n", "2 48 57 \n", "3 56 62 \n", "\n", "[7 rows x 25 columns]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#Pivot the table\n", "\n", "table = pd.pivot_table(new, values='vehicle', index=['day'], columns=['hour']).reset_index()\n", "table = table.reindex([1,5,6,4,0,2,3])\n", "display(table)" ] }, { "cell_type": "code", "execution_count": 8, "id": "209f0452", "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", "
hourMondayTuesdayWednesdayThursdayFridaySaturdaySunday
000:0052582822445061
101:0082304138374338
202:0035191818312416
303:0040141718332121
404:0029151636283015
\n", "
" ], "text/plain": [ " hour Monday Tuesday Wednesday Thursday Friday Saturday Sunday\n", "0 00:00 52 58 28 22 44 50 61\n", "1 01:00 82 30 41 38 37 43 38\n", "2 02:00 35 19 18 18 31 24 16\n", "3 03:00 40 14 17 18 33 21 21\n", "4 04:00 29 15 16 36 28 30 15" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "t = table.T\n", "t.drop('day', inplace=True)\n", "t.columns = [\"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\",\n", " \"Saturday\", \"Sunday\"]\n", "t = t.reset_index()\n", "display(t.head())" ] }, { "cell_type": "code", "execution_count": 9, "id": "5c88c289", "metadata": {}, "outputs": [ { "data": { "application/vnd.plotly.v1+json": { "config": { "plotlyServerURL": "https://plot.ly" }, "data": [ { "alignmentgroup": "True", "hovertemplate": "Day of the Week=%{x}
Vehicle Count=%{y}", "legendgroup": "Monday", "marker": { "color": "#636efa", "pattern": { "shape": "" } }, "name": "Monday", "offsetgroup": "Monday", "orientation": "v", "showlegend": true, "textposition": "auto", "texttemplate": "%{y}", "type": "bar", "x": [ "Monday" ], "xaxis": "x", "y": [ 3470 ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Day of the Week=%{x}
Vehicle Count=%{y}", "legendgroup": "Tuesday", "marker": { "color": "#EF553B", "pattern": { "shape": "" } }, "name": "Tuesday", "offsetgroup": "Tuesday", "orientation": "v", "showlegend": true, "textposition": "auto", "texttemplate": "%{y}", "type": "bar", "x": [ "Tuesday" ], "xaxis": "x", "y": [ 2814 ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Day of the Week=%{x}
Vehicle Count=%{y}", "legendgroup": "Wednesday", "marker": { "color": "#00cc96", "pattern": { "shape": "" } }, "name": "Wednesday", "offsetgroup": "Wednesday", "orientation": "v", "showlegend": true, "textposition": "auto", "texttemplate": "%{y}", "type": "bar", "x": [ "Wednesday" ], "xaxis": "x", "y": [ 2806 ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Day of the Week=%{x}
Vehicle Count=%{y}", "legendgroup": "Thursday", "marker": { "color": "#ab63fa", "pattern": { "shape": "" } }, "name": "Thursday", "offsetgroup": "Thursday", "orientation": "v", "showlegend": true, "textposition": "auto", "texttemplate": "%{y}", "type": "bar", "x": [ "Thursday" ], "xaxis": "x", "y": [ 2879 ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Day of the Week=%{x}
Vehicle Count=%{y}", "legendgroup": "Friday", "marker": { "color": "#FFA15A", "pattern": { "shape": "" } }, "name": "Friday", "offsetgroup": "Friday", "orientation": "v", "showlegend": true, "textposition": "auto", "texttemplate": "%{y}", "type": "bar", "x": [ "Friday" ], "xaxis": "x", "y": [ 2832 ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Day of the Week=%{x}
Vehicle Count=%{y}", "legendgroup": "Saturday", "marker": { "color": "#19d3f3", "pattern": { "shape": "" } }, "name": "Saturday", "offsetgroup": "Saturday", "orientation": "v", "showlegend": true, "textposition": "auto", "texttemplate": "%{y}", "type": "bar", "x": [ "Saturday" ], "xaxis": "x", "y": [ 2695 ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Day of the Week=%{x}
Vehicle Count=%{y}", "legendgroup": "Sunday", "marker": { "color": "#FF6692", "pattern": { "shape": "" } }, "name": "Sunday", "offsetgroup": "Sunday", "orientation": "v", "showlegend": true, "textposition": "auto", "texttemplate": "%{y}", "type": "bar", "x": [ "Sunday" ], "xaxis": "x", "y": [ 1943 ], "yaxis": "y" } ], "layout": { "barmode": "relative", "legend": { "title": { "text": "Day of the Week" }, "tracegroupgap": 0 }, "margin": { "t": 60 }, "template": { "data": { "bar": [ { "error_x": { "color": "#2a3f5f" }, "error_y": { "color": "#2a3f5f" }, "marker": { "line": { "color": "#E5ECF6", "width": 0.5 }, "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "bar" } ], "barpolar": [ { "marker": { "line": { "color": "#E5ECF6", "width": 0.5 }, "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "barpolar" } ], "carpet": [ { "aaxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "baxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "type": "carpet" } ], "choropleth": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "choropleth" } ], "contour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "contour" } ], "contourcarpet": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "contourcarpet" } ], "heatmap": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmap" } ], "heatmapgl": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmapgl" } ], "histogram": [ { "marker": { "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "histogram" } ], "histogram2d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2d" } ], "histogram2dcontour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2dcontour" } ], "mesh3d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "mesh3d" } ], "parcoords": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "parcoords" } ], "pie": [ { "automargin": true, "type": "pie" } ], "scatter": [ { "fillpattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 }, "type": "scatter" } ], "scatter3d": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatter3d" } ], "scattercarpet": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattercarpet" } ], "scattergeo": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergeo" } ], "scattergl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergl" } ], "scattermapbox": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattermapbox" } ], "scatterpolar": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolar" } ], "scatterpolargl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolargl" } ], "scatterternary": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterternary" } ], "surface": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "surface" } ], "table": [ { "cells": { "fill": { "color": "#EBF0F8" }, "line": { "color": "white" } }, "header": { "fill": { "color": "#C8D4E3" }, "line": { "color": "white" } }, "type": "table" } ] }, "layout": { "annotationdefaults": { "arrowcolor": "#2a3f5f", "arrowhead": 0, "arrowwidth": 1 }, "autotypenumbers": "strict", "coloraxis": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "colorscale": { "diverging": [ [ 0, "#8e0152" ], [ 0.1, "#c51b7d" ], [ 0.2, "#de77ae" ], [ 0.3, "#f1b6da" ], [ 0.4, "#fde0ef" ], [ 0.5, "#f7f7f7" ], [ 0.6, "#e6f5d0" ], [ 0.7, "#b8e186" ], [ 0.8, "#7fbc41" ], [ 0.9, "#4d9221" ], [ 1, "#276419" ] ], "sequential": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "sequentialminus": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ] }, "colorway": [ "#636efa", "#EF553B", "#00cc96", "#ab63fa", "#FFA15A", "#19d3f3", "#FF6692", "#B6E880", "#FF97FF", "#FECB52" ], "font": { "color": "#2a3f5f" }, "geo": { "bgcolor": "white", "lakecolor": "white", "landcolor": "#E5ECF6", "showlakes": true, "showland": true, "subunitcolor": "white" }, "hoverlabel": { "align": "left" }, "hovermode": "closest", "mapbox": { "style": "light" }, "paper_bgcolor": "white", "plot_bgcolor": "#E5ECF6", "polar": { "angularaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "radialaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "scene": { "xaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "yaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "zaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" } }, "shapedefaults": { "line": { "color": "#2a3f5f" } }, "ternary": { "aaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "baxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "caxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "title": { "x": 0.05 }, "xaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 }, "yaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 } } }, "xaxis": { "anchor": "y", "categoryarray": [ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" ], "categoryorder": "array", "domain": [ 0, 1 ], "title": { "text": "Day of the Week" } }, "yaxis": { "anchor": "x", "domain": [ 0, 1 ], "title": { "text": "Vehicle Count" } } } } }, "metadata": {}, "output_type": "display_data" } ], "source": [ "fig = px.bar(new_df, x = 'day', y='vehicle', color='day', \n", " text_auto=True, labels={'day':'Day of the Week','vehicle':'Vehicle Count'})\n", "fig.show()" ] }, { "cell_type": "code", "execution_count": 10, "id": "269298dd", "metadata": {}, "outputs": [ { "data": { "application/vnd.plotly.v1+json": { "config": { "plotlyServerURL": "https://plot.ly" }, "data": [ { "coloraxis": "coloraxis", "hovertemplate": "Hour of the Day: %{x}
Day of the Week: %{y}
Causeway Traffic: %{z}", "name": "0", "texttemplate": "%{z}", "type": "heatmap", "x": [ "12am", "1am", "2am", "3am", "4am", "5am", "6am", "7am", "8am", "9am", "10am", "11am", "12pm", "1pm", "2pm", "3pm", "4pm", "5pm", "6pm", "7pm", "8pm", "9pm", "10pm", "11pm" ], "xaxis": "x", "y": [ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" ], "yaxis": "y", "z": [ [ 52, 82, 35, 40, 29, 60, 77, 233, 34, 207, 296, 265, 265, 237, 268, 148, 227, 253, 214, 256, 69, 58, 30, 35 ], [ 58, 30, 19, 14, 15, 35, 85, 144, 47, 154, 227, 226, 192, 113, 186, 202, 243, 207, 265, 168, 49, 40, 46, 49 ], [ 28, 41, 18, 17, 16, 26, 57, 96, 23, 142, 285, 202, 204, 148, 182, 226, 192, 280, 271, 163, 35, 42, 32, 80 ], [ 22, 38, 18, 18, 36, 44, 75, 249, 66, 239, 285, 244, 171, 166, 111, 130, 197, 225, 184, 163, 57, 45, 45, 51 ], [ 44, 37, 31, 33, 28, 36, 65, 143, 0, 77, 200, 160, 162, 183, 281, 245, 255, 218, 215, 191, 58, 45, 56, 69 ], [ 50, 43, 24, 21, 30, 48, 53, 189, 37, 127, 235, 141, 93, 186, 170, 214, 170, 209, 271, 164, 61, 54, 48, 57 ], [ 61, 38, 16, 21, 15, 28, 45, 149, 4, 151, 127, 88, 140, 140, 132, 100, 171, 98, 96, 105, 50, 50, 56, 62 ] ] } ], "layout": { "coloraxis": { "colorbar": { "title": { "text": "Causeway Traffic" } }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ] }, "margin": { "t": 60 }, "template": { "data": { "bar": [ { "error_x": { "color": "#2a3f5f" }, "error_y": { "color": "#2a3f5f" }, "marker": { "line": { "color": "#E5ECF6", "width": 0.5 }, "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "bar" } ], "barpolar": [ { "marker": { "line": { "color": "#E5ECF6", "width": 0.5 }, "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "barpolar" } ], "carpet": [ { "aaxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "baxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "type": "carpet" } ], "choropleth": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "choropleth" } ], "contour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "contour" } ], "contourcarpet": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "contourcarpet" } ], "heatmap": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmap" } ], "heatmapgl": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmapgl" } ], "histogram": [ { "marker": { "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "histogram" } ], "histogram2d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2d" } ], "histogram2dcontour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2dcontour" } ], "mesh3d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "mesh3d" } ], "parcoords": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "parcoords" } ], "pie": [ { "automargin": true, "type": "pie" } ], "scatter": [ { "fillpattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 }, "type": "scatter" } ], "scatter3d": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatter3d" } ], "scattercarpet": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattercarpet" } ], "scattergeo": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergeo" } ], "scattergl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergl" } ], "scattermapbox": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattermapbox" } ], "scatterpolar": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolar" } ], "scatterpolargl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolargl" } ], "scatterternary": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterternary" } ], "surface": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "surface" } ], "table": [ { "cells": { "fill": { "color": "#EBF0F8" }, "line": { "color": "white" } }, "header": { "fill": { "color": "#C8D4E3" }, "line": { "color": "white" } }, "type": "table" } ] }, "layout": { "annotationdefaults": { "arrowcolor": "#2a3f5f", "arrowhead": 0, "arrowwidth": 1 }, "autotypenumbers": "strict", "coloraxis": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "colorscale": { "diverging": [ [ 0, "#8e0152" ], [ 0.1, "#c51b7d" ], [ 0.2, "#de77ae" ], [ 0.3, "#f1b6da" ], [ 0.4, "#fde0ef" ], [ 0.5, "#f7f7f7" ], [ 0.6, "#e6f5d0" ], [ 0.7, "#b8e186" ], [ 0.8, "#7fbc41" ], [ 0.9, "#4d9221" ], [ 1, "#276419" ] ], "sequential": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "sequentialminus": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ] }, "colorway": [ "#636efa", "#EF553B", "#00cc96", "#ab63fa", "#FFA15A", "#19d3f3", "#FF6692", "#B6E880", "#FF97FF", "#FECB52" ], "font": { "color": "#2a3f5f" }, "geo": { "bgcolor": "white", "lakecolor": "white", "landcolor": "#E5ECF6", "showlakes": true, "showland": true, "subunitcolor": "white" }, "hoverlabel": { "align": "left" }, "hovermode": "closest", "mapbox": { "style": "light" }, "paper_bgcolor": "white", "plot_bgcolor": "#E5ECF6", "polar": { "angularaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "radialaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "scene": { "xaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "yaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "zaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" } }, "shapedefaults": { "line": { "color": "#2a3f5f" } }, "ternary": { "aaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "baxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "caxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "title": { "x": 0.05 }, "xaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 }, "yaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 } } }, "xaxis": { "anchor": "y", "constrain": "domain", "domain": [ 0, 1 ], "scaleanchor": "y", "side": "top", "title": { "text": "Hour of the Day" } }, "yaxis": { "anchor": "x", "autorange": "reversed", "constrain": "domain", "domain": [ 0, 1 ], "title": { "text": "Day of the Week" } } } } }, "metadata": {}, "output_type": "display_data" } ], "source": [ "new_table = table.iloc[:,1:].to_numpy()\n", "fig1 = px.imshow(new_table, labels=dict(x=\"Hour of the Day\", y = 'Day of the Week', color='Causeway Traffic'),\n", " x=['12am', '1am', '2am', '3am', '4am', '5am', '6am', '7am', '8am', '9am', '10am', '11am', '12pm',\n", " '1pm', '2pm', '3pm', '4pm', '5pm', '6pm', '7pm', '8pm', '9pm', '10pm', \"11pm\"],\n", " y=[\"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\",\n", " \"Saturday\", \"Sunday\"], text_auto=True)\n", "fig1.update_xaxes(side='top')" ] }, { "cell_type": "code", "execution_count": 11, "id": "928a2063", "metadata": {}, "outputs": [], "source": [ "#fig = make_subplots(rows=1, cols=2, specs=[[{'type':'bar'},{'type':'bar'}]],\n", " #subplot_titles=('Hours', 'Days'))" ] }, { "cell_type": "code", "execution_count": 12, "id": "b622fd70", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Dash is running on http://127.0.0.1:8050/\n", "\n" ] }, { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "app_new = JupyterDash(__name__)\n", "\n", "app_new.title = 'CSE6242 Dashboard'\n", "app_new.layout = html.Div([\n", " html.Div(html.H2(\"Causian Dashboard\"), style={'width':'250px', 'height':'60px', 'padding-left':'2%',\n", " 'display':'inline-block'}),\n", " html.Div([\n", " html.Label(\"Hours\"), dcc.Dropdown(id='hours_dropdown_id',\n", " options=['00:00', '01:00', '02:00', '03:00', '04:00', '05:00', '06:00', '07:00', '08:00', '09:00', \n", " '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00','19:00',\n", " '20:00', '21:00', '22:00', '23:00'],\n", " value='07:00', clearable=False)],\n", " style={'width':'20%','height':'60px', 'padding-left':'2%',\n", " 'display':'inline-block'}),\n", " html.Div([html.Label(\"Day of the Week\"), dcc.Dropdown(id='days_dropdown_id', value='Monday',\n", " options=[\"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\",\"Saturday\", \"Sunday\"],\n", " clearable=False)],\n", " style={'width':'20%','height':'60px', 'padding-left':'2%',\n", " 'display':'inline-block'}),\n", " html.Div(dcc.Graph(id='fig_hours')),\n", " html.Div(dcc.Graph(id='fig_days')),\n", " html.Div(dcc.Graph(id='fig_heatplot', figure=fig1))])\n", "\n", "@app_new.callback(Output('fig_hours', \"figure\"), Input('hours_dropdown_id', \"value\"))\n", "def update_hour_bar_chart(Hours):\n", " fig_hours = px.bar(table, x='day', y=str(Hours), color='day', text_auto=True, labels={'day':\"Day of the Week\"})\n", " return fig_hours\n", "@app_new.callback(Output('fig_days', \"figure\"), Input('days_dropdown_id', \"value\"))\n", "def update_day_bar_chart(day):\n", " fig_days = px.bar(t, x='hour', y = str(day), color=str(day), text_auto=True, labels={'hour':\"Count of Each Hour\"})\n", " return fig_days\n", "\n", "app_new.run_server(mode='inline')" ] }, { "cell_type": "code", "execution_count": 13, "id": "66f580aa", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "RangeIndex: 168 entries, 0 to 167\n", "Data columns (total 3 columns):\n", " # Column Non-Null Count Dtype \n", "--- ------ -------------- ----- \n", " 0 hour 168 non-null category\n", " 1 day 168 non-null category\n", " 2 vehicle 168 non-null int64 \n", "dtypes: category(2), int64(1)\n", "memory usage: 2.8 KB\n", "None\n" ] } ], "source": [ "#Trial basic Linear Regression Model\n", "new['hour'] = new['hour'].astype('category')\n", "new['day'] = new['day'].astype('category')\n", "print(new.info())" ] }, { "cell_type": "code", "execution_count": null, "id": "4858f7d3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 14, "id": "0806a79a", "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", " \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", "
hour_00:00hour_01:00hour_02:00hour_03:00hour_04:00hour_05:00hour_06:00hour_07:00hour_08:00hour_09:00...hour_21:00hour_22:00hour_23:00day_Fridayday_Mondayday_Saturdayday_Sundayday_Thursdayday_Tuesdayday_Wednesday
01000000000...0001000000
11000000000...0000100000
21000000000...0000010000
31000000000...0000001000
41000000000...0000000100
\n", "

5 rows × 31 columns

\n", "
" ], "text/plain": [ " hour_00:00 hour_01:00 hour_02:00 hour_03:00 hour_04:00 hour_05:00 \\\n", "0 1 0 0 0 0 0 \n", "1 1 0 0 0 0 0 \n", "2 1 0 0 0 0 0 \n", "3 1 0 0 0 0 0 \n", "4 1 0 0 0 0 0 \n", "\n", " hour_06:00 hour_07:00 hour_08:00 hour_09:00 ... hour_21:00 \\\n", "0 0 0 0 0 ... 0 \n", "1 0 0 0 0 ... 0 \n", "2 0 0 0 0 ... 0 \n", "3 0 0 0 0 ... 0 \n", "4 0 0 0 0 ... 0 \n", "\n", " hour_22:00 hour_23:00 day_Friday day_Monday day_Saturday day_Sunday \\\n", "0 0 0 1 0 0 0 \n", "1 0 0 0 1 0 0 \n", "2 0 0 0 0 1 0 \n", "3 0 0 0 0 0 1 \n", "4 0 0 0 0 0 0 \n", "\n", " day_Thursday day_Tuesday day_Wednesday \n", "0 0 0 0 \n", "1 0 0 0 \n", "2 0 0 0 \n", "3 0 0 0 \n", "4 1 0 0 \n", "\n", "[5 rows x 31 columns]" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "(168, 31)\n", "(168,)\n" ] } ], "source": [ "from sklearn.linear_model import LinearRegression\n", "\n", "X = new.loc[:,['hour', 'day']]\n", "y = new.loc[:,'vehicle']\n", "n = pd.get_dummies(X)\n", "display(n.head())\n", "print(n.shape)\n", "print(y.shape)" ] }, { "cell_type": "code", "execution_count": 15, "id": "b6adf1cf", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.8552520624553817\n" ] } ], "source": [ "reg = LinearRegression().fit(n,y)\n", "print(reg.score(n,y))\n", "y_pred = reg.predict(n)\n", "\n", "new_pred = []\n", "for i in y_pred:\n", " if i < 0:\n", " new_pred.append(0)\n", " else:\n", " new_pred.append(i)\n" ] }, { "cell_type": "code", "execution_count": 16, "id": "4fb37621", "metadata": {}, "outputs": [], "source": [ "import sklearn.metrics as metrics\n", "def regression_results(y_true, y_pred):\n", "\n", " # Regression metrics\n", " explained_variance=metrics.explained_variance_score(y_true, y_pred)\n", " mean_absolute_error=metrics.mean_absolute_error(y_true, y_pred) \n", " mse=metrics.mean_squared_error(y_true, y_pred) \n", " mean_squared_log_error=metrics.mean_squared_log_error(y_true, y_pred)\n", " median_absolute_error=metrics.median_absolute_error(y_true, y_pred)\n", " r2=metrics.r2_score(y_true, y_pred)\n", "\n", " print('explained_variance: ', round(explained_variance,4)) \n", " print('mean_squared_log_error: ', round(mean_squared_log_error,4))\n", " print('r2: ', round(r2,4))\n", " print('MAE: ', round(mean_absolute_error,4))\n", " print('MSE: ', round(mse,4))\n", " print('RMSE: ', round(np.sqrt(mse),4))" ] }, { "cell_type": "code", "execution_count": 17, "id": "fc1bd361", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "explained_variance: 0.8567\n", "mean_squared_log_error: 0.3976\n", "r2: 0.8566\n", "MAE: 23.7455\n", "MSE: 1029.6179\n", "RMSE: 32.0877\n" ] } ], "source": [ "regression_results(y, new_pred)" ] }, { "cell_type": "code", "execution_count": null, "id": "49a4d65d", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.8.16" } }, "nbformat": 4, "nbformat_minor": 5 }