<jupyter_start><jupyter_text>Descriptive Analysis, Visualization and Predictive Analysis on Covid-19 Dataset Author: <NAME> Dataset Source: Kaggle This dataset is updated last on 21st March 2020 * Coronavirus disease (COVID-19) which is now a global pandemic is an infectious disease caused by a newly discovered coronavirus.* Most people infected with the COVID-19 virus experienced mild to moderate respiratory illness.Older people, and those with underlying medical problems like cardiovascular disease, diabetes, chronic respiratory disease, and cancer are more likely to develop serious illness.* The best way to prevent and slow down transmission is be well informed about the COVID-19 virus, the disease it causes and how it spreads. Protect yourself and others from infection by washing your hands or using an alcohol based rub frequently and not touching your face. * The COVID-19 virus spreads primarily through droplets of saliva or discharge from the nose when an infected person coughs or sneezes, so it’s important that you also practice respiratory etiquette (for example, by coughing into a flexed elbow).* At this time, there are no specific vaccines or treatments for COVID-19. However, there are many ongoing clinical trials evaluating potential treatments. WHO will continue to provide updated information as soon as clinical findings become available.  Descriptive Analysis<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# reading data
data=pd.read_csv("covid_19_data.csv")

# convert data into dataframe
df=pd.DataFrame(data)

# exploring first 10 entries
df.head(10)
# to change the name of attributes
df=df.rename(columns={"Province/State":"Province","Country/Region":"Country","ObservationDate":"Date"})

df.head()
# to get statistics of the dataset
print(df.describe())

print("\n")

# to get information of attributes and entries
df.info()
# Exploring last 20 entries 
df.tail(20)
# to check total missing values column-wise
df.isnull().sum()
# to arrange the dataframe country-wise and to get sum of all confirmed, deaths and recovered cases
rf = df.groupby(['Country','Date'])[['Country','Confirmed', 'Deaths', 'Recovered']].sum().reset_index()
rf.head()
# Arranging data in descending order as per confirmed cases
sorted_By_Confirmed=rf.sort_values('Confirmed',ascending=False)
sorted_By_Confirmed.head(10)
# Droping duplicates entries of countries
sorted_By_Confirmed=sorted_By_Confirmed.drop_duplicates('Country')
sorted_By_Confirmed.tail(40)
world_Confirmed_Total=sorted_By_Confirmed['Confirmed'].sum()

world_Deaths_Total=sorted_By_Confirmed['Deaths'].sum()

world_Recovered_Total=sorted_By_Confirmed['Recovered'].sum()

active = world_Confirmed_Total - world_Deaths_Total - world_Recovered_Total 

# Creating dictionary
veri={'Total Confirmed cases  in the world':world_Confirmed_Total,
      'Total Deaths cases in the world':world_Deaths_Total,
      'Total Recovered cases in the world':world_Recovered_Total,
      'Total Active Cases':active}

print("The data is till 21st March 2020:")
veri=pd.DataFrame.from_dict(veri, orient='index' ,columns=['Total'])
veri.style.background_gradient(cmap='Greens')
Recovered_rate=(sorted_By_Confirmed['Recovered']*100)/sorted_By_Confirmed['Confirmed']
Deaths_rate=(sorted_By_Confirmed['Deaths']*100)/sorted_By_Confirmed['Confirmed']
cases_rate=(sorted_By_Confirmed.Confirmed*100)/world_Confirmed_Total

sorted_By_Confirmed['Active']=sorted_By_Confirmed['Confirmed']-sorted_By_Confirmed['Deaths']-sorted_By_Confirmed['Recovered']
sorted_By_Confirmed['Recovered Cases Rate %']=pd.DataFrame(Recovered_rate)
sorted_By_Confirmed['Deaths Cases Rate %']=pd.DataFrame(Deaths_rate)
sorted_By_Confirmed['Total Cases Rate %']=pd.DataFrame(cases_rate)


print("Sorted By Confirmed Cases:")
sorted_By_Confirmed.style.background_gradient(cmap='Blues')<jupyter_output>Sorted By Confirmed Cases:<jupyter_text>Data Visualization  1. Comparison of Confirmed, Recovered and Death cases of different countries<jupyter_code>sns.set(style="whitegrid")

# Initialize the matplotlib figure
f, ax = plt.subplots(figsize=(15,15 ))

sns.barplot(x="Confirmed", y="Country", data=sorted_By_Confirmed.head(20),
            label="Confirmed", color="b")

sns.set_color_codes("muted")
sns.barplot(x="Recovered", y="Country", data=sorted_By_Confirmed.head(20),
            label="Recovered", color="g")

sns.set_color_codes("muted")
sns.barplot(x="Deaths", y="Country", data=sorted_By_Confirmed.head(20),
            label="Deaths", color="r")

# Add a legend and informative axis label
ax.legend(ncol=2, loc="lower right", frameon=True)<jupyter_output><empty_output><jupyter_text>2. Top 10 countries in the world facing Covid-19<jupyter_code>sorted_By_Confirmed1=sorted_By_Confirmed.head(10)

