# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# <a href="https://www.kaggle.com/tanavbajaj/covid-info-and-analysis?scriptVersionId=82559835" target="_blank"><img align="left" alt="Kaggle" title="Open in Kaggle" src="https://kaggle.com/static/images/open-in-kaggle.svg"></a>

# + [markdown] _cell_guid="b1076dfc-b9ad-4769-8c92-a6c4dae69d19" _uuid="8f2839f25d086af736a60e9eeb907d3b93b6e0e5" papermill={"duration": 0.029064, "end_time": "2021-12-17T03:38:22.745948", "exception": false, "start_time": "2021-12-17T03:38:22.716884", "status": "completed"} tags=[]
# # **College Python Project**
# ## **CIA - COVID INFO AND ANALYSIS**

# + [markdown] papermill={"duration": 0.027488, "end_time": "2021-12-17T03:38:22.80354", "exception": false, "start_time": "2021-12-17T03:38:22.776052", "status": "completed"} tags=[]
# ## Overview 
# *(Source: WHO)*
#
# Coronavirus disease (COVID-19) is an infectious disease caused by the SARS-CoV-2 virus.
#
# Most people infected with the virus will experience mild to moderate respiratory illness and recover without requiring special treatment. However, some will become seriously ill and require medical attention. Older people and those with underlying medical conditions like cardiovascular disease, diabetes, chronic respiratory disease, or cancer are more likely to develop serious illness. Anyone can get sick with COVID-19 and become seriously ill or die at any age. 
#
# The best way to prevent and slow down transmission is to be well informed about the disease and how the virus spreads. Protect yourself and others from infection by staying at least 1 metre apart from others, wearing a properly fitted mask, and washing your hands or using an alcohol-based rub frequently. Get vaccinated when it’s your turn and follow local guidance.
#
# The virus can spread from an infected person’s mouth or nose in small liquid particles when they cough, sneeze, speak, sing or breathe. These particles range from larger respiratory droplets to smaller aerosols. It is important to practice respiratory etiquette, for example by coughing into a flexed elbow, and to stay home and self-isolate until you recover if you feel unwell.
# ## We have attempted to show how this deadly virus attacked the world

# + [markdown] papermill={"duration": 0.029466, "end_time": "2021-12-17T03:38:22.861375", "exception": false, "start_time": "2021-12-17T03:38:22.831909", "status": "completed"} tags=[]
# # Initialization

# + papermill={"duration": 0.083977, "end_time": "2021-12-17T03:38:22.972854", "exception": false, "start_time": "2021-12-17T03:38:22.888877", "status": "completed"} tags=[]
# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session

# + [markdown] papermill={"duration": 0.028998, "end_time": "2021-12-17T03:38:23.03017", "exception": false, "start_time": "2021-12-17T03:38:23.001172", "status": "completed"} tags=[]
# ## 

# + [markdown] papermill={"duration": 0.028792, "end_time": "2021-12-17T03:38:23.087932", "exception": false, "start_time": "2021-12-17T03:38:23.05914", "status": "completed"} tags=[]
# # Importing Libraries

# + papermill={"duration": 2.254127, "end_time": "2021-12-17T03:38:25.370159", "exception": false, "start_time": "2021-12-17T03:38:23.116032", "status": "completed"} tags=[]
#Data Processing
import pandas as pd
import numpy as np

#Data Visulaisation
import plotly.express as px

#Machine Learning Libraries
import sklearn 
from sklearn import linear_model
from sklearn.utils import shuffle

#Miscellaneous
import os
import warnings
warnings.filterwarnings('ignore')


# + [markdown] papermill={"duration": 0.026947, "end_time": "2021-12-17T03:38:25.430122", "exception": false, "start_time": "2021-12-17T03:38:25.403175", "status": "completed"} tags=[]
# # Reading DataSet

# + papermill={"duration": 0.562786, "end_time": "2021-12-17T03:38:26.020326", "exception": false, "start_time": "2021-12-17T03:38:25.45754", "status": "completed"} tags=[]
def read_data(path,file):
     return pd.read_csv(path+"/"+file)    
path=r'../input/corona-virus-report'
world =read_data(path,'worldometer_data.csv')
india=read_data('../input/covid19-in-india','covid_19_india.csv')
state=pd.read_csv("../input/covid19-in-india/StatewiseTestingDetails.csv")
daily = pd.read_csv('../input/covid19-corona-virus-india-dataset/nation_level_daily.csv')
vac_data = pd.read_csv('../input/covid-world-vaccination-progress/country_vaccinations.csv')
pop_data = pd.read_csv('../input/corona-virus-report/worldometer_data.csv')
vac_manu = pd.read_csv('../input/covid-world-vaccination-progress/country_vaccinations_by_manufacturer.csv')
state_vac=pd.read_csv('../input/covid19-in-india/covid_vaccine_statewise.csv')
daily_records=read_data(path,'day_wise.csv')

