Kaggle competition: AILAB ML Training #0
<SOS> metric: categorizationaccuracy Kaggle data source: ailab-ml-training-#0<concatenate>
%matplotlib inline
all_data = train.append(test, sort=False ).reset_index(drop=True)
 del train, test
gc.collect()<groupby>
def seed_everything(seed=42):
os.environ['PYTHONHASHSEED'] = str(seed)
 random.seed(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
match = all_data.groupby('matchId')
 all_data['killPlacePerc'] = match['kills'].rank(pct=True ).values
all_data['walkDistancePerc'] = match['walkDistance'].rank(pct=True ).values 
<data_type_conversions>
DIRPATH = '.. /input/ailab-ml-training-0/'
TRAIN_IMAGE_DIR = 'train_images/train_images/'
TEST_IMAGE_DIR = 'test_images/test_images/'
ID = 'fname'
TARGET = 'label'
VALID_SIZE = 0.2
EPOCHS = 5
BATCH_SIZE = 64
LR = 1e-3
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
SEED = 42
seed_everything(SEED )
all_data['_totalDistance'] = all_data['rideDistance'] + all_data['walkDistance'] + all_data['swimDistance'] 
<data_type_conversions>
train_df = pd.read_csv(os.path.join(DIRPATH, 'train.csv'))
def fillInf(df, val):
numcols = df.select_dtypes(include='number' ).columns
cols = numcols[numcols != 'winPlacePerc']
df[df == np.Inf] = np.NaN
df[df == np.NINF] = np.NaN
for c in cols: df[c].fillna(val, inplace=True )<feature_engineering>
class TrainDataset(Dataset):
def __init__(self, fname_list, label_list, image_dir, transform=None):
super().__init__()
self.fname_list = fname_list
self.label_list = label_list
self.image_dir = image_dir
self.transform = transform
def __len__(self):
return len(self.fname_list)
 def __getitem__(self, idx):
fname = self.fname_list[idx]
label = self.label_list[idx]
image = cv2.imread(os.path.join(self.image_dir, fname))
if self.transform is not None:
image = self.transform(image)
 return image, label
all_data['_healthItems'] = all_data['heals'] + all_data['boosts']
all_data['_headshotKillRate'] = all_data['headshotKills'] / all_data['kills']
all_data['_killPlaceOverMaxPlace'] = all_data['killPlace'] / all_data['maxPlace']
all_data['_killsOverWalkDistance'] = all_data['kills'] / all_data['walkDistance']
fillInf(all_data, 0 )<drop_column>
class SimpleClassifier(nn.Module):
def __init__(self):
super().__init__()
self.conv_module = nn.Sequential(
 nn.Conv2d(3, 32, 3, stride=1, padding=1, bias=True),
nn.ReLU(True),
nn.MaxPool2d(2),
nn.Conv2d(32, 64, 3, stride=1, padding=1, bias=True),
nn.ReLU(True),
nn.MaxPool2d(2),
nn.Conv2d(64, 128, 3, stride=1, padding=1, bias=True),
nn.ReLU(True), 
)
 self.dense_module = nn.Sequential(
 nn.Linear(128*7*7, 10, bias=True)
)
 def forward(self, x):
x = self.conv_module(x)
 x = x.view(x.size(0), -1)
 x = self.dense_module(x)
 return x
all_data.drop(['boosts','heals','killStreaks','DBNOs'], axis=1, inplace=True)
 all_data.drop(['headshotKills','roadKills','vehicleDestroys'], axis=1, inplace=True)
 all_data.drop(['rideDistance','swimDistance','matchDuration'], axis=1, inplace=True)
 all_data.drop(['rankPoints','killPoints','winPoints'], axis=1, inplace=True )<groupby>
fname_list = train_df[ID].to_list()
label_list = train_df[TARGET].to_list()
train_fname_list, valid_fname_list, train_label_list, valid_label_list = train_test_split(
 fname_list, label_list, test_size=VALID_SIZE, random_state=SEED, shuffle=True
)
match = all_data.groupby(['matchId'])
 group = all_data.groupby(['matchId','groupId','matchType'])
 agg_col = list(all_data.columns)
 exclude_agg_col = ['Id','matchId','groupId','matchType','maxPlace','numGroups','winPlacePerc']
for c in exclude_agg_col:
agg_col.remove(c)
 print(agg_col)
 sum_col = ['kills','killPlace','damageDealt','walkDistance','_healthItems']<categorify>
image_dir = os.path.join(DIRPATH, TRAIN_IMAGE_DIR)
 transform = transforms.Compose([
transforms.ToTensor()
])
 train_dataset = TrainDataset(train_fname_list, train_label_list, image_dir, transform=transform)
 train_dataloader = DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True)
 valid_dataset = TrainDataset(valid_fname_list, valid_label_list, image_dir, transform=transform)
 valid_dataloader = DataLoader(valid_dataset, batch_size=BATCH_SIZE, shuffle=False )
minKills = all_data.sort_values(['matchId','groupId','kills','killPlace'] ).groupby(
 ['matchId','groupId','kills'] ).first().reset_index().copy()
