Kaggle competition: Santander Customer Transaction Prediction
deltas = train_clean[np.logical_and(
 train_clean.LogConfirmed > 2,
~train_clean.Location.str.startswith('China')
)].dropna().sort_values(by='LogConfirmedDelta', ascending=False)
 deltas['start'] = deltas['LogConfirmed'].round(0)
 confirmed_deltas = pd.concat([
deltas.groupby('start')[['LogConfirmedDelta']].mean() ,
deltas.groupby('start')[['LogConfirmedDelta']].std() ,
deltas.groupby('start')[['LogConfirmedDelta']].count()
], axis=1)
 deltas.mean()
confirmed_deltas.columns = ['avg', 'std', 'cnt']
confirmed_deltas
confirmed_deltas.to_csv('confirmed_deltas.csv' )<feature_engineering>
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv' )
DECAY = 0.93
DECAY ** 7, DECAY ** 14, DECAY ** 21, DECAY ** 28
confirmed_deltas = train.groupby(['Location', 'Country_Region'])[[
'Id']].count().reset_index()
GLOBAL_DELTA = 0.11
confirmed_deltas['DELTA'] = GLOBAL_DELTA
confirmed_deltas.loc[confirmed_deltas.Country_Region=='Korea South', 'DELTA'] = 0.011
confirmed_deltas.loc[confirmed_deltas.Country_Region=='US', 'DELTA'] = 0.15
confirmed_deltas.loc[confirmed_deltas.Country_Region=='China', 'DELTA'] = 0.00
confirmed_deltas.loc[confirmed_deltas.Country_Region=='Japan', 'DELTA'] = 0.05
confirmed_deltas.loc[confirmed_deltas.Country_Region=='Singapore', 'DELTA'] = 0.04
confirmed_deltas.loc[confirmed_deltas.Country_Region=='Taiwan*', 'DELTA'] = 0.04
confirmed_deltas.loc[confirmed_deltas.Country_Region=='Switzerland', 'DELTA'] = 0.05
confirmed_deltas.loc[confirmed_deltas.Country_Region=='Norway', 'DELTA'] = 0.05
confirmed_deltas.loc[confirmed_deltas.Country_Region=='Iceland', 'DELTA'] = 0.05
confirmed_deltas.loc[confirmed_deltas.Country_Region=='Austria', 'DELTA'] = 0.06
confirmed_deltas.loc[confirmed_deltas.Country_Region=='Italy', 'DELTA'] = 0.04
confirmed_deltas.loc[confirmed_deltas.Country_Region=='Spain', 'DELTA'] = 0.08
confirmed_deltas.loc[confirmed_deltas.Country_Region=='Portugal', 'DELTA'] = 0.12
confirmed_deltas.loc[confirmed_deltas.Country_Region=='Israel', 'DELTA'] = 0.12
confirmed_deltas.loc[confirmed_deltas.Country_Region=='Iran', 'DELTA'] = 0.08
confirmed_deltas.loc[confirmed_deltas.Country_Region=='Germany', 'DELTA'] = 0.07
confirmed_deltas.loc[confirmed_deltas.Country_Region=='Malaysia', 'DELTA'] = 0.04
confirmed_deltas.loc[confirmed_deltas.Country_Region=='Russia', 'DELTA'] = 0.18
confirmed_deltas.loc[confirmed_deltas.Country_Region=='Ukraine', 'DELTA'] = 0.18
confirmed_deltas.loc[confirmed_deltas.Country_Region=='Brazil', 'DELTA'] = 0.12
confirmed_deltas.loc[confirmed_deltas.Country_Region=='Turkey', 'DELTA'] = 0.16
confirmed_deltas.loc[confirmed_deltas.Country_Region=='Philippines', 'DELTA'] = 0.16
confirmed_deltas.loc[confirmed_deltas.Location=='France-', 'DELTA'] = 0.1
confirmed_deltas.loc[confirmed_deltas.Location=='United Kingdom-', 'DELTA'] = 0.12
confirmed_deltas.loc[confirmed_deltas.Location=='Diamond Princess-', 'DELTA'] = 0.00
confirmed_deltas.loc[confirmed_deltas.Location=='China-Hong Kong', 'DELTA'] = 0.08
confirmed_deltas.loc[confirmed_deltas.Location=='San Marino-', 'DELTA'] = 0.03
confirmed_deltas.shape, confirmed_deltas.DELTA.mean()
confirmed_deltas[confirmed_deltas.DELTA != GLOBAL_DELTA].shape, confirmed_deltas[confirmed_deltas.DELTA != GLOBAL_DELTA].DELTA.mean()
confirmed_deltas[confirmed_deltas.DELTA != GLOBAL_DELTA]
confirmed_deltas.describe()<feature_engineering>
train_features = train_df.drop(['target','ID_code'], axis = 1)
 test_features = test_df.drop(['ID_code'],axis = 1)
 train_target = train_df['target']
