text_plain_output_1.png
from sklearn.covariance import GraphicalLasso

from sklearn.feature_selection import VarianceThreshold

from sklearn.metrics import roc_auc_score

from sklearn.mixture import GaussianMixture

from sklearn.model_selection import StratifiedKFold

from tqdm import tqdm

import numpy as np, pandas as pd, os
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
from sklearn.model_selection import StratifiedKFold
from sklearn.feature_selection import VarianceThreshold
from tqdm import tqdm
from sklearn.covariance import EmpiricalCovariance
from sklearn.covariance import GraphicalLasso
from sklearn.metrics import roc_auc_score
from sklearn.decomposition import PCA
from matplotlib import pyplot as plt
from sklearn.preprocessing import StandardScaler
from sklearn.mixture import GaussianMixture
from sklearn.svm import NuSVC
from sklearn import svm, neighbors, linear_model, neural_network
from sklearn.ensemble import RandomForestClassifier
from tqdm import tqdm_notebook
train = pd.read_csv('../input/train.csv')
test = pd.read_csv('../input/test.csv')

def get_mean_cov(x, y):
    model = GraphicalLasso()
    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)

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(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']):
        ms, ps = get_mean_cov(train3[train_index, :], train2.loc[train_index]['target'].values)
        gm = GaussianMixture(n_components=2, 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, test3], axis=0))
        oof[idx1[test_index]] = gm.predict_proba(train3[test_index, :])[:, 0]
        preds[idx2] += gm.predict_proba(test3)[:, 0] / skf.n_splits
auc = roc_auc_score(train['target'], oof)

cat_dict = dict()
cols = [c for c in train.columns if c not in ['id', 'target']]
cols.remove('wheezy-copper-turtle-magic')
for i in 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])
    cat_dict[i] = train3.shape[1]

pd.DataFrame(list(cat_dict.items()))[1].value_counts().plot.barh()