Kaggle competition: Instant Gratification
<SOS> metric: AUC Kaggle data source: instant-gratification<filter>
warnings.filterwarnings("ignore")
low_low_fare = train[train["Fare"] < 100]<feature_engineering>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
train["FamilySize"] = train["SibSp"] + train["Parch"] + 1
train[["FamilySize"]].head()<feature_engineering>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
train.loc[train["FamilySize"]==1, "FamilyType"] = "Single"
train.loc[(train["FamilySize"] > 1)&(train["FamilySize"] < 5), "FamilyType"] = "Nuclear"
train.loc[train["FamilySize"] >=5, "FamilyType"] = "Big"
train[["FamilySize", "FamilyType"]].head()<create_dataframe>
def get_mean_cov(x,y):
model = LedoitWolf()
ones =(y==1 ).astype(bool)
 x2 = x[ones]
kmeans = KMeans(4, random_state=125,n_jobs=4 ).fit(x2)
 kmeans_predictions =(kmeans.predict(x2))
onesb =(kmeans_predictions==0 ).astype(bool)
 x21 = x2[onesb]
model.fit(x21)
 p1 = model.precision_
m1 = model.location_
onesb =(kmeans_predictions==1 ).astype(bool)
 x22 = x2[onesb]
model.fit(x22)
 p2 = model.precision_
m2 = model.location_
onesb =(kmeans_predictions==2 ).astype(bool)
 x23 = x2[onesb]
model.fit(x23)
 p3 = model.precision_
m3 = model.location_
onesb =(kmeans_predictions==3 ).astype(bool)
 x24 = x2[onesb]
model.fit(x24)
 p4 = model.precision_
m4 = model.location_
onesb =(y==0 ).astype(bool)
 x2b = x[onesb]
kmeans = KMeans(4, random_state=125,n_jobs=4 ).fit(x2b)
 kmeans_predictions =(kmeans.predict(x2b))
onesb =(kmeans_predictions==0 ).astype(bool)
 x2b1 = x2b[onesb]
model.fit(x2b1)
 p5 = model.precision_
m5 = model.location_
onesb =(kmeans_predictions==1 ).astype(bool)
 x2b2 = x2b[onesb]
model.fit(x2b2)
 p6 = model.precision_
m6 = model.location_
onesb =(kmeans_predictions==2 ).astype(bool)
 x2b3 = x2b[onesb]
model.fit(x2b3)
 p7 = model.precision_
m7 = model.location_
onesb =(kmeans_predictions==3 ).astype(bool)
 x2b4 = x2b[onesb]
model.fit(x2b4)
 p8 = model.precision_
m8 = model.location_
ms = np.stack([m1,m2,m3,m4,m5,m6,m7,m8])
 ps = np.stack([p1,p2,p3,p4,p5,p6,p7,p8])
 return ms,ps
pd.pivot_table(data=train, index="FamilyType", values="Survived" )<feature_engineering>
test['target'] = preds
oof_final6 = np.zeros(len(train))
final_preds = np.zeros(len(test))
for k in tqdm_notebook(range(512)) :
train2 = train[train['wheezy-copper-turtle-magic']==k]
train2p = train2.copy() ; idx1 = train2.index
test2 = test[test['wheezy-copper-turtle-magic']==k]
test2p = test2[(test2['target']<=0.01)|(test2['target']>=0.99)].copy()
test2p.loc[ test2p['target']>=0.5, 'target' ] = 1
test2p.loc[ test2p['target']<0.5, 'target' ] = 0
train2p = pd.concat([train2p,test2p],axis=0)
 train2p.reset_index(drop=True,inplace=True)
 sel = VarianceThreshold(threshold=2 ).fit(train2p[cols])
 train3p = sel.transform(train2p[cols])
 train3 = sel.transform(train2[cols])
 test3 = sel.transform(test2[cols])
 skf = StratifiedKFold(n_splits=11, random_state=42, shuffle=True)
 for train_index, test_index in skf.split(train3p, train2p['target']):
test_index3 = test_index[ test_index<len(train3)]
ms, ps = get_mean_cov(train3p[train_index,:],train2p.loc[train_index]['target'].values)
 gm = GaussianMixture(n_components=8, init_params='random', covariance_type='full',
tol=0.001,reg_covar=1, max_iter=100, n_init=1,means_init=ms, precisions_init=ps)
 gm.fit(np.concatenate([train3p,test3],axis = 0))
oof_final6[idx1[test_index3]] = gm.predict_proba(train3[test_index3,:])[:,:4].sum(axis=1)
 final_preds[test2.index] += gm.predict_proba(test3)[:,:4].sum(axis=1)/ skf.n_splits
auc = roc_auc_score(train['target'],oof_final6)
 print('Pseudo Labeled GMM scores CV =',round(auc,5))
train.loc[train["Name"].str.contains("Mr"), "Title"] = "Mr"
train.loc[train["Name"].str.contains("Miss"), "Title"] = "Miss"
train.loc[train["Name"].str.contains("Mrs"), "Title"] = "Mrs"
train.loc[train["Name"].str.contains("Master"), "Title"] = "Master"
train[["Name", "Title"]].head()<feature_engineering>
auc = roc_auc_score(train['target'],oof_final6+oof)
 print('Pseudo Labeled GMM scores CV =',round(auc,5))
train.loc[train["Sex"] == "male", "Sex_encode"] = 0
train.loc[train["Sex"] == "female", "Sex_encode"] = 1
train[["Sex", "Sex_encode"]].head()<feature_engineering>
sample = pd.read_csv(".. /input/sample_submission.csv" )
<filter><EOS> 
sample.target =(final_preds+preds)/2
sample.to_csv("submission.csv",index=False )