Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<count_values>
import pandas as pd
from sklearn.datasets import fetch_mldata
import torch
import numpy as np
import torch.utils.data
import matplotlib.pyplot as plt
from scipy.io import loadmat
import os
import urllib
df_train['StateHoliday'] = df_train['StateHoliday'].apply(lambda x: 1 if x == 'a' else(2 if x == 'b' else(3 if x == 'c' else x)))
 df_train['StateHoliday'].value_counts()<feature_engineering>
test_data = pd.read_csv('.. /input/test.csv')
 mnist_path = "mnist-original.mat"
mnist_alternative_url = "https://github.com/amplab/datascience-sp14/raw/master/lab7/mldata/mnist-original.mat"
response = urllib.request.urlopen(mnist_alternative_url)
 with open(mnist_path, "wb")as f:
content = response.read()
f.write(content)
 mnist_raw = loadmat(mnist_path)
 X = mnist_raw["data"].T
y = mnist_raw["label"][0].astype(np.long)
 X_test = test_data.values
X.shape, X_test.shape
df_train['DayOfYear'] = df_train['Date'].map(lambda x: datetime.datetime.strptime(str(x),'%Y-%m-%d' ).timetuple().tm_yday)
 df_train.head(10 )<data_type_conversions>
def pairwise_distances(x, y):
x_norm =(x**2 ).sum(1 ).view(-1, 1)
 y_t = torch.transpose(y, 0, 1)
 y_norm =(y**2 ).sum(1 ).view(1, -1)
 dist = x_norm + y_norm - 2.0 * torch.mm(x, y_t)
 return torch.clamp(dist, 0.0, np.inf )
df_train['Date'] = pd.to_datetime(df_train['Date'], format = '%Y-%m-%d' )<feature_engineering>
%%time
cuda_test = torch.from_numpy(X_test ).cuda().float()
cuda_train = torch.from_numpy(X ).cuda().float()
ds = torch.utils.data.TensorDataset(cuda_test)
 _min_dists = []
_arg_min_dists = []
bs = 1000
for batch, in torch.utils.data.DataLoader(ds, batch_size=bs, pin_memory=False):
min_dist, arg_min_dist = pairwise_distances(cuda_train, batch ).min(0)
 _min_dists.append(min_dist)
 _arg_min_dists.append(arg_min_dist )
df_train['Year'] = df_train['Date'].map(lambda x: x.year)
 df_train.head()<drop_column>
min_dists = torch.cat(_min_dists)
 arg_min_dists = torch.cat(_arg_min_dists)
 print(f'Number of not found samples: {len(min_dists[min_dists>0])}' )
<prepare_x_and_y><EOS> 
sub = pd.read_csv('.. /input/sample_submission.csv')
 sub.Label = y[arg_min_dists.cpu() ]
sub.to_csv('sub.csv', index=False)
 !head sub.csv