Kaggle competition: Instant Gratification
print(big['TName'].value_counts())
 big['XWho'] = big['TName']
for i in [ 'Master.', 'Sir.', 'Don.', 'Lady.', 'Dona.', 'the Countess.', 'Mme.' ]:
big['XWho'][big['TName'] == i] = "High."
for i in [ 'Col.', 'Major.', 'Capt.' ]:
big['XWho'][big['TName'] == i] = "Mil."
for i in [ 'Mr.', 'Dr.', 'Rev.' ]:
big['XWho'][big['TName'] == i] = "Mr."
for i in [ 'Mrs.', 'Ms.', 'Mlle.', 'Miss.' ]:
big['XWho'][big['TName'] == i] = "Miss."
big['XWho'][~big['TName'].isin([ 'Sir.', 'Don.', 'Lady.', 'Dona.', 'the Countess.', 'Col.',
'Major.', 'Capt.', 'Mr.', 'Master.', 'Dr.', 'Rev.', 'Mrs.',
'Ms.', 'Mlle.', 'Mme.', 'Miss.' ])] = "Oth."
print(big['XWho'].value_counts() )<categorify>
def get_mean_cov(x,y):
model = GraphicalLasso()
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,m2])
 ps = np.stack([p1,p2])
 return ms,ps
for col in [ 'Sex', 'Pclass', 'XWho', 'Embarked', 'LTick', 'XCabin', 'TName' ]:
dummy = pd.get_dummies(big[col],prefix=str(col),prefix_sep="__")
 big = pd.concat([big, dummy], axis=1)
 big.drop(col, inplace=True, axis=1)
 for col in [ 'XFam' ]:
lbl = LabelEncoder()
lbl.fit(list(big[col].values))
big[col] = lbl.transform(list(big[col].values))<filter>
cols = [c for c in train.columns if c not in ['id', 'target']]
cols.remove('wheezy-copper-turtle-magic')
 oof = np.zeros(len(train))
preds = np.zeros(len(test))
for i in tqdm(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])
 ms, ps = get_mean_cov(train3,train2['target'].values)
 skf = StratifiedKFold(n_splits=15, random_state=42, shuffle=True)
 for train_index, test_index in skf.split(train3, train2['target']):
P = train3[train_index,:]
T = train2.loc[train_index]['target'].values
gm = GaussianMixture(n_components=2, init_params='kmeans', covariance_type='full', tol=0.1,reg_covar=0.1, max_iter=150, n_init=5,means_init=ms, precisions_init=ps)
 gm.fit(np.concatenate([P,test3],axis = 0))
oof[idx1[test_index]] = gm.predict_proba(train3[test_index,:])[:,0]
preds[idx2] += gm.predict_proba(test3)[:,0] / skf.n_splits
auc = roc_auc_score(train['target'],oof)
 print('QDA scores CV =',round(auc,5))
<feature_engineering><EOS> 
sub = pd.read_csv('.. /input/instant-gratification/sample_submission.csv')
 sub['target'] = preds
sub.to_csv('submission.csv',index=False )