# + [markdown] papermill={"duration": 0.026623, "end_time": "2021-12-17T03:38:26.074076", "exception": false, "start_time": "2021-12-17T03:38:26.047453", "status": "completed"} tags=[]
# # Data Cleaning 
# ## Making the data more usable for working with it 

# + papermill={"duration": 0.27686, "end_time": "2021-12-17T03:38:26.377925", "exception": false, "start_time": "2021-12-17T03:38:26.101065", "status": "completed"} tags=[]
# For world Vaccination Dataset
usa_vac = vac_data[vac_data['country'] == 'United States']
uk_vac = vac_data[vac_data['country'] == 'United Kingdom']
ger_vac = vac_data[vac_data['country'] == 'Germany']
ita_vac = vac_data[vac_data['country'] == 'Italy']
fra_vac = vac_data[vac_data['country'] == 'France']
chi_vac = vac_data[vac_data['country'] == 'China']
rus_vac = vac_data[vac_data['country'] == 'Russia']
isr_vac = vac_data[vac_data['country'] == 'Israel']
uae_vac = vac_data[vac_data['country'] == 'United Arab Emirates']
can_vac = vac_data[vac_data['country'] == 'Canada']
jpn_vac = vac_data[vac_data['country'] == 'Japan']
ind_vac = vac_data[vac_data['country'] == 'India']
ino_vac = vac_data[vac_data['country'] == 'Indonesia']
mal_vac = vac_data[vac_data['country'] == 'Malaysia']
ban_vac = vac_data[vac_data['country'] == 'Bangladesh']
nig_vac = vac_data[vac_data['country'] == 'Nigeria']
phi_vac = vac_data[vac_data['country'] == 'Phillipines']
vie_vac = vac_data[vac_data['country'] == 'Vietnam']
egy_vac = vac_data[vac_data['country'] == 'Egypt']
pak_vac = vac_data[vac_data['country'] == 'Pakistan']
usa_vac.drop(usa_vac[usa_vac['daily_vaccinations'].isnull()].index, inplace=True)
uk_vac.drop(uk_vac[uk_vac['daily_vaccinations'].isnull()].index, inplace=True)
ger_vac.drop(ger_vac[ger_vac['daily_vaccinations'].isnull()].index, inplace=True)
ita_vac.drop(ita_vac[ita_vac['daily_vaccinations'].isnull()].index, inplace=True)
fra_vac.drop(fra_vac[fra_vac['daily_vaccinations'].isnull()].index, inplace=True)
chi_vac.drop(chi_vac[chi_vac['daily_vaccinations'].isnull()].index, inplace=True)
rus_vac.drop(rus_vac[rus_vac['daily_vaccinations'].isnull()].index, inplace=True)
isr_vac.drop(isr_vac[isr_vac['daily_vaccinations'].isnull()].index, inplace=True)
uae_vac.drop(uae_vac[uae_vac['daily_vaccinations'].isnull()].index, inplace=True)
can_vac.drop(can_vac[can_vac['daily_vaccinations'].isnull()].index, inplace=True)
jpn_vac.drop(jpn_vac[jpn_vac['daily_vaccinations'].isnull()].index, inplace=True)
ind_vac.drop(ind_vac[ind_vac['daily_vaccinations'].isnull()].index, inplace=True)
ino_vac.drop(ino_vac[ino_vac['daily_vaccinations'].isnull()].index, inplace=True)
mal_vac.drop(mal_vac[mal_vac['daily_vaccinations'].isnull()].index, inplace=True)
ban_vac.drop(ban_vac[ban_vac['daily_vaccinations'].isnull()].index, inplace=True)
nig_vac.drop(nig_vac[nig_vac['daily_vaccinations'].isnull()].index, inplace=True)
phi_vac.drop(phi_vac[phi_vac['daily_vaccinations'].isnull()].index, inplace=True)
vie_vac.drop(vie_vac[vie_vac['daily_vaccinations'].isnull()].index, inplace=True)
egy_vac.drop(egy_vac[egy_vac['daily_vaccinations'].isnull()].index, inplace=True)
pak_vac.drop(pak_vac[pak_vac['daily_vaccinations'].isnull()].index, inplace=True)


