# 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)
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px
import plotly.graph_objects as go
import plotly.offline as py
import plotly.graph_objs as go
import plotly
plotly.offline.init_notebook_mode(connected=True)

# 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
"""
<h1 style="background-color:#DC143C; font-family:'Brush Script MT',cursive;color:white;font-size:200%; text-align:center;border-radius: 50% 20% / 10% 40%">Effect of lockdown amid COVID-19 pandemic on air quality of the megacity Delhi, India</h1>

Authors: Susanta Mahato, Swades Pal, and Krishna Gopal Ghosh

Sci Total Environ. 2020 Aug 15; 730: 139086. - Published online 2020 Apr 29.
DOI: 10.1016/j.scitotenv.2020.139086 - PMCID: PMC7189867 - PMID: 32375105

"Amid the COVID-19 pandemic, a nationwide lockdown is imposed in India initially for three weeks from 24th March to 14th April 2020 and extended up to 3rd May 2020. Due to the forced restrictions, pollution level in cities across the country drastically slowed down just within few days which magnetize discussions regarding lockdown to be the effectual alternative measures to be implemented for controlling air pollution."

"The present article eventually worked on this direction to look upon the air quality scenario amidst the lockdown period scientifically with special reference to the megacity Delhi." 

"The results demonstrated that during lockdown air quality is significantly improved. Among  pollutants, NO2 (−52.68%) and CO (−30.35%) level have also reduced during-lockdown phase."

"About 40% to 50% improvement in air quality is identified just after four days of commencing lockdown. Overall, the study is thought to be a useful supplement to the regulatory bodies since it showed the pollution source control can attenuate the air quality. Temporary such source control in a suitable time interval may heal the environment."

https://www.ncbi.nlm.nih.gov/pmc/articles/PMC7189867/


"""
"""
![](https://static.toiimg.com/img/74868916/Master.jpg)timesofindia.indiatimes.com
"""
df1 = pd.read_csv("/kaggle/input/impact-of-covid19-outbreak-on-global-air-quality/CASE_DELHI.csv")
print(df1.shape)
df1.head().style.set_properties(**{'background-color':'Aquamarine',
                                     'color': 'purple'})
df = pd.read_csv("/kaggle/input/impact-of-covid19-outbreak-on-global-air-quality/NO2vO3_DELHI.csv")
print(df.shape)
df.head().style.set_properties(**{'background-color':'BurlyWood',
                                     'color': 'purple'})
df2 = pd.read_csv("/kaggle/input/impact-of-covid19-outbreak-on-global-air-quality/CASE_Satellite.csv")
print(df2.shape)
df2.head().style.set_properties(**{'background-color':'PaleGreen',
                                     'color': 'purple'})
df.shape
df.isnull().sum()
fig = px.bar(df, 
             x='yr', y='no2', color_discrete_sequence=['#D63230'],
             title='NO2 in Delhi 2015-20', text='n_no2')
fig.show()
fig = px.line(df, x="yr", y="no2", color_discrete_sequence=['#2B3A67'], 
              title="NO2 in Delhi 2015-20")
fig.show()
fig = px.pie(df,
             values="yr",
             names="case",
             template="seaborn")
fig.update_traces(rotation=90, pull=0.05, textinfo="percent+label")
fig.show()
"""
#Handling Missing Values
"""
# categorical features with missing values
categorical_nan = [feature for feature in df.columns if df[feature].isna().sum()>0 and df[feature].dtypes=='O']
print(categorical_nan)
# replacing missing values in categorical features
for feature in categorical_nan:
    df[feature] = df[feature].fillna('None')
df[categorical_nan].isna().sum()
"""
#Label Encoding
"""
#Code by Bizen https://www.kaggle.com/hiro5299834/tps-apr-2021-deebtables/notebook

TARGET = 'n_no2' #Target could Not be float otherwise will result in valueError: Unknown label type: 'continuous'. Even after the encoding.

label_cols = ['case']
numerical_cols = ['yr', 'mo', 'da', 'no2', 'o3mx8', 'starthr', 'n_o3']
#Code by Bizen https://www.kaggle.com/hiro5299834/tps-apr-2021-deebtables/notebook

from sklearn.preprocessing import LabelEncoder

def label_encoder(c):
    le = LabelEncoder()
    return le.fit_transform(c)

label_encoded_df = df[label_cols].apply(label_encoder)
numerical_df = df[numerical_cols]
target_df = df[TARGET]

