Kaggle competition: Digit Recognizer
object_columns_df = object_columns_df.drop(['Heating','RoofMatl','Condition2','Street','Utilities'],axis=1)
<filter>
class Dataset(Dataset):
def __init__(self, path, transform=None):
self.data = pd.read_csv(path)
 self.transform = transform
def __len__(self):
return len(self.data)
 def __getitem__(self, index):
item = self.data.iloc[index]
image = item[1:].values.astype(np.uint8 ).reshape(( 28, 28))
label = item[0]
if self.transform is not None:
image = self.transform(image)
 return image, label
Negatif = numerical_columns_df[numerical_columns_df['Age_House'] < 0]
Negatif<feature_engineering>
path = '.. /input/train.csv'
VALID_SIZE = 0.2
train_transform = transforms.Compose([
transforms.ToPILImage() ,
transforms.ToTensor() ,
transforms.Normalize(mean=(0.5,), std=(0.5,))
])
 valid_transform = transforms.Compose([
transforms.ToPILImage() ,
transforms.ToTensor() ,
transforms.Normalize(mean=(0.5,), std=(0.5,))
])
 train_data = Dataset(path, train_transform)
 valid_data = Dataset(path, valid_transform)
 trainloader = DataLoader(train_data, batch_size = 1500, shuffle = True)
 testloader = DataLoader(valid_data, batch_size = 1500, shuffle = False)
 len(trainloader )
numerical_columns_df['TotalBsmtBath'] = numerical_columns_df['BsmtFullBath'] + numerical_columns_df['BsmtFullBath']*0.5
numerical_columns_df['TotalBath'] = numerical_columns_df['FullBath'] + numerical_columns_df['HalfBath']*0.5
numerical_columns_df['TotalSA']=numerical_columns_df['TotalBsmtSF'] + numerical_columns_df['1stFlrSF'] + numerical_columns_df['2ndFlrSF']<categorify>
class Net(nn.Module):
def __init__(self):
super(Net, self ).__init__()
self.conv1 = nn.Sequential(
 nn.Conv2d(1, 32, 3, padding=1),
mila() ,
nn.BatchNorm2d(32),
nn.Conv2d(32, 32, 3, stride=2, padding=1),
mila() ,
nn.BatchNorm2d(32),
nn.MaxPool2d(2, 2),
nn.Dropout(0.25)
)
 self.conv2 = nn.Sequential(
 nn.Conv2d(32, 64, 3, padding=1),
mila() ,
nn.BatchNorm2d(64),
nn.Conv2d(64, 64, 3, stride=2, padding=1),
mila() ,
nn.BatchNorm2d(64),
nn.Dropout(0.25)
)
 self.conv3 = nn.Sequential(
 nn.Conv2d(64, 128, 3, padding=1),
mila() ,
nn.BatchNorm2d(128),
nn.Conv2d(128, 128, 3, stride=2, padding=1),
mila() ,
nn.BatchNorm2d(128),
nn.Dropout(0.25)
)
 self.conv4 = nn.Sequential(
 nn.Conv2d(128, 256, 3, padding=1),
mila() ,
nn.BatchNorm2d(256),
nn.MaxPool2d(2, 2),
nn.Dropout(0.25)
)
 self.fc = nn.Sequential(
 nn.Linear(256, 256),
nn.Dropout(0.3),
mila() ,
nn.Linear(256, 256),
nn.Dropout(0.4),
mila() ,
nn.Linear(256, 10),
nn.Softmax(dim=1)
)
 def forward(self, x):
x = self.conv1(x)
 x = self.conv2(x)
 x = self.conv3(x)
 x = self.conv4(x)
 x = x.view(x.size(0), -1)
 return self.fc(x )
