"""
# Joseph Stewart, 4820862
"""
# 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 seaborn as sns
import matplotlib.pyplot as plt
from sklearn.model_selection import TimeSeriesSplit
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedKFold

# 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


train_data = pd.read_csv("/kaggle/input/cap-4611-2021-fall-assignment-1/train.csv")
sub_data = pd.read_csv("/kaggle/input/cap-4611-2021-fall-assignment-1/test.csv")
"""
# Exploratory Data Analysis
"""
train_data.head()
"""
# Restrict analysis to data with just HHS region that is United States.
"""
train_data = train_data[train_data['HHS Region'] == 'United States']
train_data
"""
# Remove Total Deaths and ID
"""
train_data = train_data.drop(axis=1, columns=['Total Deaths', 'id'])
train_data
train_data.describe()
"""
# Remove Date As Of, Year, and group, as they are not important. 
"""
"""
# Remove Week Ending Date b/c same as End Date.
# Remove End Date b/c linear to start date.
"""
train_data = train_data.drop(axis=1, columns=['Data As Of', 'Year', 'Group', 'Week-Ending Date', 'End Date', 'Month'])
train_data
"""
# HHS Region no longer important b/c 1 value
"""
train_data = train_data.drop(axis=1, columns=['HHS Region', 'Footnote'])
train_data
train_data.describe()
missing = train_data.isnull().sum()
missing = missing[missing > 0]
missing.sort_values(inplace = True)
print(missing)
"""
# Drop unknown Race and Hispanical Origin Group
"""
train_data = train_data[train_data['Race and Hispanic Origin Group'] != 'Unknown']
train_data
"""
# Convert Race and Hispanic Origin Group into numerical index
"""
train_data['Race and Hispanic Origin Group'].unique()
train_data = train_data.replace(dict.fromkeys(['Hispanic'], 0))
train_data = train_data.replace(dict.fromkeys(['Non-Hispanic American Indian or Alaska Native'], 1))
train_data = train_data.replace(dict.fromkeys(['Non-Hispanic Asian'], 2))
train_data = train_data.replace(dict.fromkeys(['Non-Hispanic Black'], 3))
train_data = train_data.replace(dict.fromkeys(['Non-Hispanic More than one race'], 4))
train_data = train_data.replace(dict.fromkeys(['Non-Hispanic Native Hawaiian or Other Pacific Islander'], 5))
train_data = train_data.replace(dict.fromkeys(['Non-Hispanic White'], 6))
train_data
"""
# Convert Age Group to numerical index
"""
train_data['Age Group'].unique()
train_data = train_data.replace(dict.fromkeys(['0-4 years'], 0))
train_data = train_data.replace(dict.fromkeys(['5-17 years'], 5))
train_data = train_data.replace(dict.fromkeys(['18-29 years'], 18))
train_data = train_data.replace(dict.fromkeys(['30-39 years'], 30))
train_data = train_data.replace(dict.fromkeys(['40-49 years'], 40))
train_data = train_data.replace(dict.fromkeys(['50-64 years'], 50))
train_data = train_data.replace(dict.fromkeys(['65-74 years'], 65))
train_data = train_data.replace(dict.fromkeys(['75-84 years'], 75))
train_data = train_data.replace(dict.fromkeys(['85 years and over'], 85))

train_data
"""
# Convert 'Start Date' into numerical index
"""
#train_data['Start Date'].unique()
#train_data.shape
#train_data.reindex
#train_data.loc[63, 'Start Date']
#index_marker = 0
#curr_date = '12/29/2019'
#for x in range(4663):
#    if train_data.at[x, 'Start Date'] == curr_date:
#        train_data.at[x, 'Start Date'] = index_marker
#    else:
#        curr_date = train_data.at[x, 'Start Date']
#        index_marker = index_marker+1
#        train_data.at[x, 'Start Date'] = index_marker
train_data.shape
"""
# Drop MMWR missing
"""
train_data = train_data.dropna(subset=['MMWR Week'])
"""
# Verify the drop
"""
missing = train_data.isnull().sum()
missing = missing[missing > 0]
missing.sort_values(inplace = True)
print(missing)
"""
# Examine data relationships & outliers
"""
train_data.hist(figsize=(12,5), layout=(2,3))
plt.show()
sns.boxplot(train_data['Age Group'])
sns.boxplot(train_data['MMWR Week'])
sns.boxplot(train_data['Race and Hispanic Origin Group'])
sns.boxplot(train_data['COVID-19 Deaths'])
print(np.where(train_data['COVID-19 Deaths']<5))
"""
# Scatterplots
"""
fig, ax = plt.subplots(figsize = (18, 10))
ax.scatter(train_data['COVID-19 Deaths'], train_data['Age Group'])

