Kaggle competition: COVID19 Global Forecasting (Week 2)
<SOS> metric: MCRMSLE Kaggle data source: covid19-global-forecasting-(week-2)<create_dataframe>
%matplotlib inline
modle_13= pd.DataFrame(suball_2.mode(axis=1)[0] )<feature_engineering>
train = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-2/train.csv")
 test = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-2/test.csv")
 train.head()
modle_13['index']=modle_13.index<merge>
EMPTY_VAL = "EMPTY_VAL"
def fillState(state, country):
if state == EMPTY_VAL: return country
return state
train_['Province_State'].fillna(EMPTY_VAL, inplace=True)
 train_['Province_State'] = train_.loc[:, ['Province_State', 'Country_Region']].apply(lambda x : fillState(x['Province_State'], x['Country_Region']), axis=1)
 test['Province_State'].fillna(EMPTY_VAL, inplace=True)
 test['Province_State'] = test.loc[:, ['Province_State', 'Country_Region']].apply(lambda x : fillState(x['Province_State'], x['Country_Region']), axis=1)
 test.head()
final = suball_2.merge(modle_13, on ='index' )<define_variables>
train_['row_number'] = train_.groupby(['Country_Region', 'Province_State'] ).cumcount()
x = train_[train_["Country_Region"] == 'China'][train_["Province_State"] == 'Hubei']['row_number']
y = train_[train_["Country_Region"] == 'China'][train_["Province_State"] == 'Hubei']['ConfirmedCases']
y_ = train_[train_["Country_Region"] == 'China'][train_["Province_State"] == 'Hubei']['Fatalities']
def f(x, L, b, k, x_0):
return L /(1.+ np.exp(-k *(x - x_0)))+ b
def logistic(xs, L, k, x_0):
result = []
for x in xs:
xp = k*(x-x_0)
 if xp >= 0:
result.append(L /(1.+ np.exp(-xp)))
 else:
result.append(L * np.exp(xp)/(1.+ np.exp(xp)))
 return result
p0 = [max(y), 0.0,max(x)]
p0_ = [max(y_), 0.0,max(x)]
x_ = np.arange(0, 100, 1 ).tolist()
try:
popt, pcov = opt.curve_fit(logistic, x, y,p0)
 yfit = logistic(x_, *popt)
 popt_, pcov_ = opt.curve_fit(logistic, x, y_,p0_)
 yfit_ = logistic(x_, *popt_)
 except:
popt, pcov = opt.curve_fit(f, x, y, method="lm", maxfev=5000)
 yfit = f(x_, *popt)
 popt_, pcov_ = opt.curve_fit(f, x, y_, method="lm", maxfev=5000)
 yfit_ = f(x_, *popt_)
 fig, ax = plt.subplots(1, 1, figsize=(10, 8))
ax.plot(x, y, 'o', label ='Actual Cases')
 ax.plot(x_, yfit, '-', label ='Fitted Cases')
 ax.plot(x, y_, 'o', label ='Actual Fatalities')
 ax.plot(x_, yfit_, '-', label ='Fitted fatalities')
 ax.title.set_text('China - Hubei province')
 plt.legend(loc="center right")
 plt.show()
final_res=final[['Id',0]]<rename_columns>
unique = pd.DataFrame(train_.groupby(['Country_Region', 'Province_State'],as_index=False ).count())
 unique.head()
final_res.columns=['Id','Target']<save_to_csv>
def date_day_diff(d1, d2):
delta = dt.datetime.strptime(d1, "%Y-%m-%d")- dt.datetime.strptime(d2, "%Y-%m-%d")
 return delta.days
log_regions = []
for index, region in unique.iterrows() :
st = region['Province_State']
co = region['Country_Region']
rdata = train_[(train_['Province_State']==st)&(train_['Country_Region']==co)]
t = rdata['Date'].values
t = [float(date_day_diff(d, t[0])) for d in t]
y = rdata['ConfirmedCases'].values
y_ = rdata['Fatalities'].values
p0 = [max(y), 0.0, max(t)]
p0_ = [max(y_), 0.0, max(t)]
try:
popt, pcov = opt.curve_fit(logistic, t, y, p0, maxfev=10000)
 try:
