Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<define_variables>
%matplotlib inline
target = 'winPlacePerc'<drop_column>
train_csv_path = '.. /input/train.csv'
test_csv_path = '.. /input/test.csv'
train_df = pd.read_csv(train_csv_path)
 test_df = pd.read_csv(test_csv_path)
 n_train = len(train_df)
 n_pixels = len(train_df.columns)- 1
n_class = len(set(train_df['label']))
print('Number of training samples: {0}'.format(n_train))
print('Number of training pixels: {0}'.format(n_pixels))
print('Number of classes: {0}'.format(n_class))
print(train_df.head())
 n_test = len(test_df)
 n_pixels = len(test_df.columns)
 print('Number of test samples: {0}'.format(n_test))
print('Number of test pixels: {0}'.format(n_pixels))
print(test_df.head() )
train_df_wp = train_df.drop(train_df[train_df.winPoints == 0].index )<compute_test_metric>
class MNISTDataset(Dataset):

 def __init__(self, dataframe,
transform = transforms.Compose([transforms.ToPILImage() ,
transforms.ToTensor() ,
transforms.Normalize(mean=(0.5,), std=(0.5,)) ])
):
df = dataframe
self.n_pixels = 784
if len(df.columns)== self.n_pixels:
self.X = df.values.reshape(( -1,28,28)).astype(np.uint8)[:,:,:,None]
self.y = None
else:
self.X = df.iloc[:,1:].values.reshape(( -1,28,28)).astype(np.uint8)[:,:,:,None]
self.y = torch.from_numpy(df.iloc[:,0].values)
 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] )
np.corrcoef(train_df_wp.winPoints, train_df_wp[target] )<count_missing_values>
RandAffine = transforms.RandomAffine(degrees=45, translate=(0.1, 0.1), scale=(0.8, 1.2))
train_df.isna().sum()<set_options>
batch_size = 64
train_transforms = transforms.Compose(
 [transforms.ToPILImage() ,
RandAffine,
transforms.ToTensor() ,
transforms.Normalize(mean=(0.5,), std=(0.5,)) ])
 val_test_transforms = transforms.Compose(
 [transforms.ToPILImage() ,
transforms.ToTensor() ,
transforms.Normalize(mean=(0.5,), std=(0.5,)) ])
 def get_dataset(dataframe, dataset=MNISTDataset,
transform=transforms.Compose([transforms.ToPILImage() ,
transforms.ToTensor() ,
transforms.Normalize(mean=(0.5,), std=(0.5,)) ])) :
return dataset(dataframe, transform=transform )
train_df.winPlacePerc.fillna(-1, inplace = True )<count_values>
train_df.matchType.value_counts()<categorify>
class MNISTResNet(ResNet):
def __init__(self):
super(MNISTResNet, self ).__init__(BasicBlock, [2, 2, 2, 2], num_classes=10)
 self.conv1 = nn.Conv2d(1, 64, kernel_size=7, stride=1, padding=3,bias=False)
 model = MNISTResNet()
print(model )
def onehot(series, name):
series_new = pd.DataFrame()
for col in series.unique() :
series_new[name + "_{0}".format(col)] = series == col
return(series_new.astype(int), series_new.columns )<count_values>
def train(train_loader, model, criterion, optimizer, epoch):
model.train()
for batch_idx,(data, target)in enumerate(train_loader):
if torch.cuda.is_available() :
data = data.cuda()
target = target.cuda()
output = model(data)
 loss = criterion(output, target)
 optimizer.zero_grad()
loss.backward()
optimizer.step()
if(batch_idx + 1)% 100 == 0:
print('Train Epoch: {} [{}/{}({:.0f}%)]\tLoss: {:.6f}'.format(
 epoch,(batch_idx + 1)* len(data), len(train_loader.dataset),
100.*(batch_idx + 1)/ len(train_loader), loss.data.item()))
train_df.matchType.value_counts()<categorify>
def validate(val_loader, model, criterion):
model.eval()
loss = 0
correct = 0
for _,(data, target)in enumerate(val_loader):
if torch.cuda.is_available() :
data = data.cuda()
target = target.cuda()
output = model(data)
 loss += criterion(output, target ).data.item()
