Kaggle competition: Digit Recognizer
mae = metrics.mean_absolute_error(y_val, y_val_pred)
 mse = metrics.mean_squared_error(y_val, y_val_pred)
 rmse = np.sqrt(metrics.mean_absolute_error(y_val, y_val_pred))
print("Mean Absolute Error")
 print(mae)
 print()
print("Mean Squared Error")
 print(mse)
 print()
print("Root Mean Squared Error")
 print(rmse )<create_dataframe>
digit_recon_tran_csv = pd.read_csv('/kaggle/input/digit-recognizer/train.csv',dtype = np.float32)
 digit_recon_test_csv = pd.read_csv('/kaggle/input/digit-recognizer/test.csv',dtype = np.float32 )
evaluation = evaluation_df('Extreme Gradient Boosting Tuning 1', mae, mse, rmse, evaluation )<choose_model_class>
print('tran dataset size: ',digit_recon_tran_csv.size,'
')
 print('test dataset size: ',digit_recon_test_csv.size,'
' )
<predict_on_test>
tran_label = digit_recon_tran_csv.label.values
tran_image = digit_recon_tran_csv.loc[:,digit_recon_tran_csv.columns != "label"].values/255
test_image = digit_recon_test_csv.values/255
<choose_model_class>
train_image, valid_image, train_label, valid_label = train_test_split(tran_image,
tran_label,
test_size = 0.2,
random_state = 42 )
%%time
xgbreg = xgb.XGBRegressor(base_score=0.5, booster='gbtree', colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=1, gamma=0, gpu_id=-1,
importance_type='gain', interaction_constraints='',
learning_rate=0.3, max_delta_step=0, max_depth=6,
min_child_weight=1, monotone_constraints='() ',
n_estimators=3000, n_jobs=16, num_parallel_tree=1, random_state=0,
reg_alpha=0, reg_lambda=1, scale_pos_weight=1, subsample=1,
tree_method='exact', validate_parameters=1, verbosity=None)
 xgbreg.fit(X_train_scalled, y_train )<predict_on_test>
print(torch.__version__)
 class MNIST_data(Dataset):

 def __init__(self,
data,
transform = transforms.Compose([transforms.ToPILImage() ,
transforms.RandomAffine(30,(0.1,0.1)) ,
transforms.ToTensor()
])
):
if len(data)== 1:
self.X = data[0].reshape(-1,28,28)
 self.y = None
else:
self.X = data[0].reshape(-1,28,28)
 self.y = data[1].astype(np.long)
 self.transform = transform
def __len__(self):
return len(self.X)
 def __getitem__(self, idx):
if self.y is not None:
return self.transform(self.X[idx]), self.y[idx]
else:
return self.transform(self.X[idx] )
y_train_pred = xgbreg.predict(X_train_scalled )<compute_test_metric>
batch_size = 64
train_dataset = MNIST_data(( train_image,train_label))
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=batch_size, shuffle=True)
 valid_dataset = MNIST_data(( valid_image,valid_label))
valid_loader = torch.utils.data.DataLoader(dataset=valid_dataset,
batch_size=batch_size, shuffle=False )
r2_score(y_train, y_train_pred )<predict_on_test>
class YANNet(nn.Module):
def __init__(self):
super(YANNet,self ).__init__()
self.conv = nn.Sequential(
 nn.Conv2d(1,8,3,1,1),
nn.BatchNorm2d(8),
nn.ReLU() ,
nn.Conv2d(8,16,3,1,1),
nn.BatchNorm2d(16),
nn.ReLU() ,
nn.MaxPool2d(2),
nn.Conv2d(16,16,3,1,1),
nn.BatchNorm2d(16),
nn.ReLU() ,
nn.Conv2d(16,8,3,1,1),
nn.BatchNorm2d(8),
nn.ReLU() ,
nn.MaxPool2d(2)
)
 self.fc = nn.Sequential(
 nn.Linear(8*7*7,256),
nn.BatchNorm1d(256),
nn.ReLU() ,
nn.Dropout(0.5),
nn.Linear(256,256),
nn.BatchNorm1d(256),
nn.ReLU() ,
nn.Dropout(0.5),
nn.Linear(256,10)
)
 def forward(self, img):
x = self.conv(img)
 o = self.fc(x.view(x.shape[0],-1))
return o
y_val_pred = xgbreg.predict(X_val_scalled )<compute_test_metric>
model = YANNet()
error = nn.CrossEntropyLoss()
if torch.cuda.is_available() :
model = model.cuda()
error = error.cuda()
optimizer = torch.optim.SGD(model.parameters() , lr=0.1)
 scheduler = lr_scheduler.StepLR(optimizer, step_size=30, gamma=0.1 )
r2_score(y_val, y_val_pred )<compute_test_metric>
num_epoc = 120
for epoch in range(num_epoc):
epoc_train_loss = 0.0
epoc_train_corr = 0.0
epoc_valid_corr = 0.0
print('Epoch:{}/{}'.format(epoch,num_epoc))
model.train()
scheduler.step()
for batch_idx,(images, labels)in enumerate(train_loader):
if torch.cuda.is_available() :
images = images.cuda()
labels = labels.cuda()
images = Variable(images)
 labels = Variable(labels)
 outputs = model(images)
 optimizer.zero_grad()
loss = error(outputs,labels)
 loss.backward()
optimizer.step()
epoc_train_loss += loss.data
outputs = torch.max(outputs.data,1)[1]
epoc_train_corr += torch.sum(outputs==labels.data)
 with torch.no_grad() :
model.eval()
for batch_idx,(images, labels)in enumerate(valid_loader):
if torch.cuda.is_available() :
images = images.cuda()
labels = labels.cuda()
images = Variable(images)
 labels = Variable(labels)
 outputs = model(images)
 outputs = torch.max(outputs.data,1)[1]
epoc_valid_corr += torch.sum(outputs==labels.data)
 print("loss is :{:.4f},Train Accuracy is:{:.4f}%,Test Accuracy is:{:.4f}%".format(epoc_train_loss/len(train_dataset),100*epoc_train_corr/len(train_dataset),100*epoc_valid_corr/len(valid_dataset)) )
mae = metrics.mean_absolute_error(y_val, y_val_pred)
 mse = metrics.mean_squared_error(y_val, y_val_pred)
 rmse = np.sqrt(metrics.mean_absolute_error(y_val, y_val_pred))
print("Mean Absolute Error")
 print(mae)
 print()
print("Mean Squared Error")
 print(mse)
 print()
print("Root Mean Squared Error")
 print(rmse )<compute_test_metric>
model = model.cpu()
model.eval()
evaluation = evaluation_df('Extreme Gradient Boosting with Tuning 2', mae, mse, rmse, evaluation )<count_missing_values>
digit_recon_submission_csv = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv',dtype = np.float32)
 print(digit_recon_submission_csv.head(10))
df_store.isnull().sum() * 100 / df_store.shape[0]<categorify>
for i in range(test_image.shape[0]):
one_image = torch.from_numpy(test_image[i] ).view(1,1,28,28)
 one_output = model(one_image)
 test_results[i,0] = i+1
test_results[i,1] = torch.max(one_output.data,1)[1].numpy()
<feature_engineering><EOS> 
Data = {'ImageId': test_results[:, 0], 'Label': test_results[:, 1]}
DataFrame = pd.DataFrame(Data)
 DataFrame.to_csv('submission.csv', index=False, sep=',' )