image_output_1.png
from ipywidgets import interact, interact_manual, fixed

from pathlib import Path

from sklearn.linear_model import LinearRegression

from sklearn.metrics import r2_score, mean_squared_error

import cufflinks as cf

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

pd.set_option('display.max_rows', 500)
pd.set_option('use_inf_as_na', True)
cf.set_config_file(offline=True, theme='solar')
path = Path('../input/novel-corona-virus-2019-dataset/')

master_df = pd.read_csv(path / 'covid_19_data.csv')

recovered_df = pd.read_csv(path / 'time_series_covid_19_recovered.csv').drop(columns=['Lat', 'Long']).groupby('Country/Region').sum()
deaths_df = pd.read_csv(path / 'time_series_covid_19_deaths.csv').drop(columns=['Lat', 'Long']).groupby('Country/Region').sum()
confirmed_df = pd.read_csv(path / 'time_series_covid_19_confirmed.csv').drop(columns=['Lat', 'Long']).groupby('Country/Region').sum()

sorted_country_list = confirmed_df.sort_values(by=confirmed_df.columns[-1], ascending=False).index.to_list()

@interact(country=sorted_country_list, threshold=(0, 1000, 10))
def log_lin_visualise(country, threshold=100):
    y = confirmed_df.filter(items=[country], axis=0).values.squeeze(0)
    y = np.log(y[y > threshold])
    x = np.arange(1, y.shape[0] + 1)
    lr = LinearRegression(normalize=True)
    lr.fit(X=x.reshape(-1, 1), y=y)
    y_fitted = lr.predict(X=x.reshape(-1, 1))
    print(f'r2_score = {round(r2_score(y, y_fitted), 2)}')
    print(f'mean_squared_error = {round(mean_squared_error(y, y_fitted), 2)}')
    plt.figure(figsize=(10, 5))
    plt.plot(x, y, label='Actual')
    plt.plot(x, y_fitted, label='Linear Regression')
    plt.xlabel(f'Days Since {threshold}th Case')
    plt.ylabel('Natural Logarithm of Confirmed Cases')
    plt.legend()
    plt.title(country)
    plt.show()
    plt.close()