Kaggle competition: Digit Recognizer
y_test = model.predict(test)
 submission['SalePrice'] = y_test
submission.to_csv('./HousePrice_submission.csv', index=False )<import_modules>
%matplotlib inline
import numpy as np
import pandas as pd
import seaborn as sns
import sklearn
from sklearn import metrics
import tensorflow as tf
from tensorflow import keras
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split, StratifiedKFold
import numpy as np
import keras_tuner as kt
from tensorflow import keras
import matplotlib.pyplot as plt<load_from_csv>
train_df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 test_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')
 submit_df = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv' )
train = pd.read_csv("/kaggle/input/house-prices-advanced-regression-techniques/train.csv")
 test = pd.read_csv("/kaggle/input/house-prices-advanced-regression-techniques/test.csv")
<compute_test_metric>
num_pixel = len(train_df.columns)- 1
num_pixel
def evaluate(model, x_val, y_val):
y_pred = model.predict(x_val ).reshape(y_val.shape)
 r2 = metrics.r2_score(y_val, y_pred)
 mse = metrics.mean_squared_error(y_val, y_pred)
 mae = metrics.mean_absolute_error(y_val, y_pred)
 msle = metrics.mean_squared_log_error(y_val, y_pred)
 mape = np.mean(keras.metrics.mean_absolute_percentage_error(y_val, y_pred ).numpy())
 rmse = np.sqrt(mse)
 rmlse_score = rmlse(y_val, y_pred ).numpy()
print("R2 Score:", r2)
 print("MSE:", mse)
 print("MAE:", mae)
 print("MSLE:", msle)
 print("MAPE", mape)
 print("RMSE:", rmse)
 print("RMLSE", rmlse_score)
 return {"r2": r2, "mse": mse, "mae": mae, "msle": msle, "mape": mape, "rmse": rmse, "rmlse": rmlse_score}<compute_test_metric>
transform_0 = transforms.Compose([
transforms.ToPILImage() ,
transforms.ToTensor() ,
transforms.Normalize([0.5], [0.5])
 ])
 transform_1 = transforms.Compose([
transforms.ToPILImage() ,
transforms.RandomRotation(30),
transforms.ToTensor() ,
transforms.Normalize([0.5], [0.5])
 ])
 transform_2 = transforms.Compose([
transforms.ToPILImage() ,
transforms.RandomAffine(degrees=15, translate=(0.1,0.1), scale=(0.8,0.8)) ,
transforms.ToTensor() ,
transforms.Normalize([0.5], [0.5])
 ])
 transform_3 = transforms.Compose([
transforms.ToPILImage() ,
transforms.RandomAffine(degrees=30, scale=(1.1,1.1)) ,
transforms.ToTensor() ,
transforms.Normalize([0.5], [0.5])
 ])
 transform_4 = transforms.Compose([
transforms.ToPILImage() ,
transforms.RandomAffine(degrees=30, translate=(0.1,0.1)) ,
transforms.ToTensor() ,
transforms.Normalize([0.5], [0.5])
 ])
 transform_5 = transforms.Compose([
transforms.ToPILImage() ,
transforms.RandomAffine(degrees=10, shear=45),
transforms.ToTensor() ,
transforms.Normalize([0.5], [0.5])
 ] )
def rmlse(y_true, y_pred):
return tf.sqrt(tf.reduce_mean(tf.square(tf.math.log(y_pred + 1)- tf.math.log(y_true + 1))))<save_to_csv>
class DataFrame_to_Dataset(Dataset):
def __init__(self, df, transform=transform_0):
if len(df.columns)== num_pixel:
self.features = df.values.reshape(( -1,28,28)).astype(np.uint8)
 self.labels = None
else:
self.features = df.iloc[:,1:].values.reshape(( -1,28,28)).astype(np.uint8)
 self.labels = torch.from_numpy(df.label.values)
 self.transform = transform
def __len__(self):
return len(self.features)
 def __getitem__(self, index):
if self.labels is not None:
return self.transform(self.features[index]), self.labels[index]
else:
return self.transform(self.features[index] )
def submit(model, X, ids, file_path):
SalePrice = model.predict(X)
 submission = pd.DataFrame({"Id": ids, "SalePrice": SalePrice.reshape(-1)})
 submission.to_csv(file_path, index=False )<sort_values>
def create_dataloaders(seed, test_size=0.1, df=train_df, batch_size=32):
train_data, valid_data = train_test_split(df,
test_size=test_size,
random_state=seed)
 train_dataset_0 = DataFrame_to_Dataset(train_data)
 train_dataset_1 = DataFrame_to_Dataset(train_data, transform_1)
 train_dataset_2 = DataFrame_to_Dataset(train_data, transform_2)
 train_dataset_3 = DataFrame_to_Dataset(train_data, transform_3)
 train_dataset_4 = DataFrame_to_Dataset(train_data, transform_4)
 train_dataset_5 = DataFrame_to_Dataset(train_data, transform_5)
 train_dataset = ConcatDataset([train_dataset_0, train_dataset_1, train_dataset_2, train_dataset_3, train_dataset_4, train_dataset_5])
 valid_dataset = DataFrame_to_Dataset(valid_data)
 train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
 valid_loader = torch.utils.data.DataLoader(valid_dataset, batch_size=batch_size, shuffle=False)
 return train_loader, valid_loader
