Kaggle competition: COVID19 Global Forecasting (Week 1)
feature_columns = [
col for col in df.columns if col.startswith("feat_")
 ]
target_column = ["target_ord"]<split>
from sklearn.preprocessing import OneHotEncoder
import datetime as dt
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
X_train, X_val, y_train, y_val = train_test_split(
 df[feature_columns], df[target_column],
test_size=0.3, random_state=42,
stratify=df[target_column]
)<create_dataframe>
train=pd.read_csv('.. /input/covid19-global-forecasting-week-1/train.csv')
 test=pd.read_csv('.. /input/covid19-global-forecasting-week-1/test.csv')
 submission=pd.read_csv('.. /input/covid19-global-forecasting-week-1/submission.csv' )
dtrain = lgb_org.Dataset(X_train, y_train)
 dval = lgb_org.Dataset(X_val, y_val )<train_model>
train['Province/State'].fillna('No Province',inplace=True )
params = dict(
 objective="multiclass",
metric="multi_logloss",
num_class=9,
seed=42, 
)
 best_params, tuning_history = dict() , list()
booster = lgb.train(params, dtrain, valid_sets=dval,
verbose_eval=0,
best_params=best_params,
early_stopping_rounds=5,
tuning_history=tuning_history)
 print("Best Params:", best_params)
 print("Tuning history:", tuning_history )<load_from_csv>
test['Province/State'].fillna('No Province',inplace=True )
df_test = read_csv(str(base_dir / "test.csv"))<predict_on_test>
ohe=OneHotEncoder(handle_unknown='ignore' )
pred = booster.predict(df_test[feature_columns] )<prepare_output>
train['Date']= pd.to_datetime(train['Date'])
 test['Date']= pd.to_datetime(test['Date'] )
for idx, col in order_to_class.items() :
df_test[col] = pred[:,idx]<save_to_csv>
def create_time_features(df):
df['date'] = df['Date']
df['hour'] = df['date'].dt.hour
df['dayofweek'] = df['date'].dt.dayofweek
df['quarter'] = df['date'].dt.quarter
df['month'] = df['date'].dt.month
df['year'] = df['date'].dt.year
df['dayofyear'] = df['date'].dt.dayofyear
df['dayofmonth'] = df['date'].dt.day
df['weekofyear'] = df['date'].dt.weekofyear
X = df[['hour','dayofweek','quarter','month','year',
'dayofyear','dayofmonth','weekofyear']]
return X
df_test[["id"] + [f"Class_{i}" for i in range(1, 10)]].to_csv('submission.csv', index=False )<set_options>
create_time_features(train)
 create_time_features(test )
torch.manual_seed(0)
 np.random.seed(0 )<categorify>
train=train.drop(columns=['Date'],axis=1)
 test=test.drop(columns=['Date'],axis=1 )
class OttoDataset(Dataset):
def __init__(self, data, target, idx, mode):
self.data = data
self.target = target
self.idx = np.asarray(idx)
 self.mode = mode
def __len__(self):
return self.idx.shape[0]
def __getitem__(self, index):
index = self.idx[index]
row = self.data[index]
if self.mode == 'test':
return torch.tensor(row)
 else:
label = self.target[index]
return torch.tensor(row), torch.tensor(label )<load_from_csv>
train=train.drop(columns=['date'],axis=1)
 test=test.drop(columns=['date'],axis=1 )
train = pd.read_csv('.. /input/otto-group-product-classification-challenge/train.csv')
 test = pd.read_csv('.. /input/otto-group-product-classification-challenge/test.csv')
 sample_submit = pd.read_csv('.. /input/otto-group-product-classification-challenge/sampleSubmission.csv' )<data_type_conversions>
s =(train.dtypes == 'object')
 object_cols = list(s[s].index)
 print("Categorical variables:")
 print(object_cols )
train['target'] = train['target'].str.replace('Class_', '')
 train['target'] = train['target'].astype(int)- 1<normalization>
train_dummies=pd.get_dummies(train['Country/Region'] )
excluded_column = ['target', 'id']
cols = [c for c in train.columns if c not in(excluded_column + [])]
data = pd.concat([train, test] ).reset_index()
for c in cols:
data[c] = np.log(1+data[c])
 data_scale = StandardScaler().fit_transform(data[cols])
 data = pd.concat([data[['id', 'target']], pd.DataFrame(data_scale)], axis=1)
 train = data[~data['target'].isnull() ].reset_index(drop=True)
 test = data[data['target'].isnull() ].reset_index(drop=True )<create_dataframe>