x=sorted_By_Confirmed1.Country
y=sorted_By_Confirmed1.Confirmed

plt.rcParams['figure.figsize'] = (12, 10)
sns.barplot(x,y,order=x ,palette="rocket").set_title('Top 10 countries facing Covid-19')<jupyter_output><empty_output><jupyter_text>3. Number of confirmed, recovered and death cases per day<jupyter_code>cases_per_Day = df.groupby(["Date"])['Confirmed','Deaths', 'Recovered'].sum().reset_index()

sorted_By_Confirmed1 = cases_per_Day.sort_values('Date',ascending=False)

x=cases_per_Day.index

y=cases_per_Day.Confirmed
y1=cases_per_Day.Deaths
y2=cases_per_Day.Recovered

sns.set(style="whitegrid")

# Initialize the matplotlib figure
f, ax = plt.subplots(figsize=(12,10 ))

plt.scatter(x,y,color='blue' ,label='Confirmed Cases')
plt.scatter(x,y1,color='red' ,label="Deaths Cases")
plt.scatter(x,y2,color='green',label="Recovered Cases")
plt.title("Increasing infections cases in the world per day")
ax.legend(ncol=2, loc='upper left', frameon=True)
plt.show()<jupyter_output><empty_output><jupyter_text>Predictive Analysis  1. Feature Selection and Training of Polynomial Regression Model<jupyter_code># feature selection 
x_data=pd.DataFrame(cases_per_Day.index)   # prediction variable
y_data=pd.DataFrame(cases_per_Day.Confirmed)  # target variable

# Now, the features is to be divided into train and test data
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(x_data,y_data,test_size=0.1,random_state=0)<jupyter_output><empty_output><jupyter_text>Let us first analyse the data with Linear Regression Model<jupyter_code>from sklearn.linear_model import LinearRegression
lin_reg1=LinearRegression()

# Training of linear regression model
lin_reg1.fit(x_train,y_train)

# Prediction from linear regression model
lin_reg1.predict(x_test)
# Visualising the Linear Regression results 
plt.scatter(x_test, y_test, color = 'blue') 
  
plt.plot(x_test, lin_reg1.predict(x_test), color = 'red') 
plt.title('Linear Regression') 
plt.xlabel('No. of days') 
plt.ylabel('No. of Confirmed Cases') 
  
plt.show()<jupyter_output><empty_output><jupyter_text>Since linear regression is not suitable as it is leaving outlier which has a significant value and can not be neglected therefore I will go for Polynomial Regression which is preferred for this kind of dataset.<jupyter_code># importing Polynomial features from sci-kit learn library
from sklearn.preprocessing import PolynomialFeatures

# Adding polynomial features in training data
poly_reg=PolynomialFeatures(degree=5)
x_poly=poly_reg.fit_transform(x_train)

lin_reg2=LinearRegression()
lin_reg2.fit(x_poly,y_train)<jupyter_output><empty_output><jupyter_text>2. Making Prediction With Polynomial Regression Model  Since the model has seccessfuly trained. Now, this is the time to predict the number of confirmed cases per day in the world.<jupyter_code># Making prediction using test data
y_pred=lin_reg2.predict(poly_reg.fit_transform(x_test))

# initialization of result by storing predicted value as DataFrame
result=pd.DataFrame(y_pred)
result['Real Value']=y_test.iloc[:,:].values

# Converting predicted values into dataframe
result['Predicted Value']=pd.DataFrame(y_pred)

# Side-by-side comparison of Real Target Values and Predicted Target Values
result=result[['Real Value','Predicted Value']]
result<jupyter_output><empty_output><jupyter_text>Visualization of predicted value by Polynomial Regression Model with test data<jupyter_code>plt.scatter(x_test,y_test,color='blue') 
plt.plot(x_test,y_pred,color='red')
plt.title("Polynomial Regression Model") 
plt.xlabel('No. of days') 
plt.ylabel('No. of Confirmed Cases') 
plt.show()<jupyter_output><empty_output><jupyter_text>Let us check the accuracy of the model<jupyter_code># to check the accuracy import r2_score from metrics
from sklearn.metrics import r2_score

print('Polynomial Regession  R2 Score:',r2_score(y_test, y_pred))<jupyter_output>Polynomial Regession  R2 Score: 0.9981212244835752
{'fraction_non_alphanumeric': 0.09974891577265464, 'fraction_numerical': 0.0140378908924903, 'mean_word_length': 7.401725790987536, 'pattern_counts': {'":': 3, '<': 40, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '638496', 'license': 'MIT', 'n_tokens_mistral': 2939, 'n_tokens_neox': 2760, 'n_words': 739, 'path': 'Covid-19.ipynb', 'repo_name': 'brajeshjha98/Analysis-of-Covid-19-Dataset', 'size': '8769'}