bin_map = {'TA':2,'Gd':3, 'Fa':1,'Ex':4,'Po':1,'None':0,'Y':1,'N':0,'Reg':3,'IR1':2,'IR2':1,'IR3':0,"None" : 0,
"No" : 2, "Mn" : 2, "Av": 3,"Gd" : 4,"Unf" : 1, "LwQ": 2, "Rec" : 3,"BLQ" : 4, "ALQ" : 5, "GLQ" : 6
}
object_columns_df['ExterQual'] = object_columns_df['ExterQual'].map(bin_map)
 object_columns_df['ExterCond'] = object_columns_df['ExterCond'].map(bin_map)
 object_columns_df['BsmtCond'] = object_columns_df['BsmtCond'].map(bin_map)
 object_columns_df['BsmtQual'] = object_columns_df['BsmtQual'].map(bin_map)
 object_columns_df['HeatingQC'] = object_columns_df['HeatingQC'].map(bin_map)
 object_columns_df['KitchenQual'] = object_columns_df['KitchenQual'].map(bin_map)
 object_columns_df['FireplaceQu'] = object_columns_df['FireplaceQu'].map(bin_map)
 object_columns_df['GarageQual'] = object_columns_df['GarageQual'].map(bin_map)
 object_columns_df['GarageCond'] = object_columns_df['GarageCond'].map(bin_map)
 object_columns_df['CentralAir'] = object_columns_df['CentralAir'].map(bin_map)
 object_columns_df['LotShape'] = object_columns_df['LotShape'].map(bin_map)
 object_columns_df['BsmtExposure'] = object_columns_df['BsmtExposure'].map(bin_map)
 object_columns_df['BsmtFinType1'] = object_columns_df['BsmtFinType1'].map(bin_map)
 object_columns_df['BsmtFinType2'] = object_columns_df['BsmtFinType2'].map(bin_map)
 PavedDrive = {"N" : 0, "P" : 1, "Y" : 2}
object_columns_df['PavedDrive'] = object_columns_df['PavedDrive'].map(PavedDrive)
<categorify>
!wget https://raw.githubusercontent.com/LiyuanLucasLiu/RAdam/master/cifar_imagenet/utils/radam.py
rest_object_columns = object_columns_df.select_dtypes(include=['object'])
 object_columns_df = pd.get_dummies(object_columns_df, columns=rest_object_columns.columns )<concatenate>
model = Net()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 model.to(device)
 criterion = nn.CrossEntropyLoss()
optimizer = radam.RAdam(model.parameters() , lr=0.00159 )
df_final = pd.concat([object_columns_df, numerical_columns_df], axis=1,sort=False)
 df_final.head()<drop_column>
total_epoch = 50
df_final = df_final.drop(['Id',],axis=1)
 df_train = df_final[df_final['train'] == 1]
df_train = df_train.drop(['train',],axis=1)
 df_test = df_final[df_final['train'] == 0]
df_test = df_test.drop(['SalePrice'],axis=1)
 df_test = df_test.drop(['train',],axis=1 )<prepare_x_and_y>
n_epochs = total_epoch
train_loss_data,valid_loss_data = [],[]
valid_loss_min = np.Inf
class_correct = list(0.for i in range(10))
class_total = list(0.for i in range(10))
for epoch in range(n_epochs):
train_loss = 0.0
valid_loss = 0.0
model.train()
for data, target in trainloader:
data, target = data.to(device), target.to(device)
 optimizer.zero_grad()
output = model(data)
 loss = criterion(output, target)
 loss.backward()
optimizer.step()
train_loss += loss.item()
model.eval()
for data, target in testloader:
data, target = data.to(device), target.to(device)
 output = model(data)
 loss = criterion(output, target)
 valid_loss += loss.item()
_, pred = torch.max(output, 1)
 correct = np.squeeze(pred.eq(target.data.view_as(pred)))
 for i in range(16):
label = target.data[i]
class_correct[label] += correct[i].item()
class_total[label] += 1
train_loss = train_loss/len(trainloader.dataset)
 valid_loss = valid_loss/len(testloader.dataset)
 train_loss_data.append(train_loss)
 valid_loss_data.append(valid_loss)
 print('Epoch: {} \tTraining Loss: {:.6f} \tValidation Loss: {:.6f}'.format(
 epoch+1,
train_loss,
valid_loss 
))
print('\t\tTest Accuracy: %4d%%(%2d/%2d)' %(
 100.* np.sum(class_correct)/ np.sum(class_total),
np.sum(class_correct), np.sum(class_total)))
 if valid_loss <= valid_loss_min:
