Kaggle competition: Digit Recognizer
df_test['Date'] = pd.to_datetime(df_test['Date'], format = '%Y-%m-%d' )<feature_engineering>
d_train = pd.read_csv(".. /input/digit-recognizer/train.csv")
 d_test = pd.read_csv(".. /input/digit-recognizer/test.csv" )
df_test['Year'] = df_test['Date'].map(lambda x: x.year )<drop_column>
d_test.head()
df_test.drop('Date', inplace = True, axis = 1 )<count_values>
x_train = d_train.iloc[:, 1:].to_numpy(dtype = np.float)
 x_train = np.reshape(x_train, newshape =(len(x_train), 1, 28, 28))
y_train = d_train['label'].to_numpy()
print(x_train.shape)
 print(y_train.shape)
 x_test = d_test.to_numpy(dtype = np.float)
 x_test = np.reshape(x_test, newshape =(len(x_test), 1, 28, 28))
print(x_test.shape )
df_test['StateHoliday'].value_counts()<count_values>
x_train = x_train / 255
x_test = x_test / 255
print(x_train[0])
 print(x_test[0] )
df_test['StateHoliday'] = df_test['StateHoliday'].apply(lambda x: 1 if x == 'a' else(2 if x == 'b' else(3 if x == 'c' else x)))
 df_test['StateHoliday'].value_counts()<feature_engineering>
X_tr = torch.from_numpy(x_train ).type(torch.FloatTensor)
 Y_tr = torch.from_numpy(y_train)
 X_te = torch.from_numpy(x_test ).type(torch.FloatTensor)
 Y_te = torch.from_numpy(np.zeros(x_test.shape))
X_train, X_va, Y_train, Y_va = train_test_split(X_tr, Y_tr, test_size = 0.2)
 train_set = torch.utils.data.TensorDataset(X_train, Y_train)
 val_set = torch.utils.data.TensorDataset(X_va, Y_va)
 test_set = torch.utils.data.TensorDataset(X_te, Y_te)
 trainloader = torch.utils.data.DataLoader(train_set, batch_size = 32, shuffle = False)
 valloader = torch.utils.data.DataLoader(val_set, batch_size = 32, shuffle = False)
 testloader = torch.utils.data.DataLoader(test_set, batch_size = 32, shuffle = False )
df_test['StateHoliday'] = df_test['StateHoliday'].map(lambda x: 0 if x == '0' else x)
 type(df_test['StateHoliday'][0] )<filter>
class NMIST_CNN(nn.Module):
def __init__(self):
super(NMIST_CNN, self ).__init__()
self.cn1 = nn.Conv2d(1, 16, kernel_size = 4, padding = 2)
 nn.init.xavier_normal_(self.cn1.weight)
 nn.init.zeros_(self.cn1.bias)
 self.bn1 = nn.BatchNorm2d(num_features = 16)
 self.cn2 = nn.Conv2d(16, 32, kernel_size = 4)
 nn.init.xavier_normal_(self.cn2.weight)
 nn.init.zeros_(self.cn2.bias)
 self.bn2 = nn.BatchNorm2d(num_features = 32)
 self.fc1 = nn.Linear(800, 200)
 nn.init.xavier_normal_(self.fc1.weight)
 nn.init.zeros_(self.fc1.bias)
 self.bn3 = nn.BatchNorm1d(num_features = 200)
 self.fc2 = nn.Linear(200, 10)
 self.bn4 = nn.BatchNorm1d(num_features = 10)
 self.dropout = nn.Dropout(p = 0.2)
 self.pooling = nn.MaxPool2d(( 2,2))
def forward(self, x):
x = F.relu(self.bn1(self.cn1(x)))
 x = self.pooling(x)
 x = self.dropout(x)
 x = F.relu(self.bn2(self.cn2(x)))
 x = self.pooling(x)
 x = self.dropout(x)
 x = x.view(-1, 800)
 x = F.relu(self.bn3(self.fc1(x)))
 x = self.dropout(x)
 x = F.log_softmax(self.bn4(self.fc2(x)) , dim = 1)
 return x
df_test_open = df_test[df_test['Open'] == 1]
df_test_closed = df_test[df_test['Open'] == 0]<feature_engineering>
model = NMIST_CNN()
print(model )
df_test_closed['Sales'] = 0<drop_column>
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters() , lr=0.003)
 epochs = 30
train_losses, test_losses, train_accuracies, test_accuracies = [], [], [], []
for e in range(epochs):
tr_loss = 0
tr_accuracy = 0
model.train()
for images, labels in trainloader:
optimizer.zero_grad()
results = model(images)
 loss = criterion(results, labels)
 loss.backward()
optimizer.step()
tr_loss += loss.item()
top_p, top_class = results.topk(1, dim=1)
 equals = top_class == labels.view(*top_class.shape)
 tr_accuracy += torch.mean(equals.type(torch.FloatTensor))
else:
te_loss = 0
te_accuracy = 0
with torch.no_grad() :
model.eval()
for images, labels in valloader:
test_results = model(images)
 loss2 = criterion(test_results, labels)
 te_loss += loss2.item()
top_p, top_class = test_results.topk(1, dim=1)
 equals = top_class == labels.view(*top_class.shape)
 te_accuracy += torch.mean(equals.type(torch.FloatTensor))
train_losses.append(tr_loss/len(trainloader))
train_accuracies.append(tr_accuracy.item() /len(trainloader))
test_losses.append(te_loss/len(valloader))
test_accuracies.append(te_accuracy.item() /len(valloader))
print("Epoch: " + str(e+1))
print("Train loss: " + str(tr_loss/len(trainloader)))
 print(f'Train accuracy: {tr_accuracy.item() *100/len(trainloader)}%')
 print("Validation loss: " + str(te_loss/len(valloader)))
 print(f'Validation accuracy: {te_accuracy.item() *100/len(valloader)}%')
 print('' )
df_test_open.drop('Open', inplace = True, axis = 1)
 df_test_closed.drop('Open', inplace = True, axis = 1 )<drop_column>
prediction = []
with torch.no_grad() :
model.eval()
for images, x in testloader:
predictions = model(images)
 top_p, top_class = predictions.topk(1, dim=1)
 for n in range(len(predictions)) :
prediction.append(top_class[n].item())
 submit = pd.DataFrame(data={'Label':prediction})
 submit.insert(0, 'ImageId', range(1, 1 + len(submit)) , True)
 submit.head()
<drop_column><EOS> 
submit.to_csv('submission.csv', index = False)
 submit.head()