Kaggle competition: Instant Gratification
<SOS> metric: AUC Kaggle data source: instant-gratification<save_to_csv>
warnings.filterwarnings('ignore' )
datasets = pd.DataFrame({
'PassengerId': test_df['PassengerId'],
'Survived': y_pred
})
 datasets.to_csv('gender_submission3.csv', index=False )<load_from_csv>
n_models = 4
n_splits = 12
seed_split = 123
seed_gm = 987
cov_type = 'GL_0.2'
n_init = 4
init_params = 'random'
n_clusters_per_class = 3
train=pd.read_csv('/kaggle/input/titanic/train.csv')
 test=pd.read_csv('/kaggle/input/titanic/test.csv')
 gender_submision=pd.read_csv('/kaggle/input/titanic/gender_submission.csv')
 train.head()<feature_engineering>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 train = train.reset_index(drop=True)
 print('train:', train.shape)
 print('test:', test.shape )
train['titlelst']=train.Name.str.split(' ' ).apply(lambda x:x[-1])
 test['titlelst']=test.Name.str.split(' ' ).apply(lambda x:x[-1])
 train['title1st']=train.Name.str.split(',' ).apply(lambda x:x[1] ).str.split('.' ).apply(lambda x:x[0])
 test['title1st']=test.Name.str.split(',' ).apply(lambda x:x[1] ).str.split('.' ).apply(lambda x:x[0] )<count_values>
def get_cov_estimator(cov_type):
if cov_type == 'LW':
model = LedoitWolf()
elif cov_type == 'OAS':
model = OAS()
elif cov_type == 'MCD':
model = MinCovDet()
elif cov_type[:2] == 'SC':
shrinkage = float(cov_type.split('_')[1])
 model = ShrunkCovariance(shrinkage=shrinkage)
 elif cov_type[:2] == 'GL':
alpha = float(cov_type.split('_')[1])
 model = GraphicalLasso(alpha=alpha)
 return model
def get_mean_cov(x, y, n_clusters_per_class=1, cov_type='LW'):
model = get_cov_estimator(cov_type)
 ones =(y == 1 ).astype(bool)
 x2 = x[ones]
model.fit(x2)
 p1 = model.precision_
m1 = model.location_
onesb =(y == 0 ).astype(bool)
 x2b = x[onesb]
model.fit(x2b)
 p2 = model.precision_
m2 = model.location_
ms = np.stack([m1] * n_clusters_per_class + [m2] * n_clusters_per_class)
 ps = np.stack([p1] * n_clusters_per_class + [p2] * n_clusters_per_class)
 return ms, ps
dict(test.title1st.value_counts() )<count_values>
cols = [c for c in train.columns if c not in ['id', 'target']]
cols.remove('wheezy-copper-turtle-magic')
 oof_avg = np.zeros(len(train))
preds = np.zeros(len(test))
for j in range(n_models):
t1 = time.time()
oof = np.zeros(len(train))
for i in range(512):
train2 = train[train['wheezy-copper-turtle-magic'] == i]
test2 = test[test['wheezy-copper-turtle-magic'] == i]
idx1 = train2.index
idx2 = test2.index
train2.reset_index(drop=True, inplace=True)
 sel = VarianceThreshold(threshold=1.5 ).fit(train2[cols])
 train3 = sel.transform(train2[cols])
 test3 = sel.transform(test2[cols])
 skf = StratifiedKFold(n_splits=n_splits,
random_state=seed_split + j,
shuffle=True)
 for train_idx, valid_idx in skf.split(train3, train2['target']):
ms, ps = get_mean_cov(train3[train_idx, :],
train2.loc[train_idx]['target'].values,
n_clusters_per_class=n_clusters_per_class,
cov_type=cov_type)
 gm = GaussianMixture(n_components=2 * n_clusters_per_class,
init_params=init_params,
covariance_type='full',
max_iter=100,
n_init=n_init,
precisions_init=ps,
random_state=seed_gm + i)
 gm.fit(np.concatenate([train3[train_idx, :], test3]))
oof[idx1[valid_idx]] += gm.predict_proba(train3[valid_idx, :])[:, :2].sum(1)
 preds[idx2] += gm.predict_proba(test3)[:, :2].sum(1)
 oof_avg += oof
auc_one = roc_auc_score(train['target'], oof)
 auc_avg = roc_auc_score(train['target'], oof_avg)
 print(f' model:{j} AUC_avg:{auc_avg:.5f} AUC:{auc_one:.5f} time:{time.time() - t1:.1f}s' )
print(train.titlelst.value_counts())
 droplist=['titlelst']<concatenate>
preds /= n_models * n_splits
sub = pd.read_csv('.. /input/sample_submission.csv')
 sub['target'] = preds
sub.to_csv('submission.csv', index=False )