confirmed_prediciton = pd.melt(daily_log_confirmed[:25], id_vars='Location')
 confirmed_prediciton['ConfirmedCases'] = to_exp(confirmed_prediciton['value'] )<merge>
df_test = test_features.values
unique_samples = []
unique_count = np.zeros_like(df_test)
 basic_features = [c for c in train_df.columns if c not in ['ID_code', 'target']]
for feature in range(df_test.shape[1]):
_, index_, count_ = np.unique(df_test[:, feature], return_counts=True, return_index=True)
 unique_count[index_[count_ == 1], feature] += 1
real_samples_indexes = np.argwhere(np.sum(unique_count, axis=1)> 0)[:, 0]
synthetic_samples_indexes = np.argwhere(np.sum(unique_count, axis=1)== 0)[:, 0]
train_clean['Geo
latest = train_clean[train_clean.Date == TRAIN_END][[
'Geo
daily_death_deltas = train_clean[train_clean.Date >= '2020-03-17'].pivot(
 'Geo
daily_death_deltas = latest.merge(daily_death_deltas, on='Geo
daily_death_deltas.shape
daily_death_deltas.head()<feature_engineering>
def process_data(train_df, test_df):
idx = [c for c in train_df.columns if c not in ['ID_code', 'target']]
df = pd.concat([train_df,test_df.ix[real_samples_indexes]])
 for feat in idx:
temp = df[feat].value_counts(dropna=True)
 train_df["count_"+feat] = train_df[feat].map(temp)
 test_df["count_"+feat] = test_df[feat].map(temp)
 train_df["sum_"+feat] =(( train_df[feat]-df[feat].mean())*train_df["count_"+feat].map(lambda x:int(x>1)) ).astype(np.float32)
 test_df["sum_"+feat] =(( test_df[feat]-df[feat].mean())*test_df["count_"+feat].map(lambda x:int(x>1)) ).astype(np.float32)
 train_df["copy_"+feat]=train_df[feat] *(train_df["count_"+feat] > 1 ).astype(int)
 test_df["copy_"+feat]=test_df[feat] *(test_df["count_"+feat] > 1 ).astype(int)
 train_df["copy_"+feat] = train_df["copy_"+feat].replace(0,df[feat].median())
 test_df["copy_"+feat] = test_df["copy_"+feat].replace(0,df[feat].median())
 return train_df, test_df
death_deltas = train.groupby(['Location', 'Country_Region'])[[
'Id']].count().reset_index()
GLOBAL_DELTA = 0.11
death_deltas['DELTA'] = GLOBAL_DELTA
death_deltas.loc[death_deltas.Country_Region=='China', 'DELTA'] = 0.000
death_deltas.loc[death_deltas.Country_Region=='Korea South', 'DELTA'] = 0.04
death_deltas.loc[death_deltas.Country_Region=='Japan', 'DELTA'] = 0.04
death_deltas.loc[death_deltas.Country_Region=='Singapore', 'DELTA'] = 0.04
death_deltas.loc[death_deltas.Country_Region=='Taiwan*', 'DELTA'] = 0.04
death_deltas.loc[death_deltas.Country_Region=='US', 'DELTA'] = 0.17
death_deltas.loc[death_deltas.Country_Region=='Switzerland', 'DELTA'] = 0.15
death_deltas.loc[death_deltas.Country_Region=='Norway', 'DELTA'] = 0.15
death_deltas.loc[death_deltas.Country_Region=='Iceland', 'DELTA'] = 0.01
death_deltas.loc[death_deltas.Country_Region=='Austria', 'DELTA'] = 0.14
death_deltas.loc[death_deltas.Country_Region=='Italy', 'DELTA'] = 0.07
death_deltas.loc[death_deltas.Country_Region=='Spain', 'DELTA'] = 0.1
death_deltas.loc[death_deltas.Country_Region=='Portugal', 'DELTA'] = 0.13
death_deltas.loc[death_deltas.Country_Region=='Israel', 'DELTA'] = 0.16
death_deltas.loc[death_deltas.Country_Region=='Iran', 'DELTA'] = 0.06
death_deltas.loc[death_deltas.Country_Region=='Germany', 'DELTA'] = 0.14
death_deltas.loc[death_deltas.Country_Region=='Malaysia', 'DELTA'] = 0.10
death_deltas.loc[death_deltas.Country_Region=='Russia', 'DELTA'] = 0.2
death_deltas.loc[death_deltas.Country_Region=='Ukraine', 'DELTA'] = 0.2
death_deltas.loc[death_deltas.Country_Region=='Brazil', 'DELTA'] = 0.2
death_deltas.loc[death_deltas.Country_Region=='Turkey', 'DELTA'] = 0.22
death_deltas.loc[death_deltas.Country_Region=='Philippines', 'DELTA'] = 0.12
death_deltas.loc[death_deltas.Location=='France-', 'DELTA'] = 0.14
death_deltas.loc[death_deltas.Location=='United Kingdom-', 'DELTA'] = 0.14
death_deltas.loc[death_deltas.Location=='Diamond Princess-', 'DELTA'] = 0.00
death_deltas.loc[death_deltas.Location=='China-Hong Kong', 'DELTA'] = 0.01
death_deltas.loc[death_deltas.Location=='San Marino-', 'DELTA'] = 0.05
death_deltas.shape
death_deltas.DELTA.mean()
death_deltas[death_deltas.DELTA != GLOBAL_DELTA].shape
death_deltas[death_deltas.DELTA != GLOBAL_DELTA].DELTA.mean()
death_deltas[death_deltas.DELTA != GLOBAL_DELTA]
death_deltas.describe()<categorify>
train_features , test_features = process_data(train_features,test_features )
confirmed_prediciton = pd.melt(daily_log_deaths[:25], id_vars='Location')
 confirmed_prediciton['Fatalities'] = to_exp(confirmed_prediciton['value'])
 confirmed_prediciton.shape<feature_engineering>
train_features = train_features[feature_name]
test_features = test_features[feature_name]
gc.collect()
confirmed = []
fatalities = []
for id, d, loc in tqdm(test[['ForecastId', 'Date', 'Location']].values):
c = to_exp(daily_log_confirmed.loc[daily_log_confirmed.Location == loc, d].values[0])
 f = to_exp(daily_log_deaths.loc[daily_log_deaths.Location == loc, d].values[0])
 confirmed.append(c)
 fatalities.append(f )<prepare_output>
sc = StandardScaler()
train_features = sc.fit_transform(train_features)
 test_features = sc.transform(test_features )
my_submission = test.copy()
my_submission['ConfirmedCases'] = confirmed
my_submission['Fatalities'] = fatalities
my_submission.shape
my_submission.head() 
<count_unique_values>
n_splits = 10
splits = list(StratifiedKFold(n_splits=n_splits, shuffle=True, random_state=4590 ).split(train_features, train_target))
my_submission.Location.nunique()<feature_engineering>
class CyclicLR(object):
def __init__(self, optimizer, base_lr=1e-3, max_lr=6e-3,
step_size=2000, mode='triangular', gamma=1.,
scale_fn=None, scale_mode='cycle', last_batch_iteration=-1):
if not isinstance(optimizer, Optimizer):
raise TypeError('{} is not an Optimizer'.format(
 type(optimizer ).__name__))
self.optimizer = optimizer
if isinstance(base_lr, list)or isinstance(base_lr, tuple):
if len(base_lr)!= len(optimizer.param_groups):
raise ValueError("expected {} base_lr, got {}".format(
 len(optimizer.param_groups), len(base_lr)))
 self.base_lrs = list(base_lr)
 else:
self.base_lrs = [base_lr] * len(optimizer.param_groups)
 if isinstance(max_lr, list)or isinstance(max_lr, tuple):
if len(max_lr)!= len(optimizer.param_groups):
raise ValueError("expected {} max_lr, got {}".format(
 len(optimizer.param_groups), len(max_lr)))
 self.max_lrs = list(max_lr)
 else:
self.max_lrs = [max_lr] * len(optimizer.param_groups)
 self.step_size = step_size
if mode not in ['triangular', 'triangular2', 'exp_range'] \
and scale_fn is None:
raise ValueError('mode is invalid and scale_fn is None')
 self.mode = mode
self.gamma = gamma
if scale_fn is None:
if self.mode == 'triangular':
self.scale_fn = self._triangular_scale_fn
self.scale_mode = 'cycle'
elif self.mode == 'triangular2':
self.scale_fn = self._triangular2_scale_fn
self.scale_mode = 'cycle'
elif self.mode == 'exp_range':
self.scale_fn = self._exp_range_scale_fn
self.scale_mode = 'iterations'
else:
self.scale_fn = scale_fn
self.scale_mode = scale_mode
self.batch_step(last_batch_iteration + 1)
 self.last_batch_iteration = last_batch_iteration
def batch_step(self, batch_iteration=None):
if batch_iteration is None:
batch_iteration = self.last_batch_iteration + 1
self.last_batch_iteration = batch_iteration
for param_group, lr in zip(self.optimizer.param_groups, self.get_lr()):
param_group['lr'] = lr
def _triangular_scale_fn(self, x):
return 1.
 def _triangular2_scale_fn(self, x):
return 1 /(2.**(x - 1))
def _exp_range_scale_fn(self, x):
return self.gamma**(x)
 def get_lr(self):
step_size = float(self.step_size)
 cycle = np.floor(1 + self.last_batch_iteration /(2 * step_size))
x = np.abs(self.last_batch_iteration / step_size - 2 * cycle + 1)
 lrs = []
param_lrs = zip(self.optimizer.param_groups, self.base_lrs, self.max_lrs)
 for param_group, base_lr, max_lr in param_lrs:
base_height =(max_lr - base_lr)* np.maximum(0,(1 - x))
if self.scale_mode == 'cycle':
lr = base_lr + base_height * self.scale_fn(cycle)
 else:
lr = base_lr + base_height * self.scale_fn(self.last_batch_iteration)
 lrs.append(lr)
 return lrs
my_submission[['ConfirmedCases','Fatalities']]= my_submission[['ConfirmedCases','Fatalities']].round(1 )<save_to_csv>
class Simple_NN(nn.Module):
def __init__(self ,input_dim ,hidden_dim, dropout = 0.5):
super(Simple_NN, self ).__init__()
self.inpt_dim = input_dim
self.hidden_dim = hidden_dim
self.relu = nn.ReLU()
self.dropout = nn.Dropout(dropout)
 self.fc1 = nn.Linear(1, hidden_dim)
 self.fc2 = nn.Linear(int(hidden_dim*input_dim), 1)
 def forward(self, x):
b_size = x.size(0)
 x = x.view(-1, 1)
 y = self.fc1(x)
 y = self.relu(y)
 y = y.view(b_size, -1)
 out= self.fc2(y)
 return out
my_submission[[
'ForecastId', 'ConfirmedCases', 'Fatalities'
]].to_csv('submission.csv', index=False)
 print(DECAY)
 my_submission.head()
my_submission.tail()
my_submission.shape<load_from_csv>
def sigmoid(x):
return 1 /(1 + np.exp(-x))
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
print('--------------------------------------')
 print('Train data looks like...')
 trainData = pd.read_csv(".. /input/covid19-global-forecasting-week-3/train.csv")
 print(trainData.head(5))
print('Test data looks like...')
 testData = pd.read_csv(".. /input/covid19-global-forecasting-week-3/test.csv")
 print(testData.head(5))<data_type_conversions>
class EarlyStopping:

 def __init__(self, patience=2, verbose=False):

 self.patience = patience
self.verbose = verbose
self.counter = 0
self.best_score = None
self.early_stop = False
self.val_auc_min = 0
def __call__(self, val_auc, model):
score = val_auc
if self.best_score is None:
self.best_score = score
self.save_checkpoint(val_auc, model)
 elif score < self.best_score:
self.counter += 1
print(f'EarlyStopping counter: {self.counter} out of {self.patience}')
 if self.counter >= self.patience:
self.early_stop = True
else:
self.best_score = score
self.save_checkpoint(val_auc, model)
 self.counter = 0
def save_checkpoint(self, val_auc, model):

 if self.verbose:
print(f'Validation loss decreased({self.val_auc_min:.6f} --> {val_auc:.6f} ).Saving model...')
 torch.save(model.state_dict() , 'checkpoint.pt')
 self.val_auc_min = val_auc
def kaggle(dfTrain, dfTest):
pd.set_option('display.max_columns', None)
 dfTest['DateNew'] = pd.to_datetime(dfTest['Date'])
 dfTest = dfTest.drop(['Date'], axis=1)
 dfTest = dfTest.rename(columns={"DateNew": "Date"})
 dfTest['Year'] = dfTest['Date'].dt.year
dfTest['Month'] = dfTest['Date'].dt.month
dfTest['Day'] = dfTest['Date'].dt.day
dfTest = dfTest.drop(['Date'], axis=1)
 dfTest = dfTest.fillna('DummyProvince')
 dfTrain['DateNew'] = pd.to_datetime(dfTrain['Date'])
 dfTrain = dfTrain.drop(['Date'], axis=1)
 dfTrain = dfTrain.rename(columns={"DateNew": "Date"})
 dfTrain['Year'] = dfTrain['Date'].dt.year
dfTrain['Month'] = dfTrain['Date'].dt.month
dfTrain['Day'] = dfTrain['Date'].dt.day
dfTrain = dfTrain.drop(['Date'], axis=1)
 dfTrain = dfTrain.fillna('DummyProvince')
 result = pd.merge(dfTest, dfTrain, how='left', on=['Country_Region', 'Province_State', 'Year', 'Month', 'Day'])
 result = result.fillna(-1)
 print('Are the Kaggle submission files same? ', np.shape(dfTrain), np.shape(dfTest), np.shape(result))
return result
def ErrorCalc(mdl, ref, tag):
relError = np.abs(mdl - ref)/ np.abs(ref+1)
 MeanErrorV = np.mean(relError)
 print(tag + ': Mean Rel Error in %: ', MeanErrorV * 100)
 return MeanErrorV
def AdjustingErrorsOutliers(tempPred, tempPrev):
tempPred = np.round(tempPred)
 for i in range(len(tempPred)) :
if tempPred[i] < tempPrev[i] :
tempPred[i] = tempPrev[i]
return tempPred
def TrainMdl(trainIpData, trainOpData):
testSize = 0.1
print('Training starts...')
 randomState = None
X_train, X_test, y_train, y_test = train_test_split(trainIpData, trainOpData, test_size=testSize, random_state=randomState)
 TrainIP1 = X_train[['day1_I', 'day2_I', 'day3_I', 'day4_I', 'day5_I',
'diff1_I', 'diff2_I', 'diff3_I', 'diff4_I']]
TrainOP1 = X_train['gammaFunI']
TestIP1 = X_test[['day1_I', 'day2_I', 'day3_I', 'day4_I', 'day5_I',
'diff1_I', 'diff2_I', 'diff3_I', 'diff4_I']]
treeDepth = 10
mdl1 = DecisionTreeRegressor(max_depth=treeDepth)
 param_grid = {
'n_estimators': [100, 250, 500],
'learning_rate': [0.1, 0.01, 0.001]
}
regrMdl1 = AdaBoostRegressor(base_estimator=mdl1)
 clf1 = RandomizedSearchCV(estimator = regrMdl1, param_distributions = param_grid,
n_iter = 100, cv = 3, verbose=0, random_state=42, n_jobs = -1)
 clf1.fit(TrainIP1, TrainOP1)
 y_predictedTrain = clf1.predict(TrainIP1)
 y_predictedTrain = AdjustingErrorsOutliers(y_predictedTrain * TrainIP1['day5_I'].to_numpy() , TrainIP1['day5_I'].to_numpy())
 ErrorCalc(y_predictedTrain, X_train['dayPredictInf'].to_numpy() , 'Train Data-set model-1(infection rate)')
 y_predictedTest = clf1.predict(TestIP1)
 y_predictedTest = AdjustingErrorsOutliers(y_predictedTest * TestIP1['day5_I'].to_numpy() , TestIP1['day5_I'].to_numpy())
 ErrorCalc(y_predictedTest, X_test['dayPredictInf'].to_numpy() , 'Test Data-set model-1(infection rate)')
 TrainIP2 = X_train[['day1_I', 'day2_I', 'day3_I', 'day4_I', 'day5_I',
'diff1_I', 'diff2_I', 'diff3_I', 'diff4_I',
'day1_F', 'day2_F', 'day3_F', 'day4_F', 'day5_F',
'diff1_F', 'diff2_F', 'diff3_F', 'diff4_F']]
TrainOP2 = X_train['gammaFunF']
TestIP2 = X_test[['day1_I', 'day2_I', 'day3_I', 'day4_I', 'day5_I',
'diff1_I', 'diff2_I', 'diff3_I', 'diff4_I',
'day1_F', 'day2_F', 'day3_F', 'day4_F', 'day5_F',
'diff1_F', 'diff2_F', 'diff3_F', 'diff4_F']]
treeDepth = 10
mdl2 = DecisionTreeRegressor(max_depth=treeDepth)
 param_grid = {
'n_estimators': [100, 250, 500],
'learning_rate': [0.1, 0.01, 0.001]
}
regrMdl2 = AdaBoostRegressor(base_estimator=mdl2)
 clf2 = RandomizedSearchCV(estimator=regrMdl2, param_distributions=param_grid,
n_iter=100, cv=3, verbose=0, random_state=42, n_jobs=-1)
 clf2.fit(TrainIP2, TrainOP2)
 y_predictedTrain = clf2.predict(TrainIP2)* TrainIP2['day5_F'].to_numpy()
y_predictedTrain = AdjustingErrorsOutliers(y_predictedTrain, TrainIP2['day5_F'].to_numpy())
 ErrorCalc(y_predictedTrain, y_train.to_numpy() , 'Train Data-set model-2')
 y_predictedTest = clf2.predict(TestIP2)* TestIP2['day5_F'].to_numpy()
y_predictedTest = AdjustingErrorsOutliers(y_predictedTest, TestIP2['day5_F'].to_numpy())
 ErrorCalc(y_predictedTest, y_test.to_numpy() , 'Test Data-set model-2')
 dfValidation_Inf = pd.read_csv(".. /input/week3-covid19-traindata/Validation_Infected.csv" ).reset_index(drop=True)
 dfValidation_Fat = pd.read_csv(".. /input/week3-covid19-traindata/Validation_Fatality.csv" ).reset_index(drop=True)
 selRow = 0
startIdx = 12
[rVal, cVal] = np.shape(dfValidation_Inf)
 lengthZ = cVal-1
lengthZ = 12 * lengthZ
arrP = np.zeros(( 2 * lengthZ,))
arrA = np.zeros(( 2 * lengthZ,))
count = 0
error = 0
errorLen = 0
Threshold_I = 0
Threshold_F = 0
print('Validating...')
 while(selRow < rVal):
iDetect = startIdx
iArray = 0
while(iDetect < 17):
if iDetect == startIdx :
day5_I = dfValidation_Inf.iloc[selRow, iDetect]
day4_I = dfValidation_Inf.iloc[selRow, iDetect - 1]
day3_I = dfValidation_Inf.iloc[selRow, iDetect - 2]
day2_I = dfValidation_Inf.iloc[selRow, iDetect - 3]
day1_I = dfValidation_Inf.iloc[selRow, iDetect - 4]
if day5_I < Threshold_I:
day5_I = Threshold_I
day5_F = dfValidation_Fat.iloc[selRow, iDetect]
day4_F = dfValidation_Fat.iloc[selRow, iDetect - 1]
day3_F = dfValidation_Fat.iloc[selRow, iDetect - 2]
day2_F = dfValidation_Fat.iloc[selRow, iDetect - 3]
day1_F = dfValidation_Fat.iloc[selRow, iDetect - 4]
if day5_F < Threshold_F:
day5_F = Threshold_F
else:
day1_I = day2_I
day2_I = day3_I
day3_I = day4_I
day4_I = day5_I
day5_I = predictedInfected
day1_F = day2_F
day2_F = day3_F
day3_F = day4_F
day4_F = day5_F
day5_F = predictedFatality
diff1_I = day5_I - day4_I
diff2_I = day4_I - day3_I
diff3_I = day3_I - day2_I
diff4_I = day2_I - day1_I
diff1_F = day5_F - day4_F
diff2_F = day4_F - day3_F
diff3_F = day3_F - day2_F
diff4_F = day2_F - day1_F
data1 = {'day1_I': [day1_I], 'day2_I': [day2_I], 'day3_I': [day3_I], 'day4_I': [day4_I], 'day5_I': [day5_I],
'diff1_I': [diff1_I], 'diff2_I': [diff2_I], 'diff3_I': [diff3_I], 'diff4_I': [diff4_I]}
dfPredict1 = pd.DataFrame(data1)
 predictedInfected = clf1.predict(dfPredict1[['day1_I', 'day2_I', 'day3_I', 'day4_I', 'day5_I',
'diff1_I', 'diff2_I', 'diff3_I', 'diff4_I']])* day5_I
if predictedInfected < day5_I:
predictedInfected = day5_I
actVal = dfValidation_Inf.iloc[selRow, iDetect + 1]
arrP[iArray] = predictedInfected
arrA[iArray] = actVal
iArray = iArray + 1
data2 = {'day1_I': [day1_I], 'day2_I': [day2_I], 'day3_I': [day3_I], 'day4_I': [day4_I], 'day5_I': [day5_I],
'diff1_I': [diff1_I], 'diff2_I': [diff2_I], 'diff3_I': [diff3_I], 'diff4_I': [diff4_I],
'day1_F': [day1_F], 'day2_F': [day2_F], 'day3_F': [day3_F], 'day4_F': [day4_F], 'day5_F': [day5_F],
'diff1_F': [diff1_F], 'diff2_F': [diff2_F], 'diff3_F': [diff3_F], 'diff4_F': [diff4_F]}
dfPredict2 = pd.DataFrame(data2)
 predictedFatality = clf2.predict(dfPredict2[['day1_I', 'day2_I', 'day3_I', 'day4_I', 'day5_I',
'diff1_I', 'diff2_I', 'diff3_I', 'diff4_I',
'day1_F', 'day2_F', 'day3_F', 'day4_F', 'day5_F',
'diff1_F', 'diff2_F', 'diff3_F', 'diff4_F']])* day5_F
if predictedFatality < day5_F:
predictedFatality = day5_F
actVal = dfValidation_Fat.iloc[selRow, iDetect + 1]
arrP[iArray] = predictedFatality
arrA[iArray] = actVal
iDetect = iDetect + 1
iArray = iArray + 1
error = error + sum(np.square(np.log(arrP[0:iArray-1] + 1)- np.log(arrA[0:iArray-1] + 1)))
 errorLen = errorLen + iArray
selRow = selRow + 1
count = count + 1
error = float(error)/ errorLen
print('Validation error: ', error)
 print('Making Kaggle Submission file...')
 dfTrain = pd.read_csv(".. /input/covid19-global-forecasting-week-3/train.csv")
 dfTest = pd.read_csv(".. /input/covid19-global-forecasting-week-3/test.csv")
 df = kaggle(dfTrain, dfTest)
 print('Prediction starts for Kaggle submission...')
 [rr, cc] = np.shape(df)
 for iP in range(rr):
if df.loc[iP, 'ConfirmedCases'] == -1 :
day5_I = df.loc[iP-1, 'ConfirmedCases']
day4_I = df.loc[iP-2, 'ConfirmedCases']
day3_I = df.loc[iP-3, 'ConfirmedCases']
day2_I = df.loc[iP-4, 'ConfirmedCases']
day1_I = df.loc[iP-5, 'ConfirmedCases']
day5_F = df.loc[iP - 1, 'Fatalities']
day4_F = df.loc[iP - 2, 'Fatalities']
day3_F = df.loc[iP - 3, 'Fatalities']
day2_F = df.loc[iP - 4, 'Fatalities']
day1_F = df.loc[iP - 5, 'Fatalities']
diff1_I = day5_I - day4_I
diff2_I = day4_I - day3_I
diff3_I = day3_I - day2_I
diff4_I = day2_I - day1_I
diff1_F = day5_F - day4_F
diff2_F = day4_F - day3_F
diff3_F = day3_F - day2_F
diff4_F = day2_F - day1_F
data1 = {'day1_I': [day1_I], 'day2_I': [day2_I], 'day3_I': [day3_I], 'day4_I': [day4_I], 'day5_I': [day5_I],
'diff1_I': [diff1_I], 'diff2_I': [diff2_I], 'diff3_I': [diff3_I], 'diff4_I': [diff4_I]}
dfPredict1 = pd.DataFrame(data1)
 predictedInfected = clf1.predict(dfPredict1[['day1_I', 'day2_I', 'day3_I', 'day4_I', 'day5_I',
'diff1_I', 'diff2_I', 'diff3_I', 'diff4_I']])* day5_I
if predictedInfected < day5_I:
predictedInfected = day5_I
df.loc[iP, 'ConfirmedCases'] = np.round(predictedInfected)
 data2 = {'day1_I': [day1_I], 'day2_I': [day2_I], 'day3_I': [day3_I], 'day4_I': [day4_I], 'day5_I': [day5_I],
'diff1_I': [diff1_I], 'diff2_I': [diff2_I], 'diff3_I': [diff3_I], 'diff4_I': [diff4_I],
'day1_F': [day1_F], 'day2_F': [day2_F], 'day3_F': [day3_F], 'day4_F': [day4_F], 'day5_F': [day5_F],
'diff1_F': [diff1_F], 'diff2_F': [diff2_F], 'diff3_F': [diff3_F], 'diff4_F': [diff4_F]}
dfPredict2 = pd.DataFrame(data2)
 predictedFatality = clf2.predict(dfPredict2[['day1_I', 'day2_I', 'day3_I', 'day4_I', 'day5_I',
'diff1_I', 'diff2_I', 'diff3_I', 'diff4_I',
'day1_F', 'day2_F', 'day3_F', 'day4_F', 'day5_F',
'diff1_F', 'diff2_F', 'diff3_F', 'diff4_F']])* day5_F
if predictedFatality < day5_F:
predictedFatality = day5_F
df.loc[iP, 'Fatalities'] = np.round(predictedFatality)
 return df
df = pd.read_csv(".. /input/week3-covid19-traindata/TrainTest.csv")
 trainIpData = df[['day1_I', 'day2_I', 'day3_I', 'day4_I', 'day5_I',
'diff1_I', 'diff2_I', 'diff3_I', 'diff4_I',
'day1_F', 'day2_F', 'day3_F', 'day4_F', 'day5_F',
'diff1_F', 'diff2_F', 'diff3_F', 'diff4_F', 'gammaFunF', 'gammaFunI', 'dayPredictInf']]
trainOpData = df['dayPredictFat']
predictions_dF = TrainMdl(trainIpData, trainOpData)
 predictions_dF[['ForecastId', 'ConfirmedCases', 'Fatalities']].to_csv('submission.csv', index=False)
 print(predictions_dF[['ForecastId', 'ConfirmedCases', 'Fatalities']].head(5))
print(predictions_dF[['ForecastId', 'ConfirmedCases', 'Fatalities']].tail(5))
print('Done!' )<load_from_csv>
def augment(x,y,t=2):
xs,xn = [],[]
for i in range(t):
mask = y>0
x1 = x[mask].copy()
ids = np.arange(x1.shape[0])
 for c in range(x1.shape[1]):
np.random.shuffle(ids)
 x1[:,c] = x1[ids][:,c]
xs.append(x1)
 for i in range(t//2):
mask = y==0
x1 = x[mask].copy()
ids = np.arange(x1.shape[0])
 for c in range(x1.shape[1]):
np.random.shuffle(ids)
 x1[:,c] = x1[ids][:,c]
xn.append(x1)
 xs = np.vstack(xs)
 xn = np.vstack(xn)
 ys = np.ones(xs.shape[0])
 yn = np.zeros(xn.shape[0])
 x = np.vstack([x,xs,xn])
 y = np.concatenate([y,ys,yn])
 return x,y
titanic=pd.read_csv('/kaggle/input/titanic/train.csv')
 titanic.head()<categorify>
n_epochs = 20000
batch_size = 256
train_preds = np.zeros(( len(train_features)))
 test_preds = np.zeros(( len(test_features)))
 x_test = np.array(test_features)
 x_test_cuda = torch.tensor(x_test, dtype=torch.float ).cuda()
test = torch.utils.data.TensorDataset(x_test_cuda)
 test_loader = torch.utils.data.DataLoader(test, batch_size=batch_size, shuffle=False)
 avg_losses_f = []
avg_val_losses_f = []
val_auc=[]
for i,(train_idx, valid_idx)in enumerate(splits):
valid_preds = []
x_train = np.array(train_features)
 y_train = np.array(train_target)
 x_train_fold = torch.tensor(x_train[train_idx.astype(int)], dtype=torch.float ).cuda()
y_train_fold = torch.tensor(y_train[train_idx.astype(int), np.newaxis], dtype=torch.float32 ).cuda()
x_val_fold = torch.tensor(x_train[valid_idx.astype(int)], dtype=torch.float ).cuda()
y_val_fold = torch.tensor(y_train[valid_idx.astype(int), np.newaxis], dtype=torch.float32 ).cuda()
loss_fn = torch.nn.BCEWithLogitsLoss()
model = Simple_NN(train_features.shape[1],32)
 model.cuda()
optimizer = torch.optim.Adam(model.parameters() , lr = 0.001,weight_decay=1e-5)
 step_size = 2000
base_lr, max_lr = 0.0001, 0.002
optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad, model.parameters()),
lr=max_lr)
 scheduler = CyclicLR(optimizer, base_lr=base_lr, max_lr=max_lr,
step_size=step_size, mode='exp_range',
gamma=0.99994)
 train = torch.utils.data.TensorDataset(x_train_fold, y_train_fold)
 valid = torch.utils.data.TensorDataset(x_val_fold, y_val_fold)
 train_loader = torch.utils.data.DataLoader(train, batch_size=batch_size, shuffle=True)
 valid_loader = torch.utils.data.DataLoader(valid, batch_size=batch_size, shuffle=False)
 print(f'Fold {i + 1}')
 early_stopping = EarlyStopping(patience=8, verbose=True)
 for epoch in range(n_epochs):
start_time = time.time()
model.train()
avg_loss = 0.
 for i,(x_batch, y_batch)in enumerate(train_loader):
y_pred = model(x_batch)
 if scheduler:
scheduler.batch_step()
loss = loss_fn(y_pred, y_batch)
 optimizer.zero_grad()
loss.backward()
optimizer.step()
avg_loss += loss.item() /len(train_loader)
 model.eval()
valid_preds_fold = np.zeros(( x_val_fold.size(0)))
 test_preds_fold = np.zeros(( len(test_features)))
 avg_val_loss = 0.
 avg_val_auc = 0.
 for i,(x_batch, y_batch)in enumerate(valid_loader):
y_pred = model(x_batch ).detach()
avg_val_auc += round(roc_auc_score(y_batch.cpu() ,sigmoid(y_pred.cpu().numpy())[:, 0]),4)/ len(valid_loader)
 avg_val_loss += loss_fn(y_pred, y_batch ).item() / len(valid_loader)
 valid_preds_fold[i * batch_size:(i+1)* batch_size] = sigmoid(y_pred.cpu().numpy())[:, 0]
valid_preds.append(valid_preds_fold)
 elapsed_time = time.time() - start_time
print('Epoch {}/{} \t loss={:.4f} \t val_loss={:.4f} \t time={:.2f}s'.format(
 epoch + 1, n_epochs, avg_loss, avg_val_loss, elapsed_time))
early_stopping(avg_val_auc, model)
 if early_stopping.early_stop:
valid_preds_fold = valid_preds[-9]
print("Early stopping")
 break
model.load_state_dict(torch.load('checkpoint.pt'))
avg_losses_f.append(avg_loss)
 avg_val_losses_f.append(avg_val_loss)
 for i,(x_batch,)in enumerate(test_loader):
y_pred = model(x_batch ).detach()
test_preds_fold[i * batch_size:(i+1)* batch_size] = sigmoid(y_pred.cpu().numpy())[:, 0]
train_preds[valid_idx] = valid_preds_fold
test_preds += test_preds_fold / len(splits)
 val_auc.append(roc_auc_score(train_target[valid_idx],train_preds[valid_idx]))
print(val_auc)
 print(np.mean(val_auc))
auc = round(roc_auc_score(train_target,train_preds),4)
 print('All \t loss={:.4f} \t val_loss={:.4f} \t auc={:.4f}'.format(np.average(avg_losses_f),np.average(avg_val_losses_f),auc))
titanic_dummies=pd.get_dummies(data=titanic,columns=['Sex','Embarked'],drop_first=True)
 titanic_dummies.info()<prepare_x_and_y>
test_ID = test_df['ID_code'].values
submission_nn = pd.DataFrame({ "ID_code": test_ID, "target": test_preds})
 submission_nn.to_csv('test_preds_submission_nn_10fold.csv', index=False )
<train_model><EOS> 
train_ID = train_df['ID_code'].values
submission_nn = pd.DataFrame({ "ID_code": train_ID, "target": train_preds})
 submission_nn.to_csv('train_preds_submission_nn_10fold.csv', index=False )