for n in np.arange(4):
c = 'kills_' + str(n)+ '_Place'
nKills =(minKills['kills'] == n)
 minKills.loc[nKills, c] = minKills[nKills].groupby(['matchId'])['killPlace'].rank().values
match_data = pd.merge(match_data, minKills[nKills][['matchId','groupId',c]], how='left')
 match_data = reduce_mem_usage(match_data)
 del minKills, nKills
print(match_data.shape )<data_type_conversions>
model = SimpleClassifier().to(DEVICE)
 optim = Adam(model.parameters() , lr=LR)
 criterion = nn.CrossEntropyLoss()
<merge>
mb = master_bar(range(EPOCHS))
for epoch in mb:
model.train()
train_loss_list = []
train_accuracy_list = []
for batch_image, batch_label in progress_bar(train_dataloader, parent=mb):
batch_image = batch_image.to(dtype=torch.float32, device=DEVICE)
 batch_label = batch_label.to(dtype=torch.long, device=DEVICE)
 optim.zero_grad()
batch_pred = model(batch_image)
 loss = criterion(batch_pred, batch_label)
 loss.backward()
optim.step()
train_loss_list.append(loss.item())
 accuracy = accuracy_score(torch.argmax(batch_pred, axis=1 ).cpu().numpy() , batch_label.cpu().numpy())
 train_accuracy_list.append(accuracy)
 model.eval()
valid_loss_list = []
valid_accuracy_list = []
for batch_image, batch_label in valid_dataloader:
batch_image = batch_image.to(dtype=torch.float32, device=DEVICE)
 batch_label = batch_label.to(dtype=torch.long, device=DEVICE)
 with torch.no_grad() :
batch_pred = model(batch_image)
 loss = criterion(batch_pred, batch_label)
 valid_loss_list.append(loss.item())
 accuracy = accuracy_score(torch.argmax(batch_pred, axis=1 ).cpu().numpy() , batch_label.cpu().numpy())
 valid_accuracy_list.append(accuracy)
 mb.write('epoch: {}/{} - loss: {:.5f} - accuracy: {:.3f} - val_loss: {:.5f} - val_accuracy: {:.3f}'.format(
 epoch,
EPOCHS,
np.mean(train_loss_list),
np.mean(train_accuracy_list),
np.mean(valid_loss_list),
np.mean(valid_accuracy_list)
))
all_data = pd.merge(all_data, match_data)
 del match_data
gc.collect()
all_data['enemy.players'] = all_data['m.players'] - all_data['players']
for c in sum_col:
all_data['p.max_msum.' + c] = all_data['max.' + c] / all_data['m.sum.' + c]
all_data['p.max_mmax.' + c] = all_data['max.' + c] / all_data['m.max.' + c]
all_data.drop(['m.sum.' + c, 'm.max.' + c], axis=1, inplace=True)
 fillInf(all_data, 0)
 print(all_data.shape )<feature_engineering>
submission_df = pd.read_csv(os.path.join(DIRPATH, 'sample_submission.csv'))
match = all_data.groupby('matchId')
 matchRank = match[numcols].rank(pct=True ).rename(columns=lambda s: 'rank.' + s)
 all_data = reduce_mem_usage(pd.concat([all_data, matchRank], axis=1))
rank_col = matchRank.columns
del matchRank
gc.collect()
match = all_data.groupby('matchId')
 matchRank = match[rank_col].max().rename(columns=lambda s: 'max.' + s ).reset_index()
all_data = pd.merge(all_data, matchRank)
 for c in numcols:
all_data['rank.' + c] = all_data['rank.' + c] / all_data['max.rank.' + c]
all_data.drop(['max.rank.' + c], axis=1, inplace=True)
 del matchRank
gc.collect()
print(all_data.shape )<merge>
fname_list = submission_df[ID].to_list()
label_list = submission_df[TARGET].to_list()
image_dir = os.path.join(DIRPATH, TEST_IMAGE_DIR)
 transform = transforms.Compose([transforms.ToTensor() ])
 test_dataset = TrainDataset(fname_list, label_list, image_dir, transform=transform)
 test_dataloader = DataLoader(test_dataset, batch_size=1, shuffle=False )
killMinorRank = all_data[['matchId','min.kills','max.killPlace']].copy()
group = killMinorRank.groupby(['matchId','min.kills'])
 killMinorRank['rank.minor.maxKillPlace'] = group.rank(pct=True ).values
all_data = pd.merge(all_data, killMinorRank)
 killMinorRank = all_data[['matchId','max.kills','min.killPlace']].copy()
group = killMinorRank.groupby(['matchId','max.kills'])
 killMinorRank['rank.minor.minKillPlace'] = group.rank(pct=True ).values
all_data = pd.merge(all_data, killMinorRank)
 del killMinorRank
gc.collect()<count_unique_values>
submission_df[TARGET] = predictions
constant_column = [col for col in all_data.columns if all_data[col].nunique() == 1]
print('drop columns:', constant_column)
 all_data.drop(constant_column, axis=1, inplace=True )<feature_engineering>
submission_df.to_csv('submission.csv', index=False )
<sort_values><EOS> 
FileLink('submission.csv' )