ax.set_xlabel('COVID-19 Deaths in the US')
ax.set_ylabel('Age Group (# signifies youngest person in age group)')
plt.show()
"""
# Deaths in Age<17 range are most likely outliers, but will leave them for now.
"""
fig, ax = plt.subplots(figsize = (18, 10))
ax.scatter(train_data['COVID-19 Deaths'], train_data['Race and Hispanic Origin Group'])

ax.set_xlabel('COVID-19 Deaths in the US')
ax.set_ylabel('Race and Hispanic Origin Group')
plt.show()

# For reference, from earlier
#train_data = train_data.replace(dict.fromkeys(['Hispanic'], 0))
#train_data = train_data.replace(dict.fromkeys(['Non-Hispanic American Indian or Alaska Native'], 1))
#train_data = train_data.replace(dict.fromkeys(['Non-Hispanic Asian'], 2))
#train_data = train_data.replace(dict.fromkeys(['Non-Hispanic Black'], 3))
#train_data = train_data.replace(dict.fromkeys(['Non-Hispanic More than one race'], 4))
#train_data = train_data.replace(dict.fromkeys(['Non-Hispanic Native Hawaiian or Other Pacific Islander'], 5))
#train_data = train_data.replace(dict.fromkeys(['Non-Hispanic White'], 6))
"""
# Train_test_split data into train & test data
"""
features = ['Start Date', 'MMWR Week', 'Race and Hispanic Origin Group', 'Age Group']
target = ['COVID-19 Deaths']

X = pd.get_dummies(train_data[features])
y = pd.get_dummies(train_data[target])
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size = .60)
X_train.shape
X_train.head()
X_test.shape
"""
# Run models on training data
"""
import statsmodels.api as sm
from sklearn.metrics import accuracy_score
"""
# Ordinary Least Squares
"""
ols_model = sm.OLS(y, X).fit()
#ols.fit(X, y)
summary = ols_model.summary()
summary
predOLS = ols_model.predict(X_test)
print(predOLS)
predOLS.shape
accOLS = accuracy_score(y_test, predOLS.round())
accOLS
"""
# Ridge Regression Model
"""
from sklearn.linear_model import Ridge
ridge_reg = Ridge()
ridge_reg.fit(X_train, y_train)
ridge_pred = ridge_reg.predict(X_test)
ridge_pred.shape
acc_ridge = accuracy_score(y_test, ridge_pred.round())
acc_ridge
"""
# Lasso Regression Model
"""
from sklearn.linear_model import Lasso
lasso_reg = Lasso(normalize=True)
lasso_reg.fit(X_train,y_train)
lasso_pred = lasso_reg.predict(X_test)
lasso_pred.shape
acc_lasso = accuracy_score(y_test, lasso_pred.round())
acc_lasso
"""
# Elastic Net Regression
"""
from sklearn.linear_model import ElasticNet
elastic_reg = ElasticNet(alpha=1.0, l1_ratio=0.4)
elastic_reg.fit(X_train,y_train)
elastic_pred = elastic_reg.predict(X_test)
acc_elastic = accuracy_score(y_test, elastic_pred.round())
acc_elastic
#cv = RepeatedKFold(n_splits=10, n_repeats=3, random_state=1)
#scores = cross_val_score(elastic_reg, X_train, y_train, scoring='neg_mean_absolute_error', cv=cv, n_jobs=-1)
"""
# Compare models
"""
accOLS
acc_ridge
acc_lasso
acc_elastic
"""
# Run elastic model on submission data
"""
print(elastic_pred)
sub_data.shape
elastic_pred.shape
elastic_pred.resize((1152,))
ridge_pred.resize((1152,))
output = pd.DataFrame({'id':sub_data.id, 'COVID-19 Deaths': elastic_pred})
output.to_csv('submission.csv', index=False)