<jupyter_start><jupyter_text>Predicting Daily COVID-19 cases in Ireland<jupyter_code>import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt

from sklearn.model_selection import TimeSeriesSplit, cross_val_score
from sklearn.neural_network import MLPRegressor
from sklearn.linear_model import LinearRegression
from sklearn.svm import SVR
from sklearn.neighbors import KNeighborsRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import r2_score

import warnings
warnings.filterwarnings('ignore')
df = pd.read_excel("covid19.xlsx")
df = df.loc[(df["location"]=="Ireland")]
df.head()
df = df.drop("Unnamed: 0", axis = 1)
df = df.fillna(0)
df.head()
df.tail()
df["date"] = pd.to_datetime(df["date"])
df.sort_values(by='date', ascending=True, inplace=True)
df = df.set_index("date")
df.tail()
df_new_cases =  df[['new_cases_smoothed']]
df_new_cases.loc[:,'yesterday'] = df_new_cases.loc[:,'new_cases_smoothed'].shift()
df_new_cases.loc[:,'yesterday_diff'] = df_new_cases.loc[:,'yesterday'].diff()
df_new_cases.loc[:,'yesterday2'] = df_new_cases.loc[:,'yesterday'].shift()
df_new_cases.loc[:,'yesterday_diff2'] = df_new_cases.loc[:,'yesterday2'].diff()
df_new_cases.loc[:,'yesterday3'] = df_new_cases.loc[:,'yesterday2'].shift()
df_new_cases.loc[:,'yesterday_diff3'] = df_new_cases.loc[:,'yesterday3'].diff()
df_new_cases.loc[:,'yesterday4'] = df_new_cases.loc[:,'yesterday3'].shift()
df_new_cases.loc[:,'yesterday_diff4'] = df_new_cases.loc[:,'yesterday4'].diff()
df_new_cases.loc[:,'yesterday5'] = df_new_cases.loc[:,'yesterday4'].shift()
df_new_cases.loc[:,'yesterday_diff5'] = df_new_cases.loc[:,'yesterday5'].diff()
df_new_cases.loc[:,'yesterday6'] = df_new_cases.loc[:,'yesterday5'].shift()
df_new_cases.loc[:,'yesterday_diff6'] = df_new_cases.loc[:,'yesterday6'].diff()
df_new_cases.loc[:,'yesterday7'] = df_new_cases.loc[:,'yesterday6'].shift()
df_new_cases.loc[:,'yesterday_diff7'] = df_new_cases.loc[:,'yesterday7'].diff()
df_new_cases = df_new_cases.fillna(0)
df_new_cases
xtrain = df_new_cases[:'2021'].drop(['new_cases_smoothed'], axis = 1)
ytrain = df_new_cases.loc[:'2021','new_cases_smoothed']
xtest = df_new_cases['2022'].drop(['new_cases_smoothed'], axis = 1)
ytest = df_new_cases.loc['2022','new_cases_smoothed']
models = []
models.append(('LR', LinearRegression())) 
models.append(('NN', MLPRegressor(solver = 'lbfgs'))) 
models.append(('KNN', KNeighborsRegressor())) 
models.append(('RF', RandomForestRegressor(n_estimators = 10))) 
models.append(('SVR', SVR(gamma='auto'))) 
results = []
names = []
for name, model in models:
    tscv = TimeSeriesSplit(n_splits=2)
    
    cv_results = cross_val_score(model, xtrain, ytrain, cv = tscv, scoring='r2')
    results.append(cv_results)
    names.append(name)
    print('%s: %f (%f)' % (name, cv_results.mean(), cv_results.std()))
    
plt.boxplot(results, labels=names)
plt.title('Algorithm Comparison')
plt.show()
model = MLPRegressor()

param_search = {
    "hidden_layer_sizes": [(1,),(50,),(100,),(150,),(200,)],
    "activation": ["identity", "logistic", "tanh", "relu"],
    "solver": ["lbfgs", "sgd", "adam"],
    "alpha": [0.00005,0.0005, 0.005],
    "learning_rate": ['constant', 'invscaling', 'adaptive']
}
tsvc = TimeSeriesSplit(n_splits=2)
gsearch = GridSearchCV(estimator=model, cv=tsvc, param_grid=param_search, scoring='r2')
gsearch.fit(xtrain, ytrain)
best_score = gsearch.best_score_
best_model = gsearch.best_estimator_
print(best_model)
ypred = best_model.predict(xtest)
print("Accuracy:", r2_score(ytest, ypred))<jupyter_output>Accuracy: 0.9585556119392256
{'fraction_non_alphanumeric': 0.181292984869326, 'fraction_numerical': 0.02778541953232462, 'mean_word_length': 8.0, 'pattern_counts': {'":': 5, '<': 4, '<?xml version=': 0, '>': 4, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '386620', 'license': 'Apache-2.0', 'n_tokens_mistral': 1481, 'n_tokens_neox': 1408, 'n_words': 203, 'path': 'Code/Simon/Rough Work/NewModel2.ipynb', 'repo_name': 'SimonMcLain/TUS-Engineering-Team-Project', 'size': '3635'}