Skip to content

Instantly share code, notes, and snippets.

@LowriWilliams
Created May 20, 2020 10:01
Show Gist options
  • Save LowriWilliams/3bf30ab01ee3344a949804685c4e593d to your computer and use it in GitHub Desktop.
Save LowriWilliams/3bf30ab01ee3344a949804685c4e593d to your computer and use it in GitHub Desktop.
corona/forecast_cases
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<iframe srcdoc=\"\n",
" <!-- Load Charts.js -->\n",
" <script src='https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.bundle.min.js'></script>\n",
" \n",
" <canvas id='chart'></canvas>\n",
" <script>\n",
" var ctx = document.getElementById('chart').getContext('2d');\n",
" ctx.canvas.width = 800 - (.1 * 800);\n",
" ctx.canvas.height = 420 - (.15 * 420);\n",
" var myNewChart = new Chart(ctx,{ type: 'line', data: {\n",
" 'labels': [\n",
" '2020-01-22',\n",
" '2020-01-23',\n",
" '2020-01-24',\n",
" '2020-01-25',\n",
" '2020-01-26',\n",
" '2020-01-27',\n",
" '2020-01-28',\n",
" '2020-01-29',\n",
" '2020-01-30',\n",
" '2020-01-31',\n",
" '2020-02-01',\n",
" '2020-02-02',\n",
" '2020-02-03',\n",
" '2020-02-04',\n",
" '2020-02-05',\n",
" '2020-02-06',\n",
" '2020-02-07',\n",
" '2020-02-08',\n",
" '2020-02-09',\n",
" '2020-02-10',\n",
" '2020-02-11',\n",
" '2020-02-12',\n",
" '2020-02-13',\n",
" '2020-02-14',\n",
" '2020-02-15',\n",
" '2020-02-16',\n",
" '2020-02-17'\n",
" ],\n",
" 'datasets': [\n",
" {\n",
" 'data': [\n",
" 555,\n",
" 653,\n",
" 941,\n",
" 2019,\n",
" 2794,\n",
" 4473,\n",
" 6057,\n",
" 7783,\n",
" 9776,\n",
" 11374,\n",
" 14549,\n",
" 17295,\n",
" 20588,\n",
" 24503,\n",
" 24630,\n",
" 30806,\n",
" 31471,\n",
" 37488,\n",
" 40472,\n",
" 42632,\n",
" 44982\n",
" ],\n",
" 'label': 'Confirmed',\n",
" 'borderColor': '#FFCE00'\n",
" },\n",
" {\n",
" 'data': [\n",
" 'NaN',\n",
" 'NaN',\n",
" 'NaN',\n",
" 'NaN',\n",
" 'NaN',\n",
" 'NaN',\n",
" 'NaN',\n",
" 'NaN',\n",
" 'NaN',\n",
" 'NaN',\n",
" 'NaN',\n",
" 'NaN',\n",
" 'NaN',\n",
" 'NaN',\n",
" 'NaN',\n",
" 'NaN',\n",
" 'NaN',\n",
" 'NaN',\n",
" 'NaN',\n",
" 'NaN',\n",
" 44982,\n",
" 43807,\n",
" 44394,\n",
" 44100,\n",
" 44247,\n",
" 44173,\n",
" 44210\n",
" ],\n",
" 'label': 'Predicted',\n",
" 'borderColor': '#E63029'\n",
" },\n",
" {\n",
" 'data': [\n",
" 'NaN',\n",
" 'NaN',\n",
" 'NaN',\n",
" 'NaN',\n",
" 'NaN',\n",
" 'NaN',\n",
" 'NaN',\n",
" 'NaN',\n",
" 'NaN',\n",
" 'NaN',\n",
" 'NaN',\n",
" 'NaN',\n",
" 'NaN',\n",
" 'NaN',\n",
" 'NaN',\n",
" 'NaN',\n",
" 'NaN',\n",
" 'NaN',\n",
" 'NaN',\n",
" 'NaN',\n",
" 44982,\n",
" 60328,\n",
" 64422,\n",
" 66887,\n",
" 69032,\n",
" 71226,\n",
" 73260\n",
" ],\n",
" 'label': 'Actual',\n",
" 'borderColor': '#007849'\n",
" },\n",
" {\n",
" 'data': [\n",
" NaN,\n",
" 604.0,\n",
" 797.0,\n",
" 1480.0,\n",
" 2406.5,\n",
" 3633.5,\n",
" 5265.0,\n",
" 6920.0,\n",
" 8779.5,\n",
" 10575.0,\n",
" 12961.5,\n",
" 15922.0,\n",
" 18941.5,\n",
" 22545.5,\n",
" 24566.5,\n",
" 27718.0,\n",
" 31138.5,\n",
" 34479.5,\n",
" 38980.0,\n",
" 41552.0,\n",
" 43807.0\n",
" ],\n",
" 'label': 'Moving Average',\n",
" 'borderColor': '#1307ED'\n",
" }\n",
" ]\n",
"}, options: null });\n",
" </script>\n",
" \" src=\"\" width=\"800\" height=\"420\" frameborder=0 sandbox=\"allow-scripts\"></iframe>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Add a column to the dataframe including the moving average with of 2\n",
"\n",
"df_dates['MA'] = df_dates.iloc[:,1].rolling(window=2).mean()\n",
"\n",
"# Calculate the moving average for 4 days ahead using the last moving average and the last data point\n",
"\n",
"predictions = [int((df_dates['MA'].values.tolist()[-1])), int((df_dates['Confirmed'].values.tolist()[-1] + df_dates['MA'].values.tolist()[-1]) / 2)]\n",
"\n",
"for i in range(0, 4):\n",
" for j in zip(predictions, predictions[1:]):\n",
" ma = int((j[0] + j[1]) / 2)\n",
" predictions.append(ma)\n",
"\n",
"# Get the next 6 dates and assign them with their predictions\n",
"\n",
"df_predicted = pd.DataFrame(df_dates.Date + datetime.timedelta(days=6)).iloc[-6:]\n",
"df_predicted['Predicted'] = predictions\n",
"\n",
"# Merge all the dates into a list for graphing\n",
"\n",
"all_dates = df_dates['Date'].astype(str).tolist()\n",
"all_dates.extend(df_predicted['Date'].astype(str).tolist())\n",
"\n",
"# Preparing predicted data for graphing\n",
"\n",
"predicted_data = [\"NaN\"] * 20\n",
"predicted_data.append(df_dates['Confirmed'].values.tolist()[-1])\n",
"predicted_data.extend(df_predicted['Predicted'].values.tolist())\n",
"\n",
"# Graph the data \n",
"\n",
"data = { \n",
" 'labels': all_dates,\n",
" 'datasets': [{ \n",
" 'data': df_dates['Confirmed'].values.tolist(),\n",
" 'label': \"Confirmed\",\n",
" 'borderColor': \"#FFCE00\"\n",
" },\n",
" { \n",
" 'data': predicted_data,\n",
" 'label': \"Predicted\",\n",
" 'borderColor': \"#E63029\"\n",
" },\n",
" { \n",
" 'data': actual_data,\n",
" 'label': \"Actual\",\n",
" 'borderColor': \"#007849\"\n",
" },\n",
" { \n",
" 'data': df_dates['MA'].values.tolist(),\n",
" 'label': \"Moving Average\",\n",
" 'borderColor': \"#1307ED\"\n",
" }\n",
" ]\n",
" }\n",
"\n",
"chart_js = iplotter.ChartJSPlotter()\n",
"chart_js.plot(data, chart_type=\"line\")"
]
}
],
"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.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment