Kaggle competition: Titanic - Machine Learning from Disaster
class MPLoader:
cache = dict()
cache_vmem_percent = 45.0
data_type = 'msp'
use_augmentation = True
max_of_augid = 2
def reset() :
MPLoader.cache = dict()
def get(fname, augmentation=False, cache=False):
if fname in MPLoader.cache:
data = MPLoader.cache[fname]
else:
data = MPLoader.load(fname, cache)
 if augmentation and MPLoader.use_augmentation:
return random.choice(data)
 else:
return data[0]
def load(fname, cache=False):
data_raw = np.load(( PREPROCESSED_MP/fname ).with_suffix('.wav.npz'))
data = list()
if MPLoader.use_augmentation:
for i in range(1, MPLoader.max_of_augid+1):
tid = f'{MPLoader.data_type}{i}'
if tid in data_raw:
data.append(MPLoader._mp2tensor(data_raw[tid]))
else:
break
else:
tid = f'{MPLoader.data_type}1'
data.append(MPLoader._mp2tensor(data_raw[tid]))
if cache: MPLoader.cache[fname] = data
return data
def load_and_process(fname, cache=False):
pcm, _ = load_audio(DATA/fname)
 mels = preprocess(pcm)
 data = [MPLoader._mp2tensor(mels)]
if cache: MPLoader.cache[fname] = data
return data
def _mp2tensor(mp):
mp = torch.FloatTensor(mp ).div_(255)
 return mp
def full_load(df, use_preprocess=True):
vmem_limit = MPLoader.cache_vmem_percent
virt = psutil.virtual_memory()
if virt.percent >= vmem_limit:
vmem_limit = virt.percent + 0.1
for i, row in tqdm_notebook(df.iterrows() , total=len(df)) :
if use_preprocess:
MPLoader.load(row.fname, cache=True)
 else:
MPLoader.load_and_process(row.fname, cache=True)
 if i % 10 == 0:
virt = psutil.virtual_memory()
if virt.percent >= vmem_limit:
mb = 1024 * 1024
print(f'Stopped loading preprocessed data as cache, '
f'cause memory usage reached {vmem_limit}%.')
 print('Memory usage:', virt.available>>20, '/', virt.total>>20, 'MB')
 break
elif i % 1000 == 0:
print(virt.percent, '%', virt.available>>20, 'MB')
 print('Completed loading preprocessed data.')
 if TRAIN_MODE:
MPLoader.full_load(df_train)
 if MIXMATCH_SSL > 0:
MPLoader.full_load(df_noisy )<normalization>
trainSet = pd.read_csv('/kaggle/input/titanic/train.csv')
 testSet = pd.read_csv('/kaggle/input/titanic/test.csv')
 y = trainSet['Survived']
trainSet1 = trainSet.copy()
testSet1 = testSet.copy()
combineData = list([trainSet1, testSet1])
 print(1 )
TIME_DIM = 128
def open_fat2019_image(fn, convert_mode, after_open)->Image:
fname = '/'.join(fn.split('/')[-2:])
 x = MPLoader.get(fname, augmentation=True)
 base_dim, time_dim = x.shape
if time_dim < TIME_DIM:
x2 = torch.zeros(( base_dim,TIME_DIM), dtype=x.dtype)
 crop = random.randint(0, TIME_DIM - time_dim)
 x2[:, crop:crop+time_dim] = x
x = x2
else:
crop = random.randint(0, time_dim - TIME_DIM)
 x = x[:, crop:crop+TIME_DIM]
x = torch.stack(( x,x,x), dim=0)
 if USE_MASK_FREQ: freq_mask(
 x,
num=random.randrange(MASK_FREQ_MAX_COUNT),
mask_size=MASK_FREQ_RANGE,
mask_value=0,
inplace=True, 
)
 if USE_MASK_TIME: time_mask(
 x,
num=random.randrange(MASK_TIME_MAX_COUNT),
mask_size=MASK_TIME_RANGE,
mask_value=0,
inplace=True, 
)
 return Image(x)
 vision.data.open_image = open_fat2019_image<load_from_csv>
for data in combineData:
data.drop(columns = ['PassengerId', 'Name', 'Ticket', 'Fare', 'Cabin'], inplace = True)
 data['Sex'] = data['Sex'].map({'male':0, 'female':1})
 def age(x):