print('\t\tValidation loss decreased({:.6f} --> {:.6f} ).Saving model...'.format(
 valid_loss_min,
valid_loss))
torch.save(model.state_dict() , 'model.pt')
 valid_loss_min = valid_loss
target= df_train['SalePrice']
df_train = df_train.drop(['SalePrice'],axis=1 )<split>
model.load_state_dict(torch.load('model.pt'))
x_train,x_test,y_train,y_test = train_test_split(df_train,target,test_size=0.33,random_state=0 )<choose_model_class>
classes = ['0', '1', '2', '3', '4',
'5', '6', '7', '8', '9']
test_loss = 0.0
class_correct = list(0.for i in range(10))
class_total = list(0.for i in range(10))
with torch.no_grad() :
model.eval()
for data, target in testloader:
data, target = data.to(device), target.to(device)
 output = model(data)
 loss = criterion(output, target)
 test_loss += loss.item() *data.size(0)
 _, pred = torch.max(output, 1)
 correct = np.squeeze(pred.eq(target.data.view_as(pred)))
 for i in range(16):
label = target.data[i]
class_correct[label] += correct[i].item()
class_total[label] += 1
test_loss = test_loss/len(testloader.dataset)
 print('Test Loss: {:.6f}
'.format(test_loss))
for i in range(10):
if class_total[i] > 0:
print('Test Accuracy of %5s: %2d%%(%2d/%2d)' %(
 classes[i], 100 * class_correct[i] / class_total[i],
np.sum(class_correct[i]), np.sum(class_total[i])))
 else:
print('Test Accuracy of %5s: N/A(no training examples)' %(classes[i]))
print('
Test Accuracy(Overall): %2d%%(%2d/%2d)' %(
 100.* np.sum(class_correct)/ np.sum(class_total),
np.sum(class_correct), np.sum(class_total)) )
xgb =XGBRegressor(booster='gbtree', colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=0.6, gamma=0,
importance_type='gain', learning_rate=0.01, max_delta_step=0,
max_depth=4, min_child_weight=1.5, n_estimators=2400,
n_jobs=1, nthread=None, objective='reg:linear',
reg_alpha=0.6, reg_lambda=0.6, scale_pos_weight=1,
silent=None, subsample=0.8, verbosity=1)
 lgbm = LGBMRegressor(objective='regression',
num_leaves=4,
learning_rate=0.01,
n_estimators=12000,
max_bin=200,
bagging_fraction=0.75,
bagging_freq=5,
bagging_seed=7,
feature_fraction=0.4,
)<train_model>
class DatasetSubmissionMNIST(torch.utils.data.Dataset):
def __init__(self, file_path, transform=None):
self.data = pd.read_csv(file_path)
 self.transform = transform
def __len__(self):
return len(self.data)
 def __getitem__(self, index):
image = self.data.iloc[index].values.astype(np.uint8 ).reshape(( 28, 28, 1))
if self.transform is not None:
image = self.transform(image)
 return image
xgb.fit(x_train, y_train)
 lgbm.fit(x_train, y_train,eval_metric='rmse')
<predict_on_test>
transform = transforms.Compose([
transforms.ToPILImage() ,
transforms.ToTensor() ,
transforms.Normalize(mean=(0.5,), std=(0.5,))
])
 submissionset = DatasetSubmissionMNIST('.. /input/test.csv', transform=transform)
 submissionloader = torch.utils.data.DataLoader(submissionset, batch_size=128, shuffle=False )
predict1 = xgb.predict(x_test)
 predict = lgbm.predict(x_test )<compute_test_metric>
submission = [['ImageId', 'Label']]
with torch.no_grad() :
model.eval()
image_id = 1
for images in submissionloader:
images = images.to(device)
 log_ps = model(images)
 ps = torch.exp(log_ps)
 top_p, top_class = ps.topk(1, dim=1)
 for prediction in top_class:
submission.append([image_id, prediction.item() ])
 image_id += 1
print(len(submission)- 1 )
<train_model><EOS> 
with open('submission.csv', 'w')as submissionFile:
writer = csv.writer(submissionFile)
 writer.writerows(submission)
 print('Submission Complete!' )