Kaggle competition: Instant Gratification
<SOS> metric: AUC Kaggle data source: instant-gratification<choose_model_class>
PATH_BASE = Path('.. /input')
 PATH_WORKING = Path('.. /working' )
model.compile(loss='categorical_crossentropy', optimizer='adam',
metrics=['accuracy'] )<train_model>
train = pd.read_csv(PATH_BASE/'train.csv')
 test = pd.read_csv(PATH_BASE/'test.csv' )
str_ = 'Traning Started'
os.system('echo '+str_ )<train_model>
def get_mean_cov(x,y):
model = GraphicalLasso(max_iter=200)
 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
batch_size = 128
epochs = 25
checkpoint = ModelCheckpoint(
 'model.h5',
monitor='val_acc',
verbose=1,
save_best_only=True,
save_weights_only=False,
mode='auto' 
)
 history = model.fit(
 x=x_train,
y=y_train,
batch_size=64,
epochs=10,
callbacks=[checkpoint],
validation_split=0.1
)<load_pretrained>
def projectMeans(means):
means[means>0]=1
means[means<=0]=-1
return means
def _compute_precision_cholesky(covariances, covariance_type):
estimate_precision_error_message =("Hell no")
 if covariance_type in 'full':
n_components, n_features, _ = covariances.shape
precisions_chol = np.empty(( n_components, n_features, n_features))
for k, covariance in enumerate(covariances):
try:
cov_chol = linalg.cholesky(covariance, lower=True)
 except linalg.LinAlgError:
raise ValueError(estimate_precision_error_message)
 precisions_chol[k] = linalg.solve_triangular(cov_chol,
np.eye(n_features),
lower=True ).T
return precisions_chol
def _estimate_gaussian_covariances_full(resp, X, nk, means, reg_covar):
n_components, n_features = means.shape
covariances = np.empty(( n_components, n_features, n_features))
for k in range(n_components):
diff = X - means[k]
covariances[k] = np.dot(resp[:, k] * diff.T, diff)/ nk[k]
covariances[k].flat[::n_features + 1] += reg_covar
return covariances
def _estimate_gaussian_parameters2(X, resp, reg_covar, covariance_type):
nk = resp.sum(axis=0)+ 10 * np.finfo(resp.dtype ).eps
means = np.dot(resp.T, X)/ nk[:, np.newaxis]
means = projectMeans(means)
 covariances = {"full": _estimate_gaussian_covariances_full}[covariance_type](resp, X, nk, means, reg_covar)
 return nk, means, covariances
class GaussianMixture2(GaussianMixture):
def _m_step(self, X, log_resp):
resp = np.exp(log_resp)
 sums = resp.sum(0)
 if sums.max() - sums.min() > 2:
for i in range(3):
resp = len(X)* resp / resp.sum(0)/ len(sums)
 resp = resp/resp.sum(1)[:,None]
n_samples, _ = X.shape
self.weights_, self.means_, self.covariances_ =(
 _estimate_gaussian_parameters2(X, resp, self.reg_covar,
self.covariance_type))
self.weights_ /= n_samples
self.precisions_cholesky_ = _compute_precision_cholesky(
 self.covariances_, self.covariance_type)
 random.seed(1234)
 np.random.seed(1234)
 os.environ['PYTHONHASHSEED'] = str(1234 )
model.load_weights('model.h5' )<predict_on_test>
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))
N_RAND_INIT = 2
N_CLUST_OPT = 3
N_TEST = 1
all_acc = np.zeros(( 512, N_CLUST_OPT, N_RAND_INIT))
all_roc = np.zeros(( 512, N_CLUST_OPT, N_RAND_INIT))
cluster_cnt = np.zeros(( 512, N_CLUST_OPT, N_RAND_INIT))
j_selection = np.zeros(N_CLUST_OPT)
 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])
 test_index = range(len(train3))
yf = train2['target']
ms, ps = get_mean_cov(train3,yf)
 cc_list = []
nc_list = 2*(np.array(range(N_CLUST_OPT)) + 2)
 for j in range(N_CLUST_OPT):
cc_list.append(['cluster_' + str(i)for i in range(nc_list[j])])
 gm_list = []
acc = np.zeros(( N_CLUST_OPT, N_RAND_INIT))
res_list = []
ctc_list = []
for j in range(N_CLUST_OPT):
gm_list.append([])
 res_list.append([])
 ctc_list.append([])
 nc = nc_list[j]
cl = int(0.5*nc)
 for k in range(N_RAND_INIT):
ps_list = np.concatenate([ps]*cl, axis=0)
 th_step = 100/(cl+1)
 th_p = np.arange(th_step,99,th_step)+ 0.5*(np.random.rand(cl)- 0.5)*th_step
th = np.percentile(ms,th_p)
 ms_list = []
for t in range(cl):
ms_new = ms.copy()
ms_new[ms>=th[t]]=1
ms_new[ms<th[t]]=-1
ms_list.append(ms_new)
 ms_list = np.concatenate(ms_list, axis=0)
 perm = np.random.permutation(nc)
 ps_list = ps_list[perm]
ms_list = ms_list[perm]
gm = GaussianMixture2(n_components=nc, init_params='random', covariance_type='full', tol=0.0001,reg_covar=0.001,
max_iter=5000, n_init=1, means_init=ms_list, precisions_init=ps_list, random_state=1234)
 gm.fit(np.concatenate([train3,test3],axis = 0))
res = pd.concat([pd.DataFrame(gm.predict_proba(train3), columns = cc_list[j]),
yf.to_frame().reset_index(drop=True)], sort=False, axis=1)
 cluster_to_class = res.groupby('target' ).agg('mean' ).values.argmax(0)
 cluster_cnt[i,j,k] = cluster_to_class.sum()
res = pd.concat([pd.DataFrame(gm.predict_proba(train3), columns = cc_list[j]),
pd.DataFrame(cluster_to_class, index=cc_list[j],
columns=['target'] ).transpose() ], sort=False, axis=0 ).\
transpose().groupby('target' ).agg(sum ).transpose()
res_list[j].append(res[1])
 gm_list[j].append(gm)
 ctc_list[j].append(cluster_to_class)
 acc[j,k] =(res.values.argmax(1)== yf.values ).mean()
all_acc[i,j,k] = acc[j,k]
all_roc[i,j,k] = roc_auc_score(yf.values, res[1])
 best_j = acc.mean(1 ).argmax()
j_selection[best_j] += 1
for k in np.argsort(acc[best_j,:])[-N_TEST:]:
res2 = pd.concat([pd.DataFrame(gm_list[best_j][k].predict_proba(test3), columns = cc_list[best_j]),
pd.DataFrame(ctc_list[best_j][k], index=cc_list[best_j],
columns=['target'] ).transpose() ], sort=False, axis=0 ).\
transpose().groupby('target' ).agg(sum ).transpose()
oof[idx1] += res_list[best_j][k]/N_TEST
preds[idx2] += res2[1]/N_TEST
if i%10==0: print('QMM scores CV =',round(roc_auc_score(train['target'],oof),5))
auc = roc_auc_score(train['target'],oof)
 print(j_selection)
 print('Final QMM scores CV =',round(auc,5))
pred = model.predict_classes(x_test,verbose=1 )<load_from_csv>
for j in range(N_CLUST_OPT):
print(np.all(cluster_cnt[:,j,:] == 0.5*nc_list[j]))
<save_to_csv><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()