Kaggle competition: COVID19 Global Forecasting (Week 1)
best_model_key=ConfirmedCases_all_result_Final[['key','best_model']].drop_duplicates()
max_number_current=train.groupby('key' ).max() [['ConfirmedCases','Fatalities']].reset_index()
best_model_key=best_model_key.merge(max_number_current,on='key',how='left')
 best_model_key=best_model_key.sort_values('ConfirmedCases',ascending=False ).reset_index(drop=True )<groupby>
import scipy.optimize as opt
import matplotlib.pyplot as plt
from datetime import datetime, timedelta
from tqdm import tqdm_notebook
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_squared_log_error
import warnings; warnings.filterwarnings('ignore' )
train_ck=train.groupby(['Country/Region','Date'] ).sum().reset_index()
train_ck['key']=train_ck['Country/Region']<create_dataframe>
train = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-1/train.csv')
 train['Date'] = pd.to_datetime(train['Date'])
 last_train_date = np.datetime64('2020-03-11')
 eval_set = train[train['Date'] > last_train_date]
overlap_days = eval_set['Date'].unique() [-1] - last_train_date
overlap_days = int(overlap_days.astype('timedelta64[D]')/ np.timedelta64(1, 'D'))
train
Fatalities_all_result_final=pd.DataFrame()
ConfirmedCases_all_result_Final=pd.DataFrame()
for keys in train_ck['key'].unique() :
chk=train_ck[train_ck['key']==keys]
chk.index=chk.Date
fcastperiod=0
fcastperiod1=35
actual=chk[:chk.shape[0]-fcastperiod]
ffcast=chk[chk.shape[0]-fcastperiod-1:]
ffcast
try:
Fatalities_all_result_1=pred_ets(fcastperiod,fcastperiod1,actual,ffcast,'Fatalities' ).reset_index()
except:
Fatalities_all_result_1=pd.DataFrame(pd.date_range(start=train.Date.min() , periods=60+fcastperiod1+1, freq='D')[1:])
 Fatalities_all_result_1.columns=['index']
Fatalities_all_result_1['best_model']='naive'
Fatalities_all_result_1['best_pred']=0
Fatalities_all_result_1['key']=keys
Fatalities_all_result_final=Fatalities_all_result_final.append(Fatalities_all_result_1,sort=True)
 try:
ConfirmedCases_all_result_1=pred_ets(fcastperiod,fcastperiod1,actual,ffcast,'ConfirmedCases' ).reset_index()
except:
ConfirmedCases_all_result_1=pd.DataFrame(pd.date_range(start=train.Date.min() , periods=60+fcastperiod1+1, freq='D')[1:])
 ConfirmedCases_all_result_1.columns=['index']
ConfirmedCases_all_result_1['best_model']='naive'
ConfirmedCases_all_result_1['best_pred']=1
ConfirmedCases_all_result_1['key']=keys
ConfirmedCases_all_result_Final=ConfirmedCases_all_result_Final.append(ConfirmedCases_all_result_1,sort=True)
 print(' done for %s' % keys )<feature_engineering>
test = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-1/test.csv')
 test['Date'] = pd.to_datetime(test['Date'])
 test
