Kaggle competition: COVID19 Global Forecasting (Week 3)
param<categorify>
warnings.filterwarnings("ignore" )
y1_pred = test(x,param[0], param[1], param[2])
 base_x = range(61,100,1)
 y1_pred_test = test(base_x, param[0], param[1], param[2])
<feature_engineering>
df_train=pd.read_csv(".. /input/covid19-global-forecasting-week-3/train.csv")
 df_test=pd.read_csv(".. /input/covid19-global-forecasting-week-3/test.csv")
 df_sub=pd.read_csv(".. /input/covid19-global-forecasting-week-3/submission.csv" )
( x2[-1] - x2[0])/2 + x2[0]<compute_test_metric>

 print(df_train.shape)
 print(df_test.shape)
 print(df_sub.shape )
y2_pred = test(x2,param2[0], param2[1], param2[2] )<prepare_x_and_y>
print(f"Unique Countries: {len(df_train.Country_Region.unique())}")
 train_dates=list(df_train.Date.unique())
 print(f"Period : {len(df_train.Date.unique())} days")
 print(f"From : {df_train.Date.min() } To : {df_train.Date.max() }" )
base_x = range(61,100,1)
 print(len(base_x))
base_y1 = test(base_x,param[0], param[1], param[2])
 base_y2 = test(base_x,param2[0], param2[1], param2[2] )<define_variables>
print(f"Unique Regions: {df_train.shape[0]/75}")
 df_train.Country_Region.value_counts()
day_index_pred = 0
diff1_list = []
diff2_list = []
for day in base_x:
if day in x:
day_index = np.where(x == day)
 diff1 = y1[day_index] - base_y1[day_index_pred]
diff1_list.append(diff1)
 if day in x2:
day_index = np.where(x2 == day)
 diff2 = y2[day_index] - base_y2[day_index_pred]
diff2_list.append(diff2)
 day_index_pred = day_index_pred + 1
diff1_mean = np.max(diff1_list)
 diff2_mean = np.max(diff2_list)
 if np.isnan(diff1_mean):
pass
else:
base_y1_mod = list(np.array(base_y1)+ diff1_mean)
 if np.isnan(diff2_mean):
pass
else:
base_y2_mod = list(np.array(base_y2)+ diff2_mean)
 base_y1_pred = [int(n)for n in base_y1_mod]
base_y2_pred = [int(m)for m in base_y2_mod]<groupby>
print(f"Number of rows without Country_Region : {df_train.Country_Region.isna().sum() }")
 df_train["UniqueRegion"]=df_train.Country_Region
df_train.UniqueRegion[df_train.Province_State.isna() ==False]=df_train.Province_State+" , "+df_train.Country_Region
df_train[df_train.Province_State.isna() ==False]
test_groups = df_test.groupby(['Country_Region', 'Province_State'] )<feature_engineering>
df_train.drop(labels=["Id","Province_State","Country_Region"], axis=1, inplace=True)
 df_train
index = 0
for key_,_ in zip(min_date_sorted.index, min_date_sorted):
record = df_train[(df_train['Country_Region'] == key_[0])&(df_train['Province_State'] == key_[1])]
record['days'] = [(datetime.strptime(date, "%Y-%m-%d" ).date() - base_date_object ).days + 1 for date in record['Date']]
x = record['days']
y1 = record['ConfirmedCases']
y2 = record['Fatalities']
y1_prime = np.diff(y1)
 stage0 = False
stage1 = False
stage2 = False
stage3 = False
count1 = 0
count2 = 0
for start in range(len(y1_prime)-3):
if sum(y1_prime[start:start+3])<=12:
count1 = count1 + 1
count2 = 0
else:
count2 = count2 + 1
count1 = 0
if not stage0 and count2 == 0 and count1 > 2:
stage0 = True
count1 = 0
if not stage1 and count1 == 0 and count2 > 5:
stage0 = True
stage1 = True
count2 = 0
if stage1 and count2 == 0 and count1 > 3:
stage2 = True
count1 = 0
if stage2 and count1 == 0 and count2 > 2:
stage3 = True
count2 = 0
if stage3:
print(index, key_)
 print(y1_prime)
 plt.plot(x, y1, label = "Confirmed Cases")
 plt.xlabel('Date')
 plt.ylabel('Label')
 plt.title(str(key_[0])+ " " + str(key_[1])+ ' - Confirmed Cases')
 plt.show()
index = index + 1<define_variables>
only_train_dates=set(train_dates)-set(test_dates)
 print("Only train dates : ",len(only_train_dates))
intersection_dates=set(test_dates)&set(train_dates)
 print("Intersection dates : ",len(intersection_dates))
only_test_dates=set(test_dates)-set(train_dates)
 print("Only Test dates : ",len(only_test_dates))
total_confirmed = 0
total_fatalities = 0
rate = []
max_y1 = []
max_y2 = []
details = []
for index, start_date in zip(min_date_sorted.index, min_date_sorted):
print(index, start_date)
 record = df_train[(df_train['Country_Region'] == index[0])&(df_train['Province_State'] == index[1])]