popt_, pcov_ = opt.curve_fit(logistic, t, y_, p0_, maxfev=10000)
 except:
popt_, pcov_ = opt.curve_fit(f, t, y_,method="trf", maxfev=10000)
 log_regions.append(( co,st,popt,popt_))
except:
popt, pcov = opt.curve_fit(f, t, y,method="trf", maxfev=10000)
 popt_, pcov_ = opt.curve_fit(f, t, y_,method="trf", maxfev=10000)
 log_regions.append(( co,st,popt,popt_))
print("All done!" )
final_res.to_csv('4_model_vote.csv',index=False )<import_modules>
log_regions = pd.DataFrame(log_regions)
 log_regions.head()
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns<set_options>
log_regions.columns = ['Country_Region','Province_State','ConfirmedCases','Fatalities']
log_regions.head(1 )
pd.options.display.max_columns = None<load_from_csv>
log_regions['ConfirmedCases'].str[1].quantile([.1,.25,.5,.75,.95,.99] )
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 train.head()<data_type_conversions>
for index, rt in log_regions.iterrows() :
st = rt['Province_State']
co = rt['Country_Region']
popt = list(['ConfirmedCases'])
 popt_ = list(rt['Fatalities'])
 print(co,st,popt,popt_ )
mapping = {"yes": 1, "no": 0}
for df in [train, test]:
df['dependency'] = df['dependency'].replace(mapping ).astype(np.float64)
 df['edjefa'] = df['edjefa'].replace(mapping ).astype(np.float64)
 df['edjefe'] = df['edjefe'].replace(mapping ).astype(np.float64)
 train[['dependency', 'edjefa', 'edjefe']].describe()<concatenate>
for index, rt in log_regions.iterrows() :
st = rt['Province_State']
co = rt['Country_Region']
popt = list(rt['ConfirmedCases'])
 popt_ = list(rt['Fatalities'])
 if popt_ == [0.0,0.0,69.0]:
popt_ = np.multiply(fp,popt)
 print(co,st,popt,popt_ )
test['Target'] = np.nan
data = train.append(test, ignore_index = True )<count_unique_values>
submission = []
for index, rt in log_regions.iterrows() :
st = rt['Province_State']
co = rt['Country_Region']
popt = list(rt['ConfirmedCases'])
 popt_ = list(rt['Fatalities'])
 if popt_ == [0.0,0.0,69.0]:
popt_ = np.multiply(fp,popt)
 print(co,st,popt,popt_)
 rtest = test[(test['Province_State']==st)&(test['Country_Region']==co)]
for index, rt in rtest.iterrows() :
try:
tdate = rt['Date']
ca = logistic([date_day_diff(tdate, min(train_[(train_['Province_State']==st)&(train_['Country_Region']==co)]['Date'].values)) ], *popt)
 try:
fa = logistic([date_day_diff(tdate, min(train_[(train_['Province_State']==st)&(train_['Country_Region']==co)]['Date'].values)) ], *popt_)
 except:
fa = f([date_day_diff(tdate, min(train_[(train_['Province_State']==st)&(train_['Country_Region']==co)]['Date'].values)) ], *popt_)
 submission.append(( rt['ForecastId'], int(ca[0]), int(fa[0])))
 except:
tdate = rt['Date']
ca = f([date_day_diff(tdate, min(train_[(train_['Province_State']==st)&(train_['Country_Region']==co)]['Date'].values)) ], *popt)
 fa = f([date_day_diff(tdate, min(train_[(train_['Province_State']==st)&(train_['Country_Region']==co)]['Date'].values)) ], *popt_)
 submission.append(( rt['ForecastId'], int(ca[0]), int(fa[0])))
 print("All done!" )
<filter><EOS> 
submission = pd.DataFrame(submission)
 submission.columns = ['ForecastId','ConfirmedCases','Fatalities']
submission.to_csv('./submission.csv', index = False)
 print("submission ready!" )