if 0<x<=12.0 :
return 1
elif 12.0<x<=18.0 :
return 2
elif 18.0<x<=40.0 :
return 3
elif 40.0<x<=60.0 :
return 4
elif 60.0<x :
return 5
else:
return 0
trainSet1['Embarked'] = trainSet1['Embarked'].fillna('S')
 for i in combineData:
i['Age'] = i['Age'].map(age)
 i['Embarked'] = i['Embarked'].map({'C':0, 'Q':1, 'S':2})
 def SibSpAndParch(x):
if x == 0 :
return 0
if x == 1 :
return 1
if x > 1 :
return 2
for i in combineData:
i['farmily'] = i['SibSp'] + i['Parch']
i.drop(columns = ['SibSp', 'Parch'],inplace = True)
 i['farmily'] = i['farmily'].map(SibSpAndParch)
 print(1 )
BATCH_SIZE = 48
tfms = get_transforms(do_flip=True, max_rotate=0, max_lighting=0.1, max_zoom=0, max_warp=0.)
src =(ImageList.from_df(df_train, WORK, folder='')
.split_none() 
.label_from_df(label_delim=',')
)
 data =(src.transform(tfms, size=128)
.databunch(bs=BATCH_SIZE ).normalize(imagenet_stats)
)
 if MIXMATCH_SSL > 0:
noisy_src =(ImageList.from_df(df_noisy, WORK, folder='')
.split_none() 
.label_from_df(label_delim=',')
)
 noisy_data =(noisy_src.transform(tfms, size=128)
.databunch(bs=BATCH_SIZE ).normalize(imagenet_stats)
 )<define_variables>
x = trainSet1.drop(columns = ['Survived'])
 y = trainSet1['Survived']
x.head()
if TRAIN_MODE: data.show_batch(3 )<compute_test_metric>
train_x,test_x,train_y,test_y = train_test_split(x, y, test_size = 0.2, random_state = 1 )
def lwlrap(y_pred,y_true):
score, weight = calculate_per_class_lwlrap(y_true.cpu().numpy() , y_pred.cpu().numpy())
 lwlrap =(score * weight ).sum()
return torch.from_numpy(np.array(lwlrap))<import_modules>
cv_scores = []
maxDepths = [i for i in range(2,10)]
for maxDepth in maxDepths:
model = DecisionTreeClassifier(max_depth=maxDepth)
 scores = cross_val_score(model, train_x, train_y, cv = 5)
 cv_score = scores.mean()
print('maxDepth={}，score={:.3f}'.format(maxDepth, cv_score))
cv_scores.append(cv_score )
class MixMatchCallback(LearnerCallback):
def __init__(self, learn:Learner,
unlabeled_dl:DeviceDataLoader,
temperature:float=0.5, n_augment:int=2,
alpha:float=0.75, lambda_u:float=100, rampup:int=16):
super().__init__(learn)
 self.unlabeled_dl = unlabeled_dl
self.T = temperature
self.K = n_augment
self.beta_distirb = torch.distributions.beta.Beta(alpha, alpha)
 self.lambda_u = lambda_u
self.rampup = rampup
self.n_iterations = len(learn.data.train_dl)
 def on_train_begin(self, **kwargs):
self.unlabeled_iter = iter(self.unlabeled_dl)
 self.learn.loss_func = MultiSemiLoss(self.learn.loss_func, lambda_u=self.lambda_u, rampup=self.rampup)
 def on_train_end(self, **kwargs):
self.unlabeled_iter = None
self.learn.loss_func = self.learn.loss_func.get_old()
def unlabeled_next_batch(self):
try:
return next(self.unlabeled_iter)
 except:
self.unlabeled_iter = iter(self.unlabeled_dl)
 return next(self.unlabeled_iter)
 def sharpen(self, y):
y = y.pow(1 / self.T)
 return y / y.sum(dim=1, keepdim=True)
 def on_batch_begin(self, epoch, iteration, last_input, last_target, train, **kwargs):
if not train: return
with torch.no_grad() :
bs = len(last_input)
 u_x_hat = [self.unlabeled_next_batch() [0] for _ in range(self.K)]