ConfirmedCases_all_result_Final.rename(columns={'index':'Date'},inplace=True)
 Fatalities_all_result_final.rename(columns={'index':'Date'},inplace=True)
 ConfirmedCases_all_result_Final['best_pred']=np.where(ConfirmedCases_all_result_Final['best_pred'] is np.nan , 0,
ConfirmedCases_all_result_Final['best_pred'])
 Fatalities_all_result_final['best_pred']=np.where(Fatalities_all_result_final['best_pred'] is np.nan , 0 ,
Fatalities_all_result_final['best_pred'])
 ConfirmedCases_all_result_Final['best_pred']=np.where(ConfirmedCases_all_result_Final['best_pred'] <0 , 0,
ConfirmedCases_all_result_Final['best_pred'])
 Fatalities_all_result_final['best_pred']=np.where(Fatalities_all_result_final['best_pred'] <0 , 0 ,
Fatalities_all_result_final['best_pred'])
 ConfirmedCases_all_result_Final['best_pred_1']=np.where(ConfirmedCases_all_result_Final['AdditiveDamped'] is np.nan , ConfirmedCases_all_result_Final['best_pred'] ,
ConfirmedCases_all_result_Final['AdditiveDamped'])
 Fatalities_all_result_final['best_pred_1']=np.where(Fatalities_all_result_final['AdditiveDamped'] is np.nan , Fatalities_all_result_final['best_pred'] ,
Fatalities_all_result_final['AdditiveDamped'])
 ConfirmedCases_all_result_Final['best_pred_1']=np.where(ConfirmedCases_all_result_Final['best_pred'] is np.nan , 0,
ConfirmedCases_all_result_Final['best_pred'])
 Fatalities_all_result_final['best_pred_1']=np.where(Fatalities_all_result_final['best_pred'] is np.nan , 0 ,
Fatalities_all_result_final['best_pred'])
 ConfirmedCases_all_result_Final['best_pred_1']=np.where(ConfirmedCases_all_result_Final['best_pred'] <0 , 0,
ConfirmedCases_all_result_Final['best_pred'])
 Fatalities_all_result_final['best_pred_1']=np.where(Fatalities_all_result_final['best_pred'] <0 , 0 ,
Fatalities_all_result_final['best_pred'] )<remove_duplicates>
sub = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-1/submission.csv')
 sub = sub.set_index('ForecastId', drop=True)
 sub
best_model_key=ConfirmedCases_all_result_Final[['key','best_model']].drop_duplicates()
max_number_current=train_ck.groupby('key' ).max() [['ConfirmedCases','Fatalities']].reset_index()
best_model_key=best_model_key.merge(max_number_current,on='key',how='left')
 best_model_key=best_model_key.sort_values('ConfirmedCases',ascending=False ).reset_index(drop=True )<set_options>
def model(parameters, time):
y_pred = parameters[0] *(1 - np.exp(-parameters[1] *(time - parameters[3])))** parameters[2]
return np.nan_to_num(y_pred ).clip(0, np.inf)
 def residual(parameters, time, data):
y_pred = model(parameters, time)
 return mean_squared_error(data, y_pred )
<load_from_csv>
def fitModel(time, data, guess):
params = opt.minimize(residual, guess, args=(time, data), bounds=opt.Bounds(0, np.inf, True), method='Nelder-Mead', tol=1e-7)
 return params.x
df = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-1/train.csv')
 test = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-1/test.csv')
 submission = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-1/submission.csv' )<define_variables>
model_params = trainModels(train )
ids = submission['ForecastId']<define_variables>
model_predictions = forecast(test, model_params, sub )
input_cols = ["Lat","Long","Date"]
output_cols = ["ConfirmedCases","Fatalities"]<feature_engineering>
eval_with_id = pd.merge(eval_set, test, on=['Date', 'Country/Region', 'Province/State'])
 sub = sub.replace([np.inf, -np.inf], np.nan)
 sub = sub.fillna(0)
 merged_eval = pd.merge(eval_with_id, sub, left_on='ForecastId', right_index=True)
 merged_eval
for i in range(df.shape[0]):
df["Date"][i] = df["Date"][i][:4] + df["Date"][i][5:7] + df["Date"][i][8:]
df["Date"][i] = int(df["Date"][i] )<feature_engineering>
score_confirmed = np.sqrt(mean_squared_log_error(merged_eval['ConfirmedCases_x'].values, merged_eval['ConfirmedCases_y']))
score_fatality = np.sqrt(mean_squared_log_error(merged_eval['Fatalities_x'].values, merged_eval['Fatalities_y']))
print(f'Confirmed Cases Score: {score_confirmed}
Fatality Score: {score_fatality}
Average Score: {np.mean([score_confirmed, score_fatality])}' )
for i in range(test.shape[0]):
test["Date"][i] = test["Date"][i][:4] + test["Date"][i][5:7] + test["Date"][i][8:]
test["Date"][i] = int(test["Date"][i] )<prepare_x_and_y>
sub.to_csv('submission.csv' )