%%html
<iframe width="560" height="315" src="https://www.youtube.com/embed/6isr3mEpu44" frameborder="0" allow="accelerometer; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
%%html
<iframe width="560" height="315" src="https://www.youtube.com/embed/_rJVTaTWBwE" frameborder="0" allow="accelerometer; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
import requests
url = 'https://coronavirus-tracker-api.herokuapp.com/v2/locations?timelines=1'
r = requests.get(url)
print(type(r))
<class 'requests.models.Response'>
%%html
<iframe src='https://developer.spotify.com/documentation/web-api/' width='800' height='400'>
%%html
<iframe src='https://coronavirus-tracker-api.herokuapp.com/v2/locations?country_code=BR' width='800' height='100'>
%%html
<iframe width="560" height="315" src="https://www.youtube.com/embed/zGtpapHXswc" frameborder="0" allow="accelerometer; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
# Python Dictionary
r=r.json()
print(type(r))
<class 'dict'>
import pandas as pd
df = pd.DataFrame(r['locations']).set_index('country')
%%html
<iframe src='https://docs.python.org/3/tutorial/datastructures.html#dictionaries' width='800' height='400'>
%%html
<iframe src='https://pandas.pydata.org/docs/' width='800' height='400'>
%%html
<iframe src='https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html?highlight=dataframe' width='800' height='400'>
df.head()
coordinates | country_code | id | last_updated | latest | province | timelines | |
---|---|---|---|---|---|---|---|
country | |||||||
Thailand | {'latitude': '15', 'longitude': '101'} | TH | 0 | 2020-03-22T15:54:41.375723Z | {'confirmed': 411, 'deaths': 1, 'recovered': 42} | {'confirmed': {'latest': 411, 'timeline': {'20... | |
Japan | {'latitude': '36', 'longitude': '138'} | JP | 1 | 2020-03-22T15:54:41.383890Z | {'confirmed': 1007, 'deaths': 35, 'recovered':... | {'confirmed': {'latest': 1007, 'timeline': {'2... | |
Singapore | {'latitude': '1.2833', 'longitude': '103.8333'} | SG | 2 | 2020-03-22T15:54:41.392349Z | {'confirmed': 432, 'deaths': 2, 'recovered': 140} | {'confirmed': {'latest': 432, 'timeline': {'20... | |
Nepal | {'latitude': '28.1667', 'longitude': '84.25'} | NP | 3 | 2020-03-22T15:54:41.397230Z | {'confirmed': 1, 'deaths': 0, 'recovered': 1} | {'confirmed': {'latest': 1, 'timeline': {'2020... | |
Malaysia | {'latitude': '2.5', 'longitude': '112.5'} | MY | 4 | 2020-03-22T15:54:41.401656Z | {'confirmed': 1183, 'deaths': 4, 'recovered': ... | {'confirmed': {'latest': 1183, 'timeline': {'2... |
%%html
<iframe width="560" height="315" src="https://www.youtube.com/embed/Lf-FGnjqI4U" frameborder="0" allow="accelerometer; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
data = pd.DataFrame(df['timelines'])
data.head()
timelines | |
---|---|
country | |
Thailand | {'confirmed': {'latest': 411, 'timeline': {'20... |
Japan | {'confirmed': {'latest': 1007, 'timeline': {'2... |
Singapore | {'confirmed': {'latest': 432, 'timeline': {'20... |
Nepal | {'confirmed': {'latest': 1, 'timeline': {'2020... |
Malaysia | {'confirmed': {'latest': 1183, 'timeline': {'2... |
countries = ['Latvia','Italy', 'Germany', 'Spain', 'Brazil']
data=data.loc[countries,: ]
data.head()
timelines | |
---|---|
country | |
Latvia | {'confirmed': {'latest': 124, 'timeline': {'20... |
Italy | {'confirmed': {'latest': 53578, 'timeline': {'... |
Germany | {'confirmed': {'latest': 22213, 'timeline': {'... |
Spain | {'confirmed': {'latest': 25374, 'timeline': {'... |
Brazil | {'confirmed': {'latest': 1021, 'timeline': {'2... |
data.loc['Italy','timelines']['confirmed']
{'latest': 53578, 'timeline': {'2020-01-22T00:00:00Z': 0, '2020-01-23T00:00:00Z': 0, '2020-01-24T00:00:00Z': 0, '2020-01-25T00:00:00Z': 0, '2020-01-26T00:00:00Z': 0, '2020-01-27T00:00:00Z': 0, '2020-01-28T00:00:00Z': 0, '2020-01-29T00:00:00Z': 0, '2020-01-30T00:00:00Z': 0, '2020-01-31T00:00:00Z': 2, '2020-02-01T00:00:00Z': 2, '2020-02-02T00:00:00Z': 2, '2020-02-03T00:00:00Z': 2, '2020-02-04T00:00:00Z': 2, '2020-02-05T00:00:00Z': 2, '2020-02-06T00:00:00Z': 2, '2020-02-07T00:00:00Z': 3, '2020-02-08T00:00:00Z': 3, '2020-02-09T00:00:00Z': 3, '2020-02-10T00:00:00Z': 3, '2020-02-11T00:00:00Z': 3, '2020-02-12T00:00:00Z': 3, '2020-02-13T00:00:00Z': 3, '2020-02-14T00:00:00Z': 3, '2020-02-15T00:00:00Z': 3, '2020-02-16T00:00:00Z': 3, '2020-02-17T00:00:00Z': 3, '2020-02-18T00:00:00Z': 3, '2020-02-19T00:00:00Z': 3, '2020-02-20T00:00:00Z': 3, '2020-02-21T00:00:00Z': 20, '2020-02-22T00:00:00Z': 62, '2020-02-23T00:00:00Z': 155, '2020-02-24T00:00:00Z': 229, '2020-02-25T00:00:00Z': 322, '2020-02-26T00:00:00Z': 453, '2020-02-27T00:00:00Z': 655, '2020-02-28T00:00:00Z': 888, '2020-02-29T00:00:00Z': 1128, '2020-03-01T00:00:00Z': 1694, '2020-03-02T00:00:00Z': 2036, '2020-03-03T00:00:00Z': 2502, '2020-03-04T00:00:00Z': 3089, '2020-03-05T00:00:00Z': 3858, '2020-03-06T00:00:00Z': 4636, '2020-03-07T00:00:00Z': 5883, '2020-03-08T00:00:00Z': 7375, '2020-03-09T00:00:00Z': 9172, '2020-03-10T00:00:00Z': 10149, '2020-03-11T00:00:00Z': 12462, '2020-03-12T00:00:00Z': 12462, '2020-03-13T00:00:00Z': 17660, '2020-03-14T00:00:00Z': 21157, '2020-03-15T00:00:00Z': 24747, '2020-03-16T00:00:00Z': 27980, '2020-03-17T00:00:00Z': 31506, '2020-03-18T00:00:00Z': 35713, '2020-03-19T00:00:00Z': 41035, '2020-03-20T00:00:00Z': 47021, '2020-03-21T00:00:00Z': 53578}}
%%html
<iframe width="560" height="315" src="https://www.youtube.com/embed/tcbHKpT1bKg" frameborder="0" allow="accelerometer; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
Italy_confirmed = pd.Series(data.loc['Italy','timelines']['confirmed']['timeline'])
Italy_deaths = pd.Series(data.loc['Italy','timelines']['deaths']['timeline'])
day_first_death=(Italy_deaths == 0).sum()
Italy_confirmed = Italy_confirmed.iloc[day_first_death-14:].reset_index(drop=True)
Germany_confirmed = pd.Series(data.loc['Germany','timelines']['confirmed']['timeline'])
Germany_deaths = pd.Series(data.loc['Germany','timelines']['deaths']['timeline'])
day_first_death=(Germany_deaths == 0).sum()
Germany_confirmed = Germany_confirmed.iloc[day_first_death-14:].reset_index(drop=True)
Spain_confirmed = pd.Series(data.loc['Spain','timelines']['confirmed']['timeline'])
Spain_deaths = pd.Series(data.loc['Spain','timelines']['deaths']['timeline'])
day_first_death=(Spain_deaths == 0).sum()
Spain_confirmed = Spain_confirmed.iloc[day_first_death-14:].reset_index(drop=True)
Brazil_confirmed = pd.Series(data.loc['Brazil','timelines']['confirmed']['timeline'])
Brazil_deaths = pd.Series(data.loc['Brazil','timelines']['deaths']['timeline'])
day_first_death=(Brazil_deaths == 0).sum()
Brazil_confirmed = Brazil_confirmed.iloc[day_first_death-14:].reset_index(drop=True)
%%html
<iframe width="560" height="315" src="https://www.youtube.com/embed/5xZy9VcTloI" frameborder="0" allow="accelerometer; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
import numpy as np
def exponential_func(x, a, c, d):
return a*np.exp(c*x)+d
from scipy.optimize import curve_fit
days_to_predict=Italy_confirmed.size+5
y = np.array(Italy_confirmed.tolist())
x = np.arange(y.size)
popt, pcov = curve_fit(exponential_func, x, y)
Italy_cases_prediction = exponential_func(np.arange(days_to_predict), *popt)
y = np.array(Germany_confirmed.tolist())
x = np.arange(y.size)
popt, pcov = curve_fit(exponential_func, x, y)
Germany_cases_prediction = exponential_func(np.arange(days_to_predict), *popt)
y = np.array(Spain_confirmed.tolist())
x = np.arange(y.size)
popt, pcov = curve_fit(exponential_func, x, y)
Spain_cases_prediction = exponential_func(np.arange(days_to_predict), *popt)
y = np.array(Brazil_confirmed.tolist())
x = np.arange(y.size)
popt, pcov = curve_fit(exponential_func, x, y)
Brazil_cases_prediction = exponential_func(np.arange(days_to_predict), *popt)
%%html
<iframe width="560" height="315" src="https://www.youtube.com/embed/kbYlAgqZhfQ" frameborder="0" allow="accelerometer; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
Italy_deaths = np.trim_zeros(np.array(Italy_deaths.tolist()))
Italy_death_rate=Italy_deaths/np.array(Italy_confirmed.tolist()[14:])
days_to_predict=Italy_deaths.size+5
y = Italy_deaths
x = np.arange(y.size)
popt, pcov = curve_fit(exponential_func, x, y)
Italy_deaths_prediction = exponential_func(np.arange(days_to_predict), *popt)
Spain_deaths = np.trim_zeros(np.array(Spain_deaths.tolist()))
Spain_death_rate=Spain_deaths/np.array(Spain_confirmed.tolist()[14:])
y = Spain_deaths
x = np.arange(y.size)
popt, pcov = curve_fit(exponential_func, x, y)
Spain_deaths_prediction = exponential_func(np.arange(days_to_predict), *popt)
Germany_deaths = np.trim_zeros(np.array(Germany_deaths.tolist()))
Germany_death_rate=Germany_deaths/np.array(Germany_confirmed.tolist()[14:])
y = Germany_deaths
x = np.arange(y.size)
popt, pcov = curve_fit(exponential_func, x, y)
Germany_deaths_prediction = exponential_func(np.arange(days_to_predict), *popt)
Brazil_deaths = np.trim_zeros(np.array(Brazil_deaths.tolist()))
Brazil_death_rate=Brazil_deaths/np.array(Brazil_confirmed.tolist()[14:])
Brazil_deaths[4]=18
y = Brazil_deaths
x = np.arange(y.size)
popt, pcov = curve_fit(exponential_func, x, y)
Brazil_deaths_prediction = exponential_func(np.arange(days_to_predict), *popt)
%%html
<iframe width="560" height="315" src="https://www.youtube.com/embed/kbYlAgqZhfQ" frameborder="0" allow="accelerometer; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
import matplotlib.pyplot as plt
import plotly.offline
import plotly.tools as tls
import plotly.plotly as py
# Configurations
plotly.offline.init_notebook_mode(connected=True)
import warnings; warnings.simplefilter('ignore')
fig=plt.figure(figsize=(12,8))
plt.yscale('log')
plt.xlabel('Number of Days since 1st Death in each Country')
plt.ylabel('Number of Cases')
plt.title('Confirmed Cases and Curve Fit since 1st Death in each Country')
plt.grid()
## Italy
# Italy Case
plt.plot(Italy_confirmed.tolist()[14:], 'r', label='Italy Cases')
plt.plot(Italy_cases_prediction[14:], 'rx',label = 'Italy Cases Prediction', markersize=3)
# Italy Deaths
plt.plot(Italy_deaths, 'r', label='Italy Deaths',linestyle='dashed')
plt.plot(Italy_deaths_prediction, 'r.',label = 'Italy Deaths Prediction', markersize=3)
## Spain
# Spain Cases
plt.plot(Spain_confirmed.tolist()[14:], 'k', label='Spain Cases')
plt.plot(Spain_cases_prediction[14:], 'kx',label = 'Spain Cases Prediction', markersize=3)
#Spain Deaths
plt.plot(Spain_deaths, 'k', label='Spain Deaths',linestyle='dashed')
plt.plot(Spain_deaths_prediction, 'k.',label = 'Spain Deaths Prediction', markersize=3)
## Germany
# Germany Cases
plt.plot(Germany_confirmed.tolist()[14:], 'b', label='Germany Cases')
plt.plot(Germany_cases_prediction[14:], 'bx',label = 'Germany Cases Prediction', markersize=3)
# Germany Deaths
plt.plot(Germany_deaths, 'b', label='Germany Deaths',linestyle='dashed')
plt.plot(Germany_deaths_prediction, 'b.',label = 'Germany Deaths Prediction', markersize=3)
## Brazil
# Brazil Cases
plt.plot(Brazil_confirmed.tolist()[14:], 'g', label='Brazil Cases')
plt.plot(Brazil_cases_prediction[14:], 'gx',label = 'Brazil Cases Prediction', markersize=3)
# Brazil Deaths
plt.plot(Brazil_deaths,label = 'Brazil Deaths', linestyle='dashed', color='g')
plt.plot(Brazil_deaths_prediction, 'gx',label = 'Brazil Deaths Prediction', markersize=3)
# Transfomr to Plotlly
plotly_fig = tls.mpl_to_plotly(fig)
shapes = list()
for i in (11, 18):
shapes.append({'type': 'line',
'xref': 'x',
'yref': 'y',
'x0': i,
'y0': 0,
'x1': i,
'y1': 10000000,
'line' : dict(color="LightSeaGreen",
width=1,
dash="dash",)},
)
plotly_fig.layout.update(showlegend=True, shapes=shapes)
plotly.offline.iplot(plotly_fig)
fig=plt.figure(figsize=(12,8))
plt.yscale('log')
plt.xlabel('Number of Days since 1st Death in each Country')
plt.ylabel('Number of Deaths / Cases')
plt.title('Deaths / Cases since 1st Death in each Country')
plt.grid()
## Italy
plt.plot(Italy_death_rate, 'r', label='Italy Deaths/Cases')
## Spain
plt.plot(Spain_death_rate, 'k', label='Spain Deaths/Cases')
## Germany
plt.plot(Germany_death_rate, 'b', label='Germany Deaths/Cases')
## Brazil
plt.plot(Brazil_death_rate, 'g', label='Brazil Deaths/Cases')
# Transfomr to Plotlly
plotly_fig = tls.mpl_to_plotly(fig)
shapes = list()
for i in (11, 18):
shapes.append({'type': 'line',
'xref': 'x',
'yref': 'y',
'x0': i,
'y0': 0,
'x1': i,
'y1': 10000000,
'line' : dict(color="LightSeaGreen",
width=1,
dash="dash",)},
)
plotly_fig.layout.update(showlegend=True, shapes=shapes)
plotly.offline.iplot(plotly_fig)