y_hat = sum([self.learn.model(u_x_hat[i] ).sigmoid_() for i in range(self.K)])/ self.K
y_hat = self.sharpen(y_hat)
 y_hat = y_hat.repeat(self.K, 1)
 u_x_hat = torch.cat([last_input] + u_x_hat, dim=0)
 y_hat = torch.cat(( last_target, y_hat), dim=0)
 index = torch.randperm(u_x_hat.shape[0])
 lam = self.beta_distirb.sample().item()
lam = max(lam, 1-lam)
 mixed_input = lam * u_x_hat +(1-lam)* u_x_hat[index]
mixed_input = self.interleave(torch.split(mixed_input, bs), bs)
 mixed_target = lam * y_hat +(1-lam)* y_hat[index]
loss_func = self.learn.loss_func
loss_func.labeled_bs = bs
loss_func.epoch = iteration / self.n_iterations
return {'last_input': mixed_input, 'last_target': mixed_target}
def on_loss_begin(self, last_input, last_output, iteration, **kwargs):
bs = len(last_input)
 last_output = self.interleave(torch.split(last_output, bs), bs)
 return {'last_output': last_output}
def interleave_offsets(self, batch, nu):
groups = [batch //(nu + 1)] *(nu + 1)
 for x in range(batch - sum(groups)) :
groups[-x - 1] += 1
offsets = [0]
for g in groups:
offsets.append(offsets[-1] + g)
 return offsets
def interleave(self, xy, batch):
nu = len(xy)- 1
offsets = self.interleave_offsets(batch, nu)
 xy = [[v[offsets[p]:offsets[p + 1]] for p in range(nu + 1)] for v in xy]
for i in range(1, nu + 1):
xy[0][i], xy[i][i] = xy[i][i], xy[0][i]
return torch.cat([torch.cat(v, dim=0)for v in xy], dim=0)
 MM_LOG = pd.DataFrame(columns=['loss', 'loss_labeled', 'loss_unlabeled', 'weight'])
 class MultiSemiLoss(nn.Module):
"Adapt the loss function `crit` to go with mixmatch."
def __init__(self, crit, lambda_u=100.0, rampup=16):
super().__init__()
self.crit = crit
self.labeled_bs = 0
self.lambda_u = lambda_u
self.rampup = rampup
self.epoch = 0
def forward(self, output, target):
if self.labeled_bs == 0: return self.crit(output, target)
 output_x, target_x = output[:self.labeled_bs], target[:self.labeled_bs]
output_u, target_u = output[self.labeled_bs:], target[self.labeled_bs:]
q = output_u.sigmoid()
Lx = self.crit(output_x, target_x)
 Lu = torch.mean(( q - target_u)** 2)
 w = self.lambda_u *(np.clip(self.epoch / self.rampup, 0., 1.) if self.rampup > 0 else 1.)
self.labeled_bs = 0
d = Lx + w * Lu
MM_LOG.loc[self.epoch] = [float(d), float(Lx), float(Lu), w]
return d
def get_old(self):
return self.crit
def _mixmatch(learn:Learner, unlabeled_data:ImageDataBunch,
temperature:float=0.5, n_augment:int=2,
alpha:float=0.75, lambda_u:float=100, rampup:int=16)-> Learner:
"Add mixup https://arxiv.org/abs/1905.02249 to `learn`."
unlabeled_dl = unlabeled_data.dl(DatasetType.Train)
 learn.callback_fns.append(partial(MixMatchCallback,
unlabeled_dl=unlabeled_dl,
temperature=temperature, n_augment=n_augment,
alpha=alpha, lambda_u=lambda_u, rampup=rampup))
return learn
Learner.mixmatch = _mixmatch<train_model>
depth = maxDepths[np.argmax(cv_scores)]
model = DecisionTreeClassifier(max_depth=depth)
 model.fit(train_x,train_y)
 score = model.score(test_x,test_y)
 print(score )
<define_search_model><EOS> 
id = testSet['PassengerId']
id = id.as_matrix()
result = list(zip(id,model.predict(testSet1)))
 df = pd.DataFrame(result, columns = ['PassengerId', 'Survived'])
 df.to_csv('decisionTreeResult.csv', index = False)
 print(df.shape)
 df.head()