test_dummies=pd.get_dummies(test['Country/Region'] )
excluded_column = ['target', 'id']
cols = [c for c in train.columns if c not in excluded_column]
test_dataset = OttoDataset(test[cols].values, np.zeros(test.shape[0]), list(range(test.shape[0])) , 'test')
 test_loader = torch.utils.data.DataLoader(
 dataset=test_dataset,
batch_size=64,
shuffle=False,
num_workers=2 )<normalization>
train1_dummies=pd.get_dummies(train['Province/State'])
 test1_dummies=pd.get_dummies(test['Province/State'] )
class MLPNet(nn.Module):
def __init__(self):
super(MLPNet, self ).__init__()
self.fc1 = nn.Linear(93, 512)
 self.fc2 = nn.Linear(512, 512)
 self.fc3 = nn.Linear(512, 512)
 self.fc4 = nn.Linear(512, 9)
 self.dropout1 = nn.Dropout2d(0.2)
 self.dropout2 = nn.Dropout2d(0.2)
 self.dropout3 = nn.Dropout2d(0.2)
 def forward(self, x):
x = F.relu(self.fc1(x))
x = self.dropout1(x)
 x = F.relu(self.fc2(x))
x = self.dropout2(x)
 x = F.relu(self.fc3(x))
x = self.dropout3(x)
 x = self.fc4(x)
 return x<choose_model_class>
train=train.drop(['Country/Region','Province/State','Id'],axis=1)
 test=test.drop(['Country/Region','Province/State','ForecastId'],axis=1 )
device = 'cuda' if torch.cuda.is_available() else 'cpu'
num_epochs = 50
NFOLDS = 5
RANDOM_STATE = 871972
folds = StratifiedKFold(n_splits=NFOLDS, shuffle=True,
random_state=RANDOM_STATE )<prepare_x_and_y>
train=pd.concat([train,train_dummies,train1_dummies],axis=1 )
y_preds = []
oof = np.zeros(( len(train), 9))
for fold_n,(train_index, valid_index)in enumerate(folds.split(train, y = train['target'])) :
X_train, X_valid = train.iloc[train_index], train.iloc[valid_index]
y_train, y_valid = X_train['target'].astype(int), X_valid['target'].astype(int)
 train_dataset = OttoDataset(X_train[cols].values, y_train.values, list(range(X_train.shape[0])) , 'train')
 valid_dataset = OttoDataset(X_valid[cols].values, y_valid.values, list(range(X_valid.shape[0])) , 'train')
 train_loader = torch.utils.data.DataLoader(
 dataset=train_dataset,
batch_size=64,
shuffle=True,
num_workers=2)
 valid_loader = torch.utils.data.DataLoader(
 dataset=valid_dataset,
batch_size=64,
shuffle=False,
num_workers=2)
 net = MLPNet().to(device)
 criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(net.parameters() , lr=0.01, momentum=0.9, weight_decay=5e-4)
 for epoch in range(num_epochs):
train_loss, train_acc, val_loss, val_acc = 0, 0, 0, 0
net.train()
for i,(datas, labels)in enumerate(train_loader):
datas, labels = datas.to(device), labels.to(device)
 optimizer.zero_grad()
feature = net(datas.float())
 outputs = feature
loss = criterion(outputs, labels)
 train_loss += loss.item()
acc =(outputs.max(1)[1] == labels ).sum()
train_acc += acc.item()
loss.backward()
optimizer.step()
avg_train_loss = train_loss / len(train_loader.dataset)
 avg_train_acc = train_acc / len(train_loader.dataset)
 net.eval()
with torch.no_grad() :
for datas, labels in valid_loader:
datas, labels = datas.to(device), labels.to(device)
 feature = net(datas.float())
 outputs = feature
loss = criterion(outputs, labels)
 val_loss += loss.item()
