Kaggle competition: COVID19 Global Forecasting (Week 4)
<load_from_csv><EOS> 
train = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/train.csv')
 test = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/test.csv')
 train['Province_State'] = train['Province_State'].replace(np.nan, 'nan', regex=True)
 train['Province_State'] = np.where(train['Province_State'] == 'nan' ,train['Country_Region'],train['Province_State'])
 test['Province_State'] = test['Province_State'].replace(np.nan, 'nan', regex=True)
 test['Province_State'] = np.where(test['Province_State'] == 'nan' ,test['Country_Region'],test['Province_State'])
 train['Date'] = pd.to_datetime(train['Date'])
 train['Days'] = train['Date']-train['Date'].min()
train['Days'] = train['Days'].dt.days.astype(int)
 train[['ConfirmedCases', 'Fatalities']] = train[['ConfirmedCases', 'Fatalities']].astype(int)
 test['Date'] = pd.to_datetime(test['Date'])
 test['Days'] = test['Date']-train['Date'].min()
test['Days'] = test['Days'].dt.days.astype(int)
 province_train = np.zeros(int(len(train)/84)).astype(object)
 province_test = np.zeros(int(len(test)/43)).astype(object)
 X_train = np.zeros(len(province_train)).astype(object)
 y_train = np.zeros(len(province_train)).astype(object)
 X_test = np.zeros(len(province_test)).astype(object)
 province_index = 0
index = 0
while province_index < len(train):
province = train['Province_State'][province_index]
country = train['Country_Region'][province_index]
province_train[index] = train[(train['Province_State'] == province)&(train['Country_Region']== country)]
X_train[index] = pd.DataFrame(province_train[index] ).iloc[:,[0,1,2,6]]
y_train[index] = pd.DataFrame(province_train[index] ).iloc[:,4:6]
index +=1
province_index += 84
province_index = 0
index = 0
while province_index < len(test):
province = test['Province_State'][province_index]
country = test['Country_Region'][province_index]
province_test[index] = test[(test['Province_State'] == province)&(test['Country_Region']== country)]
X_test[index] = pd.DataFrame(province_test[index] ).iloc[:,[0,1,2,4]]
index +=1
province_index += 43
label_encoder1 = LabelEncoder()
label_encoder2 = LabelEncoder()
for index in range(0,len(province_train)) :
X_train[index]['Province_State'] = label_encoder1.fit_transform(X_train[index]['Province_State'])
 X_train[index]['Country_Region'] = label_encoder2.fit_transform(X_train[index]['Country_Region'])
 X_test[index]['Province_State'] = label_encoder1.fit_transform(X_test[index]['Province_State'])
 X_test[index]['Country_Region'] = label_encoder2.fit_transform(X_test[index]['Country_Region'])
 model = PolynomialFeatures(degree = 4)
 X_poly = model.fit_transform(X_train[index].iloc[:,1:4])
 model.fit(X_poly, y_train[index].iloc[:,0])
 lin_reg = LinearRegression()
lin_reg.fit(X_poly, y_train[index].iloc[:,0])
 y_pred=lin_reg.predict(model.fit_transform(X_test[index].iloc[:,1:4]))
y_pred = np.around(y_pred,decimals=0 ).astype(int)
 X_test[index]['ConfirmedCases'] = np.where(y_pred < 0 ,0,y_pred)
 model = PolynomialFeatures(degree = 4)
 X_poly = model.fit_transform(X_train[index].iloc[:,1:4])
 model.fit(X_poly, y_train[index].iloc[:,0])
 lin_reg = LinearRegression()
lin_reg.fit(X_poly, y_train[index].iloc[:,1])
 y_pred=lin_reg.predict(model.fit_transform(X_test[index].iloc[:,1:4]))
y_pred = np.around(y_pred,decimals=0 ).astype(int)
 X_test[index]['Fatalities'] = np.where(y_pred <0 ,0,y_pred)
 X_test[index]['Province_State'] = province_test[index].iloc[:,1]
X_test[index]['Country_Region'] = province_test[index].iloc[:,2]
test_final = pd.DataFrame([])
 for i in range(0, len(X_test)) :
test_final = pd.concat([test_final, X_test[i]], ignore_index=True)
 test['ConfirmedCases'] = test_final['ConfirmedCases']
test['Fatalities'] = test_final['Fatalities']
test[(test['Days'] >=71)&(test['Days']<=83)]['ConfirmedCases'] = train[(train['Days'] >=71)&(train['Days']<=83)]['ConfirmedCases']
test[(test['Days'] >=71)&(test['Days']<=83)]['Fatalities'] = train[(train['Days'] >=71)&(train['Days']<=83)]['Fatalities']
replace_train_index = train.index[(train['Days'] >=71)&(train['Days']<=83)].values
replace_test_index = test.index[(test['Days'] >=71)&(test['Days']<=83)].values
index = 0
for i in replace_test_index:
test.set_value(i,'ConfirmedCases', train.iloc[replace_train_index[index],4])
 test.set_value(i,'Fatalities', train.iloc[replace_train_index[index],5])
 index += 1
submission = test.iloc[:,[0,5,6]]
submission.to_csv('submission.csv', index=False)