Kaggle competition: Instant Gratification
<SOS> metric: AUC Kaggle data source: instant-gratification<drop_column>
np.random.seed(0 )
dataset.drop(labels=['Fence_num'],axis=1,inplace=True )<feature_engineering>
train = pd.read_csv('.. /input/train.csv', index_col='id')
 test = pd.read_csv('.. /input/test.csv', index_col='id')
 data = pd.concat([train, test], sort=False )
tmp = dataset.loc[dataset['FireplaceQu'].isnull() ]
d = ['TA','Gd']
lens = len(tmp)
 dataset.loc[dataset['FireplaceQu'].isnull() ,'FireplaceQu'] = [ d[ x % len(d)] for x in np.random.randint(10,1000,lens)] 
<feature_engineering>
methods = ['diff', 'discrete']
methods += ['random'+str(i)for i in range(1, 100)]
pred = pd.DataFrame(index=test.index, columns=methods)
 for i in range(512):
cur = data[data['wheezy-copper-turtle-magic']==i]
train_idx = [i for i in cur.index if i in train.index]
test_idx = [i for i in cur.index if i in test.index]
y_data = cur['target']
x_data = cur.loc[:, cur.std() >2]
cur_test = x_data.loc[test_idx]
gm = GaussianMixture(6, n_init=20, max_iter=10000, tol=1e-5 ).fit(x_data)
 tmp = pd.DataFrame([gm.predict(x_data), y_data.values] ).dropna(axis=1)
 pos_val_counts = tmp.loc[:, tmp.loc[1]==1].loc[0].value_counts()
neg_val_counts = tmp.loc[:, tmp.loc[1]==0].loc[0].value_counts()
pos_cluster = pos_val_counts.iloc[:3].index.astype(int)
 neg_cluster = neg_val_counts.iloc[:3].index.astype(int)
 pos_mns = [multivariate_normal(gm.means_[i], gm.covariances_[i])for i in pos_cluster]
neg_mns = [multivariate_normal(gm.means_[i], gm.covariances_[i])for i in neg_cluster]
pos_pred = np.max([pos_mn.logpdf(cur_test)for pos_mn in pos_mns], axis=0)
 neg_pred = np.max([neg_mn.logpdf(cur_test)for neg_mn in neg_mns], axis=0)
 pred.loc[cur_test.index, 'diff'] = pos_pred - neg_pred
pred.loc[cur_test.index, 'discrete'] = pd.Series(gm.predict(cur_test)).replace(pos_cluster, -1 ).replace(neg_cluster, 0 ).abs().values
for i in range(1, 100):
pred['random'+str(i)] = pred['discrete']
ascending = np.arange(2, len(pred)+2)
 n_zeros =(pred['discrete']==0 ).sum()
n_ones =(pred['discrete']==1 ).sum()
zeros = ascending[:n_zeros].copy()
ones = ascending[-n_ones:].copy()
np.random.shuffle(zeros)
 np.random.shuffle(ones)
 pred['random'+str(i)][pred['discrete']==0] = zeros
pred['random'+str(i)][pred['discrete']==1] = ones
<feature_engineering><EOS> 
for i in methods:
sub = pd.DataFrame({'id': pred.index, 'target': pred[i].values})
 sub.to_csv(i+'.csv', index=False)
 sub.head(20 )