# + papermill={"duration": 0.070395, "end_time": "2021-12-17T03:38:26.475066", "exception": false, "start_time": "2021-12-17T03:38:26.404671", "status": "completed"} tags=[]
#For Indian Vaccination Dataset
df2=state_vac
df2 = df2.rename(columns= {'Updated On':'Date','Total Doses Administered':'TotalDoses','Male(Individuals Vaccinated)':'Male','Female(Individuals Vaccinated)':'Female', 'Total Individuals Vaccinated':'TotalVaccinated',' Covaxin (Doses Administered)':'Covaxin','CoviShield (Doses Administered)':'CoviShield','Sputnik V (Doses Administered)':'Sputnik'})
df2.Date = pd.to_datetime(df2.Date, format="%d/%m/%Y")
df3=india
df1=state
df2 = df2[df2['State'] !='India']
df2 = df2.rename(columns= {'Updated On':'Date','Total Doses Administered':'TotalDoses','Male(Individuals Vaccinated)':'Male','Female(Individuals Vaccinated)':'Female', 'Total Individuals Vaccinated':'TotalVaccinated',' Covaxin (Doses Administered)':'Covaxin','CoviShield (Doses Administered)':'CoviShield','Sputnik V (Doses Administered)':'Sputnik'})
df2.Date = pd.to_datetime(df2.Date, format="%d/%m/%Y")
df2_2=df2[df2['Date']=="2021-08-9"]
df2_2.dropna()
df2_1 = df3[df3['Date']=='2021-08-11']

# + [markdown] papermill={"duration": 0.026933, "end_time": "2021-12-17T03:38:26.528956", "exception": false, "start_time": "2021-12-17T03:38:26.502023", "status": "completed"} tags=[]
# # Data Visualization

# + [markdown] papermill={"duration": 0.026977, "end_time": "2021-12-17T03:38:26.583009", "exception": false, "start_time": "2021-12-17T03:38:26.556032", "status": "completed"} tags=[]
# ## World Dataset

# + papermill={"duration": 1.304319, "end_time": "2021-12-17T03:38:27.914252", "exception": false, "start_time": "2021-12-17T03:38:26.609933", "status": "completed"} tags=[]
features=['TotalCases','TotalDeaths','TotalRecovered','ActiveCases']
for i in features:
    fig=px.treemap(world.iloc[0:25],
               values=i,
               path=['Country/Region'],
               template='plotly_dark',
               title="Tree Map depicting Impact of Covid-19 w.r.t {}".format(i))
    fig.show()

# + papermill={"duration": 0.155446, "end_time": "2021-12-17T03:38:28.100325", "exception": false, "start_time": "2021-12-17T03:38:27.944879", "status": "completed"} tags=[]
px.line(daily_records,
       x='Date',
       y=['Confirmed', 'Deaths', 'Recovered','Active'],
       template='plotly_dark',
       title='Daily trends of Covid-19 cases',
       labels={'Date':'Month','value':'Statistics'})

# + papermill={"duration": 0.134577, "end_time": "2021-12-17T03:38:28.266527", "exception": false, "start_time": "2021-12-17T03:38:28.13195", "status": "completed"} tags=[]
fig2=px.bar(world.iloc[0:20][::-1],
      y='Country/Region',
      x=['TotalCases','TotalRecovered', 'ActiveCases','TotalDeaths','Serious,Critical'],
      template='plotly_dark',
      title='Severly Hit Countries')
fig2.update_xaxes(tickangle=270)
fig2.show()

# + papermill={"duration": 0.099936, "end_time": "2021-12-17T03:38:28.398669", "exception": false, "start_time": "2021-12-17T03:38:28.298733", "status": "completed"} tags=[]
px.pie(world.iloc[0:20],
       names='Country/Region',
       values='TotalCases',
      template='plotly_dark',
      title='Distribution of Total Cases ')

# + [markdown] papermill={"duration": 0.032835, "end_time": "2021-12-17T03:38:28.464256", "exception": false, "start_time": "2021-12-17T03:38:28.431421", "status": "completed"} tags=[]
# ## Indian Dataset

# + papermill={"duration": 0.116912, "end_time": "2021-12-17T03:38:28.613428", "exception": false, "start_time": "2021-12-17T03:38:28.496516", "status": "completed"} tags=[]
from plotly.subplots import make_subplots
import plotly.graph_objects as go

grouped_data=read_data(path,'full_grouped.csv')
grouped_data.head()