df = pd.concat([numerical_df, label_encoded_df, target_df], axis=1)
df.head()
"""
#Code by Napetrov https://www.kaggle.com/napetrov/tps04-svm-with-scikit-learn-intelex/notebook
"""
"""
#Installing scikit-learn-intelex

Package also available in conda - please refer to details https://github.com/intel/scikit-learn-intelex
"""
!pip install scikit-learn-intelex --progress-bar off >> /tmp/pip_sklearnex.log
from sklearnex import patch_sklearn
patch_sklearn()
from sklearn.svm import LinearSVC
from sklearn.svm import SVC
from sklearn.linear_model import SGDClassifier

from sklearn.impute import SimpleImputer
from sklearn.metrics import accuracy_score

from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OrdinalEncoder
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import StandardScaler

from sklearn.model_selection import KFold
from sklearn.model_selection import train_test_split

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split

import optuna
RANDOM_SEED = 2021
PROBAS = True
FOLDS = 5
N_ESTIMATORS = 1000
#Code by Napetrov https://www.kaggle.com/napetrov/tps04-svm-with-scikit-learn-intelex/notebook

df_scaled = df.drop([TARGET], axis = 1).copy()

scaler = StandardScaler()
scaler.fit(df.drop([TARGET], axis = 1))
df_scaled = scaler.transform(df_scaled)

df_scaled = pd.DataFrame(df_scaled, columns=df.drop([TARGET], axis = 1).columns)
df_scaled.head(5)
#Code by Napetrov https://www.kaggle.com/napetrov/tps04-svm-with-scikit-learn-intelex/notebook

X = df_scaled
y = df[TARGET]

print (f'X:{X.shape} y: {y.shape} \n')

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.20, random_state = RANDOM_SEED)
print (f'X_train:{X_train.shape} y_train: {y_train.shape}')
print (f'X_test:{X_test.shape} y_test: {y_test.shape}')

test = df_scaled[len(df):]
print (f'test:{test.shape}')
#Code by Napetrov https://www.kaggle.com/napetrov/tps04-svm-with-scikit-learn-intelex/notebook

%time
svc_kernel_rbf = SVC(kernel='rbf', random_state=0, C=1.3040348958661234, gamma=0.11195797734572176 )
svc_kernel_rbf.fit(X_train, y_train)
y_pred = svc_kernel_rbf.predict(X_test)
accuracy_score(y_pred, y_test)
%time
final_pred = svc_kernel_rbf.predict(test)
#Code by Napetrov https://www.kaggle.com/napetrov/tps04-svm-with-scikit-learn-intelex/notebook

def objective(trial):
    from sklearn.svm import SVC
    params = {
        'C': trial.suggest_loguniform('C', 0.1, 0.5),
        'gamma': trial.suggest_categorical('gamma', ["auto"]),
        'kernel': trial.suggest_categorical("kernel", ["rbf"])
    }

    svc = SVC(**params)
    svc.fit(X_train, y_train)
    return svc.score(X_test, y_test)
#Code by Napetrov https://www.kaggle.com/napetrov/tps04-svm-with-scikit-learn-intelex/notebook

study = optuna.create_study(sampler=optuna.samplers.TPESampler(seed=123),
                            direction="maximize",
                            pruner=optuna.pruners.MedianPruner())
study.optimize(objective, n_trials=5, show_progress_bar=True)
print(f"Best Value: {study.best_trial.value}")
print(f"Best Params: {study.best_params}")
#Code by Napetrov https://www.kaggle.com/napetrov/tps04-svm-with-scikit-learn-intelex/notebook

%time
n_folds = 5
kf = KFold(n_splits=n_folds, shuffle=True, random_state=0)
y_pred = np.zeros(test.shape[0])

for fold, (train_index, valid_index) in enumerate(kf.split(X, y)):
    print("Running Fold {}".format(fold + 1))
    X_train, X_valid = pd.DataFrame(X.iloc[train_index]), pd.DataFrame(X.iloc[valid_index])
    y_train, y_valid = y.iloc[train_index], y.iloc[valid_index]
    svc_kernel_rbf = SVC(**study.best_params)
    svc_kernel_rbf.fit(X_train, y_train)
    print("  Accuracy: {}".format(accuracy_score(y_valid, svc_kernel_rbf.predict(X_valid))))
    y_pred += svc_kernel_rbf.predict(test)

y_pred /= n_folds

print("I'm so screwed!")
print("Not Done yet!")
"""
Maybe next time I can handle with scikit-learn-intelex. I was almost there.
"""