Kaggle competition: Instant Gratification
<SOS> metric: AUC Kaggle data source: instant-gratification<normalization>
import numpy as np, pandas as pd, os
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis, LinearDiscriminantAnalysis
from sklearn.feature_selection import VarianceThreshold
from sklearn.model_selection import StratifiedKFold, KFold
from sklearn.metrics import roc_auc_score
from tqdm import tqdm
from sklearn.covariance import EmpiricalCovariance
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
import sympy
y = sig(y)
 y<create_dataframe>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 train.head()
ans2 = pd.DataFrame(IDtest )<feature_engineering>
def get_mean_cov(x,y):
model = OAS()
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
ans2["Survived"] = y<save_to_csv>
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_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)
 sel = VarianceThreshold(threshold=1.5 ).fit(train2[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(train3, train2['target']):
x_train, y_train = train3[train_index,:], train2.loc[train_index]['target'].values
x_train_0 = x_train[y_train==0]
x_train_1 = x_train[y_train==1]
brc = Birch(branching_factor=50, n_clusters=3, threshold=0.4, compute_labels=True)
 labels_0 = brc.fit_predict(x_train_0)
 labels_1 = brc.fit_predict(x_train_1)
 zero_mean = []
zero_cov = []
for l in np.unique(labels_0):
model = OAS()
model.fit(x_train_0[labels_0==l])
 p = model.precision_
m = model.location_
zero_mean.append(m)
 zero_cov.append(p)
 one_mean = []
one_cov = []
for l in np.unique(labels_1):
model = OAS()
model.fit(x_train_1[labels_1==l])
 p = model.precision_
m = model.location_
one_mean.append(m)
 one_cov.append(p)
 ms = np.stack(zero_mean + one_mean)
 ps = np.stack(zero_cov + one_cov)
 gm = GaussianMixture(n_components=6, init_params='random',
covariance_type='full', tol=0.001,reg_covar=0.001, max_iter=100, n_init=1, means_init=ms, precisions_init=ps)
 gm.fit(np.concatenate([train3[train_index,:],test3],axis = 0))
oof[idx1[test_index]] = gm.predict_proba(train3[test_index,:])[:, 0:3].mean(axis=1)
 preds[idx2] += gm.predict_proba(test3)[:, 0:3].mean(axis=1)/ skf.n_splits
print('AUC ', i, roc_auc_score(1- train2['target'], oof[idx1]))
auc = roc_auc_score(1 - train['target'],oof)
 print('QDA scores CV =',round(auc,5))
ans2.to_csv("sub.csv",index = False )<data_type_conversions>
auc = roc_auc_score(1 - train['target'],oof)
 print('QDA scores CV =',round(auc,5))
x_train = np.load('.. /input/reducing-image-sizes-to-32x32/X_train.npy')
 x_test = np.load('.. /input/reducing-image-sizes-to-32x32/X_test.npy')
 y_train = np.load('.. /input/reducing-image-sizes-to-32x32/y_train.npy')
 print('x_train shape:', x_train.shape)
 print(x_train.shape[0], 'train samples')
 print(x_test.shape[0], 'test samples')
 x_train = x_train.astype('float32')
 x_test = x_test.astype('float32')
 x_train /= 255.
 x_test /= 255 .<train_model>
x_test_0 = pd.read_csv('.. /input/test.csv')
 x_test_0['target']=preds
datagen_train = ImageDataGenerator(
 width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=True)
 datagen_train.fit(x_train )<import_modules>
cols = [c for c in train.columns if c not in ['id', 'target']]
cols.remove('wheezy-copper-turtle-magic' )
from keras.applications import DenseNet121
from keras.layers import *
from keras.models import Sequential<choose_model_class>
oof = np.zeros(len(train))
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 = x_test_0[x_test_0['wheezy-copper-turtle-magic']==k]
test2p = test2
train2p = pd.concat([train2p,test2p],axis=0)
 train2p.reset_index(drop=True,inplace=True)
 sel = VarianceThreshold(threshold=1.5 ).fit(train2p[cols])
 train3p = sel.transform(train2p[cols])
 train3 = sel.transform(train2[cols])
 test3 = sel.transform(test2[cols])
 skf = KFold(n_splits=17, random_state=42, shuffle=True)
 for train_index, test_index in skf.split(train3p):
test_index3 = test_index[ test_index<len(train3)]
clf = neighbors.KNeighborsRegressor(n_neighbors=9, weights='distance')
 clf.fit(train3p[train_index,:], train2p.loc[train_index]['target'])
 oof[idx1[test_index3]] = clf.predict(train3[test_index3,:])
 preds[test2.index] += clf.predict(test3)/ skf.n_splits
if k%64==0: print(k)
 auc = roc_auc_score(train['target'], oof)
 print('Pseudo Labeled QDA scores CV =',round(auc,5))
<choose_model_class><EOS> 
sub = pd.read_csv('.. /input/sample_submission.csv')
 sub['target'] = preds
sub.to_csv('submission.csv',index=False)
 plt.hist(preds,bins=100)
 plt.title('Final Test.csv predictions')
 plt.show()