Kaggle competition: COVID19 Global Forecasting (Week 1)
<SOS> metric: MCRMSLE Kaggle data source: covid19-global-forecasting-(week-1)<groupby>
import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
n_match = train_results[train_results["ypred"]!=train_results["yactual"]]
print(n_match.shape)
 n_match.groupby(by="yactual" ).count()<data_type_conversions>
df = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-1/train.csv')
 test = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-1/test.csv')
 submission = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-1/submission.csv' )
df_test=df_test_raw.iloc[:,1:]
x = df_test.astype('float32')
 test=np.array(x ).reshape(x.shape[0],image_x,image_y,1)
 test/=255.0<predict_on_test>
ids = submission['ForecastId']
y_pred=model_cnn2.predict(test )<prepare_output>
input_cols = ["Lat","Long","Date"]
output_cols = ["ConfirmedCases","Fatalities"]
y_list=[]
for i in range(len(y_pred)) :
y_list.append(np.argmax(y_pred[i]))
result=df_test_raw[["id"]]
result["label"] = y_list 
<save_to_csv>
for i in range(df.shape[0]):
df["Date"][i] = df["Date"][i][:4] + df["Date"][i][5:7] + df["Date"][i][8:]
df["Date"][i] = int(df["Date"][i] )
result.to_csv("submission.csv",index=False )<load_from_csv>
for i in range(test.shape[0]):
test["Date"][i] = test["Date"][i][:4] + test["Date"][i][5:7] + test["Date"][i][8:]
test["Date"][i] = int(test["Date"][i] )
print("Reading data")
 train_set = pd.read_csv("/kaggle/input/Kannada-MNIST/train.csv")
 val_set = pd.read_csv("/kaggle/input/Kannada-MNIST/Dig-MNIST.csv")
 test_set = pd.read_csv("/kaggle/input/Kannada-MNIST/test.csv")
 train_images = train_set.iloc[:, 1:]
train_labels = train_set.iloc[:, 0]
val_images = val_set.iloc[:, 1:]
val_labels = val_set.iloc[:, 0]
test_images = test_set.iloc[:, 1:]<categorify>
X = df[input_cols]
Y1 = df[output_cols[0]]
Y2 = df[output_cols[1]]
train_transform = transforms.Compose(( [
transforms.ToPILImage() ,
transforms.RandomCrop(28),
transforms.RandomAffine(degrees=5, translate=(0.1, 0.1)) ,
transforms.ToTensor() ,
]))
test_transform = transforms.Compose(( [
transforms.ToPILImage() ,
transforms.ToTensor() ,
]))<prepare_x_and_y>
X_test = test[input_cols]
class KannadaDataset(torch.utils.data.Dataset):
def __init__(self, images, labels=None, transforms=None):
self.X = images
self.y = labels
self.transforms = transforms
def __len__(self):
return len(self.X)
 def __getitem__(self, i):
data = self.X.iloc[i, :]
data = np.array(data ).astype(np.uint8 ).reshape(28, 28, 1)
 if self.transforms:
data = self.transforms(data)
 if self.y is not None:
return(data, self.y[i])
 else:
return data
print("Dataset class created" )<choose_model_class>
sk_tree = DecisionTreeClassifier(criterion='entropy' )
print("Defining network architecture")
 class network(nn.Module):
def __init__(self):
super(network, self ).__init__()
self.conv1_5 = nn.Conv2d(in_channels=1, out_channels=64, kernel_size=5, padding=2)
 self.conv1_5_bn = nn.BatchNorm2d(num_features=64)
 self.conv1_3 = nn.Conv2d(in_channels=1, out_channels=64, kernel_size=3, padding=1)
 self.conv1_3_bn = nn.BatchNorm2d(num_features=64)
 self.conv2_5 = nn.Conv2d(in_channels=128, out_channels=128, kernel_size=5, padding=2)
 self.conv2_5_bn = nn.BatchNorm2d(num_features=128)
 self.conv2_3 = nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3, padding=1)
 self.conv2_3_bn = nn.BatchNorm2d(num_features=128)
 self.conv3_5 = nn.Conv2d(in_channels=256, out_channels=256, kernel_size=5, padding=2)
 self.conv3_5_bn = nn.BatchNorm2d(num_features=256)
 self.conv3_3 = nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, padding=1)
 self.conv3_3_bn = nn.BatchNorm2d(num_features=256)
 self.fc1 = nn.Linear(in_features=512*6*6, out_features=512)
 self.fc1_bn = nn.BatchNorm1d(num_features=512)
 self.fc2 = nn.Linear(in_features=512, out_features=256)
 self.fc2_bn = nn.BatchNorm1d(num_features=256)
 self.fc3 = nn.Linear(in_features=256, out_features=128)
 self.fc3_bn = nn.BatchNorm1d(num_features=128)
 self.fc4 = nn.Linear(in_features=128, out_features=64)
 self.fc4_bn = nn.BatchNorm1d(num_features=64)
 self.out = nn.Linear(in_features=64, out_features=10)
 def forward(self, x):
