Kaggle competition: Microsoft Malware Prediction
plt.imshow(dataset.iloc[0][1:].values.astype(np.uint8 ).reshape(( 28, 28)) , cmap='binary')
 plt.title(dataset.iloc[0][0])
 plt.show()<categorify>
from scipy.stats import rankdata
class DatasetMNIST(Dataset):
def __init__(self, data, transform=None):
self.data = data
self.transform = transform
def __len__(self):
return len(self.data)
 def __getitem__(self, index):
data = self.data.iloc[index]
image = data[1:].values.astype(np.uint8 ).reshape(( 28, 28))
label = data[0]
if self.transform is not None:
image = self.transform(image)
 return image, label<categorify>
sub_files = [
".. /input/new-blend/ens_sub_v2.csv",
".. /input/new-blend/sub.csv",
".. /input/new-blend/super_blend.csv",
".. /input/newsub/submission.csv",
]
BATCH_SIZE = 16
VALID_SIZE = 0.2
transform_train = transforms.Compose([
transforms.ToPILImage() ,
transforms.RandomRotation(20),
transforms.ToTensor() ,
transforms.Normalize(mean=(0.5,), std=(0.5,))
])
 transform_valid = transforms.Compose([
transforms.ToPILImage() ,
transforms.ToTensor() ,
transforms.Normalize(mean=(0.5,), std=(0.5,))
] )<create_dataframe>
df1 = pd.read_csv(sub_files[0])
 df2 = pd.read_csv(sub_files[1])
 df3 = pd.read_csv(sub_files[2])
 df4 = pd.read_csv(sub_files[3] )
train_data = DatasetMNIST(dataset, transform=transform_train)
 valid_data = DatasetMNIST(dataset, transform=transform_valid )<load_pretrained>
ens_sub = pd.read_csv(".. /input/microsoft-malware-prediction/sample_submission.csv" )
num_train = len(train_data)
 indices = list(range(num_train))
np.random.shuffle(indices)
 split = int(np.floor(VALID_SIZE * num_train))
train_idx, valid_idx = indices[split:], indices[:split]
print("Size of Train Set: ", len(train_idx))
print("Size of Valid Set: ", len(valid_idx))
train_sampler = SubsetRandomSampler(train_idx)
 valid_sampler = SubsetRandomSampler(valid_idx)
 train_loader = DataLoader(train_data, batch_size=BATCH_SIZE, sampler=train_sampler)
 valid_loader = DataLoader(valid_data, batch_size=BATCH_SIZE, sampler=valid_sampler )<choose_model_class>
ens_pred = rankdata(ens_pred, "dense" )
class CNN_model(nn.Module):
def __init__(self):
super(CNN_model, self ).__init__()
self.conv1 = nn.Sequential(
 nn.Conv2d(1, 16, 3, padding=1),
nn.BatchNorm2d(16),
nn.ReLU() ,
nn.Conv2d(16, 16, 3, padding=1),
nn.BatchNorm2d(16),
nn.ReLU() ,
nn.MaxPool2d(2,2)
)
 self.conv2 = nn.Sequential(
 nn.Conv2d(16, 32, 3, padding=1),
nn.BatchNorm2d(32),
nn.ReLU() ,
nn.Conv2d(32, 32, 3, padding=1),
nn.BatchNorm2d(32),
nn.ReLU() ,
nn.MaxPool2d(2,2)
)
 self.conv3 = nn.Sequential(
 nn.Conv2d(32, 64, 3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU() ,
nn.Conv2d(64, 64, 3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU() 
)
 self.fc = nn.Linear(64*7*7, 10)
 self.bn_fc = nn.BatchNorm1d(10)
 def forward(self, x):
out = self.conv1(x)
 out = self.conv2(out)
 out = self.conv3(out)
 out = out.view(out.size(0), -1)
 out = self.fc(out)
 out = self.bn_fc(out)
 return out
model = CNN_model()
model<train_model>
ens_sub['HasDetections'] = ens_pred
if train_on_gpu:
model = model.cuda()
print("Train on GPU")
 else:
print("Train on CPU" )<choose_model_class>
ens_sub.HasDetections -= ens_sub.HasDetections.min() /2
<define_variables><EOS> 
ens_sub.to_csv("ens_sub_v16.csv", index=False )