train.corr() ["SalePrice"].sort_values(key = lambda x: abs(x), ascending=False )<feature_engineering>
class Net(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Sequential(
 nn.Conv2d(1, 32, kernel_size=3),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
nn.Conv2d(32, 32, kernel_size=3),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
nn.Conv2d(32, 32, kernel_size=5, stride=2, padding=14),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
nn.MaxPool2d(2, 2),
nn.Dropout2d(0.25)
)
 self.conv2 = nn.Sequential(
 nn.Conv2d(32, 64, kernel_size=3),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.Conv2d(64, 64, kernel_size=3),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.Conv2d(64, 64, kernel_size=5, stride=2, padding=6),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(2, 2),
nn.Dropout2d(0.25)
)
 self.conv3 = nn.Sequential(
 nn.Conv2d(64, 128, kernel_size=4),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True),
nn.Dropout2d(0.25)
)
 self.fc = nn.Sequential(
 nn.Linear(128*1*1, 10)
)
 def forward(self, x):
x = self.conv1(x)
 x = self.conv2(x)
 x = self.conv3(x)
 x = x.view(-1, 128*1*1)
 x = self.fc(x)
 return x
for data in [train, test]:
null_counts = data.isnull().sum()
null_counts[null_counts > 0]
null_columns = list(pd.DataFrame(null_counts[null_counts > 0] ).index)
 for column in null_columns:
if data[column].dtype == object:
data[column] = data[[column]].replace(np.NAN, "Unknown")
 else:
data[column] = data[column].replace(np.NAN, data[column].mean() )<categorify>
use_cuda = torch.cuda.is_available()
print(use_cuda )
train_test = pd.get_dummies(pd.concat([train, test]))<drop_column>
def train(seed, num_epochs):
print('Creating new dataloaders...')
 train_loader, valid_loader = create_dataloaders(seed=seed)
 print('Creating a new model...')
 net = Net()
criterion = nn.CrossEntropyLoss()
if use_cuda:
net.cuda()
criterion.cuda()
optimizer = optim.Adam(net.parameters() ,
lr=0.003, betas=(0.9, 0.999),
eps=1e-08, weight_decay=0,
amsgrad=False)
 scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=3, gamma=0.1)
 print('Training the model...')
 for epoch in range(num_epochs):
net.train()
t0 = time.time()
training_loss = 0.0
num_samples = 0
for features, labels in train_loader:
if use_cuda:
features = features.cuda()
labels = labels.cuda()
optimizer.zero_grad()
outputs = net(features)
 loss = criterion(outputs, labels)
 loss.backward()
optimizer.step()
training_loss += loss.item()
num_samples += len(features)
 net.eval()
correct = 0
total = 0
with torch.no_grad() :
for valid_features, valid_labels in valid_loader:
if use_cuda:
valid_features = valid_features.cuda()
valid_labels = valid_labels.cuda()
outputs = net(valid_features)
 _, predicted = torch.max(outputs, 1)
 total += valid_labels.size(0)
 correct +=(predicted == valid_labels ).sum().item()
scheduler.step()
print('[model %d, epoch %d, time: %.3f seconds] train_loss: %.5f, val_acc: %4f %%' % 
(seed + 1, epoch + 1, time.time() - t0, training_loss/num_samples, 100 * correct / total))
net.eval()
test_pred = torch.LongTensor()
if use_cuda:
test_pred = test_pred.cuda()
with torch.no_grad() :
for features in test_loader:
if use_cuda:
features = features.cuda()
outputs = net(features)
 _, predicted = torch.max(outputs, 1)
 test_pred = torch.cat(( test_pred, predicted), dim=0)
 model_name = 'model_' + str(seed + 1)
 ensemble_df[model_name] = test_pred.cpu().numpy()
print('Prediction Saved!
' )
train_features = train_test.iloc[0: len(train)]
test_features = train_test.iloc[len(train):]
_ = train_features.pop("Id")
 _ = test_features.pop("SalePrice")
 test_ids = test_features.pop("Id" )<split>
test_dataset = DataFrame_to_Dataset(test_df)
 test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=32, shuffle=False)
 ensemble_df = submit_df.copy()
num_models = 11
num_epochs = 6
for seed in range(num_models):
train(seed, num_epochs )
train_features, val_features = train_test_split(train_features, test_size=0.2, random_state=np.random.randint(1000))<sort_values>
final_pred = ensemble_df.iloc[:,2:].mode(axis=1 ).iloc[:,0]
submit_df.Label = final_pred.astype(int)
 submit_df.head()
<drop_column><EOS> 
submit_df.to_csv('submission.csv', index=False )