x_5 = F.relu(self.conv1_5_bn(self.conv1_5(x)))
 x_3 = F.relu(self.conv1_3_bn(self.conv1_3(x)))
 x = F.max_pool2d(torch.cat(( x_5, x_3), dim=1), kernel_size=2, stride=2)
 x_5 = F.relu(self.conv2_5_bn(self.conv2_5(x)))
 x_3 = F.relu(self.conv2_3_bn(self.conv2_3(x)))
 x = F.max_pool2d(torch.cat(( x_5, x_3), dim=1), kernel_size=2, stride=2)
 x_5 = F.relu(self.conv3_5_bn(self.conv3_5(x)))
 x_3 = F.relu(self.conv3_3_bn(self.conv3_3(x)))
 x = F.max_pool2d(torch.cat(( x_5, x_3), dim=1), kernel_size=2, stride=1)
 x = F.relu(self.fc1_bn(self.fc1(x.view(-1, 6*6*512))))
x = F.relu(self.fc2_bn(self.fc2(x)))
 x = F.relu(self.fc3_bn(self.fc3(x)))
 x = F.relu(self.fc4_bn(self.fc4(x)))
 x = self.out(x)
 return x<train_on_grid>
sk_tree.fit(X,Y1 )
net = network().to(device)
 lr = 0.001
batch_size = 100
max_epochs = 10<create_dataframe>
pred1 = sk_tree.predict(X_test )
train_set = KannadaDataset(train_images, train_labels, train_transform)
 val_set = KannadaDataset(val_images, val_labels, test_transform)
 train_data = DataLoader(train_set, batch_size=batch_size, shuffle=True)
 val_data = DataLoader(val_set, batch_size=batch_size, shuffle=False)
 print("Begin training" )<train_on_grid>
sk_tree.fit(X,Y2 )
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters() , lr=lr, momentum=0.9)
 scheduler = optim.lr_scheduler.StepLR(optimizer, 4, gamma=0.1)
 for epoch in range(max_epochs):
epoch_loss = 0.
 epoch_accuracy = 0.
 net.train()
for i,(images, labels)in enumerate(train_data):
images = images.to(device)
 labels = labels.to(device)
 optimizer.zero_grad()
outputs = net(images)
 loss = criterion(outputs, labels)
 loss.backward()
optimizer.step()
_, predicted = torch.max(outputs, 1)
 if(i+1)% 100 == 0:
print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'.format(epoch+1, max_epochs, i+1, len(train_set)//batch_size, loss.item()))
net.eval()
with torch.no_grad() :
val_accuracy = 0.
 for images, labels in val_data:
images = images.to(device)
 labels = labels.to(device)
 outputs = net(images)
 _, predicted = torch.max(outputs, 1)
 val_accuracy +=(predicted==labels ).sum().item()
print('Validation accuracy is: {} %'.format(100 * val_accuracy / len(val_set)))
 scheduler.step()<save_to_csv>
pred2 = sk_tree.predict(X_test )
test_set = KannadaDataset(images=test_images, transforms=test_transform)
 test_data = DataLoader(test_set, batch_size=1, shuffle=False)
 net.eval()
predictions = torch.LongTensor().to(device)
 with torch.no_grad() :
for images in test_data:
images = images.to(device)
 outputs = net(images)
 predictions = torch.cat(( predictions, outputs.argmax(dim=1)) , dim=0)
 submission = pd.read_csv("/kaggle/input/Kannada-MNIST/sample_submission.csv")
 submission['label'] = predictions.cpu().numpy()
submission.to_csv("/kaggle/working/submission.csv", index=False)
 print("Output written to csv file!" )<load_from_csv>
output = pd.DataFrame({ 'ForecastId' : ids, 'ConfirmedCases': pred1,'Fatalities':pred2 })
 output.to_csv('submission.csv', index=False )