acc =(outputs.max(1)[1] == labels ).sum()
val_acc += acc.item()
avg_val_loss = val_loss / len(valid_loader.dataset)
 avg_val_acc = val_acc / len(valid_loader.dataset)
 print('Epoch [{}/{}], Loss: {loss:.4f}, val_loss: {val_loss:.4f}, val_acc: {val_acc:.4f}' 
.format(epoch+1, num_epochs, i+1, loss=avg_train_loss, val_loss=avg_val_loss, val_acc=avg_val_acc))
net.eval()
valid_predict = []
with torch.no_grad() :
for datas, labels in valid_loader:
datas, labels = datas.to(device), labels.to(device)
 feature = net(datas.float())
 outputs = F.softmax(feature, dim=1)
 valid_predict.append(outputs)
 oof[valid_index] = torch.cat(valid_predict, dim=0)
 test_predict = []
with torch.no_grad() :
for datas in test_loader:
datas = datas.to(device)
 feature = net(datas.float())
 outputs = F.softmax(feature, dim=1)
 test_predict.append(outputs)
 y_preds.append(torch.cat(test_predict, dim=0))<save_to_csv>
test=pd.concat([test,test_dummies,test1_dummies],axis=1 )
column_name = ['nn_' + str(i)for i in range(9)]
pd.DataFrame(oof, columns = column_name ).to_csv('oof_nn.csv', index=False)
 y_pred_nn = np.zeros(( len(y_preds[0]), 9))
for i in range(len(y_preds)) :
y_pred_nn += y_preds[i].cpu().numpy() / len(y_preds)
 pd.DataFrame(y_pred_nn, columns = column_name ).to_csv('submit_nn.csv', index=False )<save_to_csv>
features=train.drop(['ConfirmedCases','Fatalities'],axis=1)
 target1=train['ConfirmedCases']
target2=train['Fatalities']
submit = pd.concat([sample_submit[['id']], pd.DataFrame(y_pred_nn)], axis = 1)
 submit.columns = sample_submit.columns
submit.to_csv('submit.csv', index=False )<set_options>
rf=DecisionTreeRegressor(criterion='mse', splitter='best' )
%matplotlib inline 
<load_from_csv>
rf.fit(features,target1 )
train = pd.read_csv('.. /input/otto-group-product-classification-challenge/train.csv')
 test = pd.read_csv('.. /input/otto-group-product-classification-challenge/test.csv')
 sample_submit = pd.read_csv('.. /input/otto-group-product-classification-challenge/sampleSubmission.csv' )<data_type_conversions>
predict_cases=rf.predict(test )
train['target'] = train['target'].str.replace('Class_', '')
 train['target'] = train['target'].astype(int)- 1<feature_engineering>
submission['ConfirmedCases']=predict_cases
data = pd.concat([train, test])
 cols = [c for c in data.columns if c not in ['id', 'target']]
for col in cols:
dictionary=data[col].value_counts().to_dict()
data['count_'+col]=data[col].map(dictionary)
 data['max_val'] = data[cols].max(axis=1)
 data['sum_val'] = data[cols].sum(axis=1)
 data['non_zero'] =(data[cols] > 0 ).sum(axis=1)
 data['count_one'] =(data[cols] == 1 ).sum(axis=1)
 data['count_two'] =(data[cols] == 2 ).sum(axis=1)
 data['count_three'] =(data[cols] == 3 ).sum(axis=1)
 train = data[~data['target'].isnull() ].reset_index(drop=True)
 test = data[data['target'].isnull() ].reset_index(drop=True )<init_hyperparams>
rf.fit(features,target2)
 predict_fatalities=rf.predict(test)
 submission['Fatalities']=predict_fatalities
NFOLDS = 5
RANDOM_STATE = 871972
excluded_column = ['target', 'id']
cols = [c for c in train.columns if c not in excluded_column]
folds = StratifiedKFold(n_splits=NFOLDS, shuffle=True,
random_state=RANDOM_STATE)
 params = {
'metric':'multi_logloss','objective': 'multiclass', 'num_class': 9, 'verbosity': 1,
'feature_fraction': 0.4, 'num_leaves': 139, 'bagging_fraction': 0.8254401463359962, 'bagging_freq': 3,
'lambda_l1': 0.02563829140437355, 'lambda_l2': 9.594334397031103, 'min_child_samples': 100
}<prepare_x_and_y>
submission.round().astype(int )
<save_to_csv><EOS> 
submission.to_csv('submission.csv',index=False )