if len(record[record['ConfirmedCases'] > 0])!= 0:
record = record[record['ConfirmedCases'] > 0]
record2 = record
if len(record[record['Fatalities'] > 0])!= 0:
record2 = record[record['Fatalities'] > 0]
y1 = record['ConfirmedCases'].values
y2 = record2['Fatalities'].values
b = -1
bad_index = 0
mod_count = 0
y1_copy = list(y1)
 for a in y1:
if a < b:
y1[bad_index] = b
mod_count = mod_count + 1
else:
b = a
bad_index = bad_index + 1
b = -1
bad_index = 0
mod_count = 0
y2_copy = list(y2)
 for a in y2:
if a < b:
y2[bad_index] = b
mod_count = mod_count + 1
else:
b = a
bad_index = bad_index + 1
y1_prime = np.diff(y1)
 y1_prime2 = np.diff(y1_prime)
 y_max_ = y1[-1]*2 + 1500
if len(y1)>0 and len(y1_prime)> 0 and len(y1_prime2)> 0:
max_slope_index = len(y1_prime)- 1 - list(y1_prime)[::-1].index(max(y1_prime))
max_slope_range =(max_slope_index+1)/len(y1_prime)
 if max_slope_range < 0.75:
if y1_prime[max_slope_index] > 0 and max_slope_range < 0.5 and(((y1_prime[max_slope_index] - max(y1_prime[-2:])) /y1_prime[max_slope_index])< 0.5):
y_max_ = y1[-1]*2 + 1500
pass
else:
y_max_ = y1[max_slope_index + 1]*2 + 1500
pass
else:
y_max_ = y1[-1]*2 + 1500
ratio = 0
if y2[-1] > 0:
ratio = y1[-1]/y2[-1]
else:
ratio = y1[-1]
max_y1.append(y1[-1])
 max_y2.append(y2[-1])
 rate.append(ratio)
 details.append(" ".join([str(x)for x in [y1[-1], " ------- ", y2[-1], " ---- ", ratio, " --------------- ", record['Date'].values[-1], " ---- ", index, "----", list(min_date_sorted.index ).index(index)]]))
total_confirmed = total_confirmed + y1[-1]
total_fatalities = total_fatalities + y2[-1]
print(total_confirmed/total_fatalities )<define_variables>
df_test_temp=pd.DataFrame()
df_test_temp["Date"]=df_test.Date
df_test_temp["ConfirmedCases"]=0.0
df_test_temp["Fatalities"]=0.0
df_test_temp["UniqueRegion"]=df_test.UniqueRegion
df_test_temp["Delta"]=1.0
for a1, a2, b, c in zip(max_y1, max_y2, rate, details):
print(c )<categorify>
%%time
final_df=pd.DataFrame(columns=["Date","ConfirmedCases","Fatalities","UniqueRegion"])
 for region in df_train.UniqueRegion.unique() :
df_temp=df_train[df_train.UniqueRegion==region].reset_index()
df_temp["Delta"]=1.0
size_train=df_temp.shape[0]
for i in range(1,df_temp.shape[0]):
if(df_temp.ConfirmedCases[i-1]>0):
df_temp.Delta[i]=df_temp.ConfirmedCases[i]/df_temp.ConfirmedCases[i-1]
n=5
delta_avg=df_temp.tail(n ).Delta.mean()
delta_list=df_temp.tail(n ).Delta
death_rate=df_temp.tail(1 ).Fatalities.sum() /df_temp.tail(1 ).ConfirmedCases.sum()
df_test_app=df_test_temp[df_test_temp.UniqueRegion==region]
df_test_app=df_test_app[df_test_app.Date>df_temp.Date.max() ]
X=np.arange(1,n+1 ).reshape(-1,1)
 Y=delta_list
model=LinearRegression()
model.fit(X,Y)
 df_temp=pd.concat([df_temp,df_test_app])
 df_temp=df_temp.reset_index()
for i in range(size_train, df_temp.shape[0]):
n=n+1
df_temp.Delta[i]=max(1,model.predict(np.array([n] ).reshape(-1,1)) [0])
 df_temp.ConfirmedCases[i]=round(df_temp.ConfirmedCases[i-1]*df_temp.Delta[i],0)
 df_temp.Fatalities[i]=round(death_rate*df_temp.ConfirmedCases[i],0)
 size_test=df_temp.shape[0]-df_test_temp[df_test_temp.UniqueRegion==region].shape[0]
df_temp=df_temp.iloc[size_test:,:]
df_temp=df_temp[["Date","ConfirmedCases","Fatalities","UniqueRegion"]]
final_df=pd.concat([final_df,df_temp], ignore_index=True)
 final_df.shape
for a1, a2, b, c in zip(max_y1, max_y2, rate, details):
if(a1 < 100 and a2 < 4 and b < avg):
print(c)
 pass
else:
pass<create_dataframe>
df_sub.Fatalities=final_df.Fatalities
df_sub.ConfirmedCases=final_df.ConfirmedCases
df_sub.to_csv("submission.csv", index=None )