def country_visualisations(df,country):
    data_group=df[df['Country/Region']==country]
    data=data_group.loc[:,['Date','Confirmed','Deaths','Recovered','Active']]
    figure1=make_subplots(rows=1,cols=4,subplot_titles=('Confirmed','Active','Recovered','Deaths'))
    
    figure1.add_trace(go.Scatter(name='Confirmed',x=data['Date'],y=data['Confirmed']),row=1,col=1)
    
    figure1.add_trace(go.Scatter(name='Active',x=data['Date'],y=data['Active']),row=1,col=2)
        
    figure1.add_trace(go.Scatter(name='Recovered',x=data['Date'],y=data['Recovered']),row=1,col=3)
    
    figure1.add_trace(go.Scatter(name='Deaths',x=data['Date'],y=data['Deaths']),row=1,col=4)
    
    figure1.update_layout(height=500,
                         width=2000,
                         title_text='Recorded Cases in {}'.format(country),
                         template='plotly_dark')
    figure1.show()


# + papermill={"duration": 0.229602, "end_time": "2021-12-17T03:38:28.875937", "exception": false, "start_time": "2021-12-17T03:38:28.646335", "status": "completed"} tags=[]
country_visualisations(grouped_data,'India')
country_visualisations(grouped_data,'Peru')

# + [markdown] papermill={"duration": 0.03367, "end_time": "2021-12-17T03:38:28.944693", "exception": false, "start_time": "2021-12-17T03:38:28.911023", "status": "completed"} tags=[]
# ## World Vaccination Situation 

# + papermill={"duration": 0.084483, "end_time": "2021-12-17T03:38:29.063475", "exception": false, "start_time": "2021-12-17T03:38:28.978992", "status": "completed"} tags=[]

fig = go.Figure()
fig.add_trace(go.Scatter(x=usa_vac['date'], y=usa_vac['daily_vaccinations'], mode='lines+markers', name='USA'))
fig.add_trace(go.Scatter(x=uk_vac['date'],y=uk_vac['daily_vaccinations'], mode='lines+markers', name='UK'))
fig.add_trace(go.Scatter(x=ger_vac['date'],y=ger_vac['daily_vaccinations'], mode='lines+markers', name='Germany'))
fig.add_trace(go.Scatter(x=ind_vac['date'],y=ind_vac['daily_vaccinations'], mode='lines+markers', name='India'))
fig.update_layout(title='Comparison of Daily Vaccinations' , template='plotly_dark' )
fig.show()

# + [markdown] papermill={"duration": 0.03606, "end_time": "2021-12-17T03:38:29.136072", "exception": false, "start_time": "2021-12-17T03:38:29.100012", "status": "completed"} tags=[]
# ## Indian Vs Pakistan (And China) 
# ### Not Cricket but Vaccinations  

# + papermill={"duration": 0.080436, "end_time": "2021-12-17T03:38:29.252625", "exception": false, "start_time": "2021-12-17T03:38:29.172189", "status": "completed"} tags=[]
fig = go.Figure()
fig.add_trace(go.Scatter(x=ind_vac['date'],y=ind_vac['daily_vaccinations'], mode='lines+markers', name='India'))
fig.add_trace(go.Scatter(x=chi_vac['date'],y=chi_vac['daily_vaccinations'], mode='lines+markers', name='China'))
fig.add_trace(go.Scatter(x=pak_vac['date'],y=pak_vac['daily_vaccinations'], mode='lines+markers', name='Pakistan'))
fig.update_layout(title='Comparison of Daily Vaccinations' , template='plotly_dark')
fig.show()

# + [markdown] papermill={"duration": 0.037258, "end_time": "2021-12-17T03:38:29.327669", "exception": false, "start_time": "2021-12-17T03:38:29.290411", "status": "completed"} tags=[]
# ## Pakistan may have won the cricket match but they did not win at vaccinations per day 
#

# + [markdown] papermill={"duration": 0.036979, "end_time": "2021-12-17T03:38:29.402072", "exception": false, "start_time": "2021-12-17T03:38:29.365093", "status": "completed"} tags=[]
# ## Indian Situation

# + [markdown] papermill={"duration": 0.037121, "end_time": "2021-12-17T03:38:29.476521", "exception": false, "start_time": "2021-12-17T03:38:29.4394", "status": "completed"} tags=[]
# ### Testing Situation In India

# + papermill={"duration": 0.813541, "end_time": "2021-12-17T03:38:30.327582", "exception": false, "start_time": "2021-12-17T03:38:29.514041", "status": "completed"} tags=[]
state.Date = pd.to_datetime(state.Date, format="%Y/%m/%d")
fig = px.line(state, x='Date', y='TotalSamples', color='State', title='Total number of samples collected for Covid-19 testing(Statewise)' , template="plotly_dark")

