Kaggle competition: Instant Gratification
dataset["Pclass"] = dataset["Pclass"].astype("category")
 dataset = pd.get_dummies(dataset, columns = ["Pclass"],prefix="Pc" )<drop_column>
def permute_predict(y):
_y = y.copy()
_c1 = _y < 0.00001
_c2 = _y > 0.99999
_y[_c1] = _y[_c1].max() - _y[_c1] + _y[_c1].min()
_y[_c2] = _y[_c2].max() - _y[_c2] + _y[_c2].min()
return _y
dataset.drop(labels = ["PassengerId"], axis = 1, inplace = True )<drop_column>
warnings.filterwarnings('ignore' )
train = dataset[:train_len]
test = dataset[train_len:]
test.drop(labels=["Survived"],axis = 1,inplace=True )<prepare_x_and_y>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 train.head()
train["Survived"] = train["Survived"].astype(int)
 Y_train = train["Survived"]
X_train = train.drop(labels = ["Survived"],axis = 1 )<choose_model_class>
cols = [c for c in train.columns if c not in ['id', 'target']]
cols.remove('wheezy-copper-turtle-magic' )
lr = LogisticRegression()<import_modules>
oof = np.zeros(len(train))
preds = np.zeros(len(test))
for i in tqdm_notebook(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)
 data = pd.concat([pd.DataFrame(train2[cols]), pd.DataFrame(test2[cols])])
 data2 = VarianceThreshold(2.3 ).fit_transform(data[cols])
 train3 = data2[:train2.shape[0]]; test3 = data2[train2.shape[0]:]
for c in range(train3.shape[1]):
low_=np.quantile(train3[:,c] , 0.001)
 up_=np.quantile(train3[:,c], 0.999)
 train3[:,c]=np.clip(train3[:,c],low_, up_)
 test3[:,c]=np.clip(test3[:,c],low_, up_)
 skf = StratifiedKFold(n_splits=11, random_state=42)
 for train_index, test_index in skf.split(train2, train2['target']):
gmm=GMM(n_components=5, random_state=42, covariance_type='full')
 gmm.fit(np.vstack([train3[train_index], test3]))
gmm_1_train=gmm.predict_proba(train3[train_index])
 gmm_1_val=gmm.predict_proba(train3[test_index])
 gmm_1_test=gmm.predict_proba(test3)
 gmm=GMM(n_components=4, random_state=42, covariance_type='full')
 gmm.fit(np.vstack([train3[train_index], test3]))
gmm_2_train=gmm.predict_proba(train3[train_index])
 gmm_2_val=gmm.predict_proba(train3[test_index])
 gmm_2_test=gmm.predict_proba(test3)
 gmm=GMM(n_components=6, random_state=42, covariance_type='full')
 gmm.fit(np.vstack([train3[train_index], test3]))
gmm_3_train=gmm.predict_proba(train3[train_index])
 gmm_3_val=gmm.predict_proba(train3[test_index])
 gmm_3_test=gmm.predict_proba(test3)
 bgm=BGM(n_components=5, random_state=42)
 bgm.fit(np.vstack([train3[train_index], test3]))
bgm_1_train=bgm.predict_proba(train3[train_index])
 bgm_1_val=bgm.predict_proba(train3[test_index])
 bgm_1_test=bgm.predict_proba(test3)
 bgm=BGM(n_components=4, random_state=42)
 bgm.fit(np.vstack([train3[train_index], test3]))
bgm_2_train=bgm.predict_proba(train3[train_index])
 bgm_2_val=bgm.predict_proba(train3[test_index])
 bgm_2_test=bgm.predict_proba(test3)
 bgm=BGM(n_components=6, random_state=42)
 bgm.fit(np.vstack([train3[train_index], test3]))
bgm_3_train=bgm.predict_proba(train3[train_index])
 bgm_3_val=bgm.predict_proba(train3[test_index])
 bgm_3_test=bgm.predict_proba(test3)
 _train = np.hstack(( train3[train_index],
gmm_1_train, gmm_2_train, gmm_3_train,
bgm_1_train, bgm_2_train, bgm_3_train))
_val = np.hstack(( train3[test_index],
gmm_1_val, gmm_2_val, gmm_3_val,
bgm_1_val, bgm_2_val, bgm_3_val))
_test = np.hstack(( test3,
gmm_1_test, gmm_2_test, gmm_3_test,
bgm_1_test, bgm_2_test, bgm_3_test))
clf = QuadraticDiscriminantAnalysis(reg_param=0.04, tol=0.01)
 clf.fit(_train,train2.loc[train_index]['target'])
 oof[idx1[test_index]] = clf.predict_proba(_val)[:,1]
preds[idx2] += clf.predict_proba(_test)[:,1] / skf.n_splits
print(i, roc_auc_score(train2['target'], oof[idx1]))
print(roc_auc_score(train['target'], oof))
from sklearn.naive_bayes import GaussianNB<choose_model_class>
sub = pd.read_csv('.. /input/sample_submission.csv')
 sub['target'] = permute_predict(preds)
 sub.to_csv('submission.csv',index=False)
 plt.hist(preds,bins=100)
 plt.title('Final Test.csv predictions')
 plt.show()