pred = output.data.max(1, keepdim=True)[1]
correct += pred.eq(target.data.view_as(pred)).cpu().sum()
loss /= len(val_loader.dataset)
 print('
On Val set Average loss: {:.4f}, Accuracy: {}/{}({:.3f}%)
 '.format(
 loss, correct, len(val_loader.dataset),
100.0 * float(correct)/ len(val_loader.dataset)) )
encoded_ = onehot(train_df.matchType, "matchType" )<merge>
total_epoches = 20
step_size = 5
base_lr = 0.01
optimizer = optim.Adam(model.parameters() , lr=base_lr)
 criterion = nn.CrossEntropyLoss()
exp_lr_scheduler = lr_scheduler.StepLR(optimizer, step_size=step_size, gamma=0.1)
 if torch.cuda.is_available() :
model = model.cuda()
criterion = criterion.cuda()
train_df_j = train_df.join(encoded_[0])
 train_df_j = train_df_j.drop('matchType', axis = 1)
 train_df_j<define_variables>
def split_dataframe(dataframe=None, fraction=0.9, rand_seed=1):
df_1 = dataframe.sample(frac=fraction, random_state=rand_seed)
 df_2 = dataframe.drop(df_1.index)
 return df_1, df_2
for epoch in range(total_epoches):
print("
Train Epoch {}: lr = {}".format(epoch, exp_lr_scheduler.get_lr() [0]))
train_df_new, val_df = split_dataframe(dataframe=train_df, fraction=0.9, rand_seed=epoch)
 train_dataset = get_dataset(train_df_new, transform=train_transforms)
 val_dataset = get_dataset(val_df, transform=val_test_transforms)
 train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=batch_size, shuffle=True)
 val_loader = torch.utils.data.DataLoader(dataset=val_dataset,
batch_size=batch_size, shuffle=False)
 train(train_loader=train_loader, model=model, criterion=criterion, optimizer=optimizer, epoch=epoch)
 validate(val_loader=val_loader, model=model, criterion=criterion)
 exp_lr_scheduler.step()
random_seed = 800<drop_column>
def prediciton(test_loader, model):
model.eval()
test_pred = torch.LongTensor()
for i, data in enumerate(test_loader):
if torch.cuda.is_available() :
data = data.cuda()
output = model(data)
 pred = output.cpu().data.max(1, keepdim=True)[1]
test_pred = torch.cat(( test_pred, pred), dim=0)
 return test_pred
features = train_df_j.columns
features = features.drop(['Id', 'groupId', 'matchId', target])
 features<split>
test_batch_size = 64
test_dataset = get_dataset(test_df, transform=val_test_transforms)
 test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
batch_size=test_batch_size, shuffle=False)
 test_pred = prediciton(test_loader, model)
 test_pred_df = pd.DataFrame(np.c_[np.arange(1, len(test_dataset)+1), test_pred.numpy() ],
columns=['ImageId', 'Label'])
 print(test_pred_df.head() )
XTrain, XTest, YTrain, YTest = train_test_split(train_df_j[features], train_df_j[target], test_size = 0.33, random_state = random_seed )<choose_model_class>
def create_download_link(df, title = "Download CSV file", filename = "data.csv"):
csv = df.to_csv(index=False)
 b64 = base64.b64encode(csv.encode())
 payload = b64.decode()
html = '<a download="{filename}" href="data:text/csv;base64,{payload}" target="_blank">{title}</a>'
html = html.format(payload=payload,title=title,filename=filename)
 return HTML(html)
 create_download_link(test_pred_df, filename="submission.csv" )
model = CatBoostRegressor(
 random_state = random_seed,
n_estimators = 1000,
depth = 8,
cat_features = cat_features_encoded,
loss_function = "MAE" )<train_model>
test_pred_df.to_csv('submission.csv', index=False )