fig.show()

# + [markdown] papermill={"duration": 0.050614, "end_time": "2021-12-17T03:38:30.486576", "exception": false, "start_time": "2021-12-17T03:38:30.435962", "status": "completed"} tags=[]
# ### Result of Testing
#

# + papermill={"duration": 0.138953, "end_time": "2021-12-17T03:38:30.674983", "exception": false, "start_time": "2021-12-17T03:38:30.53603", "status": "completed"} tags=[]
fig = px.bar(df2_1, x='State/UnionTerritory', y=['Confirmed','Cured','Deaths'], template="plotly_dark")
fig.update_layout(xaxis={'categoryorder':'total descending'})
fig.show()

# + [markdown] papermill={"duration": 0.050281, "end_time": "2021-12-17T03:38:30.775429", "exception": false, "start_time": "2021-12-17T03:38:30.725148", "status": "completed"} tags=[]
# ### Indian Vaccination Status

# + papermill={"duration": 0.12962, "end_time": "2021-12-17T03:38:30.956384", "exception": false, "start_time": "2021-12-17T03:38:30.826764", "status": "completed"} tags=[]
fig = px.bar(df2_2, x='State', y='TotalDoses',title='Total Doses (Jan-Aug)',template="plotly_dark")
fig.update_traces(textposition='outside')
fig.update_layout(xaxis={'categoryorder':'total descending'})
fig.update_xaxes(tickfont=dict(size=14))
fig.update_yaxes(tickfont=dict(size=14))
fig.show()

# + papermill={"duration": 0.147765, "end_time": "2021-12-17T03:38:31.155473", "exception": false, "start_time": "2021-12-17T03:38:31.007708", "status": "completed"} tags=[]
fig = px.bar(df2_2, x="State", y=["Covaxin", "CoviShield",'TotalDoses'], template="plotly_dark")

fig.update_layout(barmode='stack',legend_orientation="h",legend=dict(x= 0.3, y=1.0),
                  xaxis={'categoryorder':'total descending'},
                 title_text='<b>Covid-19 Total Vaccinations in India according to type of vaccine<b>',
                  title_x=0.5,
                  width= 1100,
                  height= 500
                 )
fig.update_xaxes(tickfont=dict(size=14))
fig.update_yaxes(tickfont=dict(size=14))
fig.show()

# + [markdown] papermill={"duration": 0.052167, "end_time": "2021-12-17T03:38:31.260736", "exception": false, "start_time": "2021-12-17T03:38:31.208569", "status": "completed"} tags=[]
# # Machine Learning Part

# + [markdown] papermill={"duration": 0.052202, "end_time": "2021-12-17T03:38:31.365559", "exception": false, "start_time": "2021-12-17T03:38:31.313357", "status": "completed"} tags=[]
# ## Attempt At Linear Regression 

# + papermill={"duration": 0.084136, "end_time": "2021-12-17T03:38:31.501964", "exception": false, "start_time": "2021-12-17T03:38:31.417828", "status": "completed"} tags=[]
from sklearn import model_selection
from sklearn.linear_model import LinearRegression
df2_1=df2_1[df2_1['State/UnionTerritory']!='Maharashtra']
states_clubbed=df2_1[["Confirmed","Cured","Deaths"]]
predict="Deaths"
X=np.array(states_clubbed.drop(predict,1))
y=np.array(states_clubbed[predict])
X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.25)
linear = LinearRegression()
linear.fit(X_train,y_train)
Y_pred = linear.predict(X_test)
print(linear.score(X_test, y_test))
print(linear.score(X_train,y_train))


# + papermill={"duration": 0.11594, "end_time": "2021-12-17T03:38:31.673859", "exception": false, "start_time": "2021-12-17T03:38:31.557919", "status": "completed"} tags=[]
df2_1
fig = px.scatter(df2_1, x='Cured', y='Deaths', template="plotly_dark")
fig.show()

# + papermill={"duration": 0.170887, "end_time": "2021-12-17T03:38:31.897854", "exception": false, "start_time": "2021-12-17T03:38:31.726967", "status": "completed"} tags=[]
df2_1

# + papermill={"duration": 0.0538, "end_time": "2021-12-17T03:38:32.006676", "exception": false, "start_time": "2021-12-17T03:38:31.952876", "status": "completed"} tags=[]


StarcoderdataJupyterScriptsDedupFiltered