Kaggle competition: Kannada MNIST
train = np.unique([p.split('-')[0] for p in sorted(glob.glob('.. /input/train_*/**')) ])
 test = np.unique([p.split('-')[0] for p in sorted(glob.glob('.. /input/test/**')) ])
 def read_files(path):
return [p.split('-')[0] for p in sorted(glob.glob(path)) ]
train_file_names = np.unique(read_files('.. /input/train_*/**.csv'))
test_file_names = np.unique(read_files('.. /input/test/**.csv'))
detector = pd.read_csv('.. /input/detectors.csv')
 submission = pd.read_csv('.. /input/sample_submission.csv' )<choose_model_class>
%matplotlib inline
denoise_model = Sequential()
denoise_model.add(Dense(256, activation='relu', input_shape=[25, ]))
denoise_model.add(Dense(256, activation='relu'))
denoise_model.add(Dense(256, activation='relu'))
denoise_model.add(Dropout(0.5))
denoise_model.add(Dense(256, activation='relu'))
denoise_model.add(Dense(256, activation='relu'))
denoise_model.add(Dense(256, activation='relu'))
denoise_model.add(BatchNormalization())
 denoise_model.add(Dense(1, activation='sigmoid'))
denoise_model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'] )<merge>
train_df = pd.read_csv("/kaggle/input/Kannada-MNIST/train.csv" )
def get_train_data_for_denoise(e):
hits, cells, truth = load_event(e, parts=['hits', 'cells', 'truth'])
 hits['event_id'] = int(e[-9:])
 cells = cells.groupby(by=['hit_id'])['ch0', 'ch1', 'value'].agg(
 ['mean', 'median', 'max', 'min', 'sum'] ).reset_index()
cells.columns = ['hit_id'] + ['-'.join([c2, c1])for c1 in [
'mean', 'median', 'max', 'min', 'sum']
for c2 in ['ch0', 'ch1', 'value']]
hits = pd.merge(hits, cells, how='left', on='hit_id')
 tcols = list(truth.columns)
 hits = pd.merge(hits, truth, how='left', on='hit_id')
 hits = norm_points(hits)
 cols = [c for c in hits.columns if c not in [
'event_id', 'hit_id', 'particle_id'] + tcols]
hits['target'] = hits['particle_id'].map(lambda x: 0 if x == 0 else 1)
 return hits[cols], hits['target'].values
def norm_points(df, g=0.7):
x = df.x.values
y = df.y.values
z = df.z.values
r = np.sqrt(x**2 + y**2 + z**2)
 df['x2'] = x / r
df['y2'] = y / r
r = np.sqrt(x**2 + y**2)
 df['z2'] =(z / r)* g
df['r'] = r
return df 
<train_model>
test_df = pd.read_csv("/kaggle/input/Kannada-MNIST/test.csv" )
<choose_model_class>
train_df['label'].value_counts()
scl = preprocessing.StandardScaler()
dbscan = hdbscan.HDBSCAN(min_samples=3,
min_cluster_size=5,
cluster_selection_method='leaf',
prediction_data=False,
metric='braycurtis' )<categorify>
import keras
import sklearn
from keras.preprocessing import image
from keras.models import Sequential, Model
from keras.layers import Dense, Flatten, BatchNormalization, Activation, Dropout, LeakyReLU, Input, Conv2D, MaxPooling2D
from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau
from keras import optimizers
from keras.layers.merge import concatenate
from keras import utils
from keras.utils import plot_model
from sklearn.model_selection import train_test_split
def create_one_event_submission(event_id, hits, labels):
sub_data = np.column_stack(( [event_id]*len(hits), hits.hit_id.values, labels))
submission = pd.DataFrame(data=sub_data, columns=["event_id", "hit_id", "track_id"] ).astype(int)
 return submission<compute_test_metric>
BATCH_SIZE = 64
NP_EPOCHS = 50
NP_CLASSES = 10
VALIDATION_SPLIT = 0.20
OPTIMIZER = optimizers.RMSprop()
submission = create_one_event_submission(0, hits, labels)
 score = score_event(truth, submission)
 print("Your score: ", score )<load_from_csv>
train = train_df.drop('label', axis=1)
 target = train_df['label']
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<count_missing_values>
test = test_df.drop('id', axis=1 )
sum(train.isna().any() )<concatenate>
train /= 255
df = pd.concat([train, test], sort=False )<import_modules>
test /= 255
from sklearn.feature_selection import VarianceThreshold
from sklearn.preprocessing import StandardScaler<train_on_grid>
target = utils.to_categorical(target, NP_CLASSES )
def get_best_param(x, y, modelo, params):
grid = GridSearchCV(estimator=modelo, param_grid=params, cv=3, n_jobs=-1)
 grid_result = grid.fit(x, y)
 return(grid_result.best_score_, grid_result.best_params_)
 def testa_tudo(train, alvo):

 start_time = time.time()
melhores = dict()
models = {
'QuadraticDiscriminantAnalysis': QuadraticDiscriminantAnalysis() ,
}
params = {
'QuadraticDiscriminantAnalysis': {'tol': [1,0.1,0.01,0.001,0.0001,0.00001]}
}
for nome, modelo in models.items() :
param = params[nome]
melhores[nome] = get_best_param(train, alvo, modelo, param)
 print("Modelo: %s, melhor classificação: %f usando %s" %(nome, melhores[nome][0], melhores[nome][1]))
print("Tempo rodando: " + str(( time.time() - start_time)) + ' ms')
 return melhores<train_on_grid>
NP_OUTPUT_FUNC = "softmax"
NP_DESCRIMINATOR_FUNC = "sigmoid"
NP_DROPOUT_FIRST = 0.15
NP_DROPOUT_SECOND = 0.10
def qual_melhor(melhores):
melhor = {'score': 0, 'nome': '', 'params': None}
for nome, res in melhores.items() :
if res[0] > melhor['score']:
melhor['score'] = res[0]
melhor['nome'] = nome
melhor['params'] = res[1]
return melhor
def roda_melhor(melhor, train, target, test):
models = {
'QuadraticDiscriminantAnalysis': QuadraticDiscriminantAnalysis() ,
}
modelo = models[melhor['nome']].set_params(**melhor['params'])
 modelo.fit(train, target)
 return modelo.predict_proba(test)
<prepare_x_and_y>
left_input = Input(shape=input_shape)
 x = left_input
NP_FILTERS = 32
for i in range(3):
x = Conv2D(filters=NP_FILTERS, kernel_size=NP_KERNEL_SIZE, padding="same" )(x)
 x = LeakyReLU(alpha=0.15 )(x)
 x = BatchNormalization(axis=-1, momentum=0.9, epsilon=0.001, center=True,
scale=True, beta_initializer="zeros", gamma_initializer="ones",
moving_mean_initializer="zeros", moving_variance_initializer="one",
beta_regularizer=None, gamma_regularizer=None,
beta_constraint=None, gamma_constraint=None )(x)
 x = Conv2D(filters=NP_FILTERS, kernel_size=NP_KERNEL_SIZE )(x)
 x = LeakyReLU(alpha=0.15 )(x)
 x = BatchNormalization(axis=-1, momentum=0.9, epsilon=0.001, center=True,
scale=True, beta_initializer="zeros", gamma_initializer="ones",
moving_mean_initializer="zeros", moving_variance_initializer="one",
beta_regularizer=None, gamma_regularizer=None,
beta_constraint=None, gamma_constraint=None )(x)
 x = Dropout(NP_DROPOUT_FIRST )(x)
 x = MaxPooling2D()(x)
 NP_FILTERS *= 2
size_x = train.shape[0]
size_y = test.shape[0] 
<define_variables>
right_input = Input(shape=input_shape)
 y = right_input
NP_FILTERS = 32
for i in range(3):
y = Conv2D(filters=NP_FILTERS, kernel_size=NP_KERNEL_SIZE, padding="same" )(y)
 y = LeakyReLU(alpha=0.15 )(y)
 y = BatchNormalization(axis=-1, momentum=0.9, epsilon=0.001, center=True,
scale=True, beta_initializer="zeros", gamma_initializer="ones",
moving_mean_initializer="zeros", moving_variance_initializer="one",
beta_regularizer=None, gamma_regularizer=None,
beta_constraint=None, gamma_constraint=None )(y)
 y = Conv2D(filters=NP_FILTERS, kernel_size=NP_KERNEL_SIZE )(y)
 y = LeakyReLU(alpha=0.15 )(y)
 y = BatchNormalization(axis=-1, momentum=0.9, epsilon=0.001, center=True,
scale=True, beta_initializer="zeros", gamma_initializer="ones",
moving_mean_initializer="zeros", moving_variance_initializer="one",
beta_regularizer=None, gamma_regularizer=None,
beta_constraint=None, gamma_constraint=None )(y)
 y = Dropout(NP_DROPOUT_FIRST )(y)
 y = MaxPooling2D()(y)
 NP_FILTERS *= 2
cols = [c for c in df.columns if c not in ['id', 'target', 'wheezy-copper-turtle-magic']]
oof = np.zeros(len(train))
preds = np.zeros(len(test))
for i in range(df['wheezy-copper-turtle-magic'].max() +1):
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)
 df2 = pd.concat([train2[cols], test2[cols]])
 df2 = VarianceThreshold(threshold=1.5 ).fit_transform(df2[cols])
 train3 = df2[:train2.shape[0]]
test3 = df2[train2.shape[0]:]
skf = StratifiedKFold(n_splits=10, random_state=666)
 for train_index, test_index in skf.split(train2, train2['target']):
clf = QuadraticDiscriminantAnalysis(tol=1)
 clf.fit(train3[train_index,:],train2.loc[train_index]['target'])
 oof[idx1[test_index]] = clf.predict_proba(train3[test_index,:])[:,1]
preds[idx2] += clf.predict_proba(test3)[:,1] / skf.n_splits
auc = roc_auc_score(train['target'], oof)
 print(f'AUC: {auc:.5}' )<feature_engineering>
network = concatenate([x, y] )
test['target'] = preds
oof = np.zeros(len(train))
preds = np.zeros(len(test))
for k in range(512):
train2 = train[train['wheezy-copper-turtle-magic']==k]
train2p = train2.copy() ; idx1 = train2.index
test2 = test[test['wheezy-copper-turtle-magic']==k]
test2p = test2[(test2['target']<=0.01)|(test2['target']>=0.99)].copy()
test2p.loc[ test2p['target']>=0.5, 'target' ] = 1
test2p.loc[ test2p['target']<0.5, 'target' ] = 0
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 = StratifiedKFold(n_splits=11, random_state=42, shuffle=True)
 for train_index, test_index in skf.split(train3p, train2p['target']):
test_index3 = test_index[ test_index<len(train3)]
clf = QuadraticDiscriminantAnalysis(reg_param=0.5)
 clf.fit(train3p[train_index,:],train2p.loc[train_index]['target'])
 oof[idx1[test_index3]] = clf.predict_proba(train3[test_index3,:])[:,1]
preds[test2.index] += clf.predict_proba(test3)[:,1] / skf.n_splits
auc = roc_auc_score(train['target'],oof)
 print('Pseudo Labeled QDA scores CV =',round(auc,5))<save_to_csv>
network = Flatten()(network)
 network = Dense(512 )(network)
 network = LeakyReLU(alpha=0.10 )(network)
 network = BatchNormalization(axis=-1, momentum=0.9, epsilon=0.001, center=True, scale=True, beta_initializer="zeros",
gamma_initializer="ones", moving_mean_initializer="zeros",
moving_variance_initializer="one", beta_regularizer=None, gamma_regularizer=None,
beta_constraint=None, gamma_constraint=None )(network)
 network = Dropout(NP_DROPOUT_SECOND )(network)
 network = Dense(256 )(network)
 network = LeakyReLU(alpha=0.10 )(network)
 network = BatchNormalization(axis=-1, momentum=0.9, epsilon=0.001, center=True,
scale=True, beta_initializer="zeros", gamma_initializer="ones",
moving_mean_initializer="zeros", moving_variance_initializer="one",
beta_regularizer=None, gamma_regularizer=None,
beta_constraint=None, gamma_constraint=None )(network)
 network = Dropout(NP_DROPOUT_SECOND )(network)
 output = Dense(NP_CLASSES )(network)
 output = Activation(NP_OUTPUT_FUNC )(output )
sub = pd.read_csv('.. /input/sample_submission.csv')
 sub['target'] = preds
sub.to_csv('submission.csv',index=False )<import_modules>
model = Model([left_input, right_input], output )
from sklearn.svm import SVC, NuSVC, LinearSVC
from sklearn.linear_model import Ridge, LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis, LinearDiscriminantAnalysis
from sklearn.neural_network import MLPClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier, GradientBoostingClassifier
from sklearn.naive_bayes import GaussianNB
import sklearn.gaussian_process.kernels as ker
from sklearn.feature_selection import VarianceThreshold
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import roc_auc_score
import numpy as np, pandas as pd, os, gc
from tqdm import tqdm_notebook
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.preprocessing import PolynomialFeatures
from sklearn.covariance import EmpiricalCovariance
from sklearn.covariance import GraphicalLasso
from sklearn.mixture import GaussianMixture<data_type_conversions>
optimizer = optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=1e-06, decay=0.0 )
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<load_from_csv>
model.compile(
 optimizer=optimizer,
loss="categorical_crossentropy",
metrics=["categorical_accuracy"]
)
print('Reading Train Data...')
 train = pd.read_csv('.. /input/train.csv')
 print('Reading Test Data...')
 test = pd.read_csv('.. /input/test.csv')
 print('Finish Reading.' )<define_variables>
X_train, X_val, y_train, y_val = train_test_split(train, target, test_size=0.20, random_state=123456789 )
n_folds = 11<define_variables>
generator = image.ImageDataGenerator(
 featurewise_center=True,
featurewise_std_normalization=True,
rotation_range=10,
zoom_range=0.1,
width_shift_range=0.1,
height_shift_range=0.1,
vertical_flip=False,
horizontal_flip=False )
oof_GMM = np.zeros(len(train))
preds_GMM = np.zeros(len(test))
cols = [c for c in train.columns if c not in ['id', 'target', 'wheezy-copper-turtle-magic']]
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=n_folds, random_state=42)
 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[train_index,:],test3],axis = 0))
oof_GMM[idx1[test_index]] = gm.predict_proba(train3[test_index,:])[:,0]
preds_GMM[idx2] += gm.predict_proba(test3)[:,0] / skf.n_splits
if i%64==0:
print(i, 'GMM oof auc : ', round(roc_auc_score(train['target'][idx1], oof_GMM[idx1]), 5))<compute_test_metric>
num_train_sequences = len(X_train)
 num_val_sequences = len(X_val)
 print("
print("
auc_GMM = roc_auc_score(train['target'],oof_GMM)
 print('GMM scores CV: ',round(auc_GMM, 5))<define_variables>
if(num_train_sequences % BATCH_SIZE)== 0:
steps_per_epoch = int(num_train_sequences / BATCH_SIZE)
 else:
steps_per_epoch = int(num_train_sequences / BATCH_SIZE)+ 1
if(num_val_sequences % BATCH_SIZE)== 0:
validation_steps = int(num_val_sequences / BATCH_SIZE)
 else:
validation_steps = int(num_val_sequences / BATCH_SIZE)+ 1
print("
print("
test['target'] = preds_GMM
oof_QDA = np.zeros(len(train))
preds_QDA = np.zeros(len(test))
oof_NuSVC = np.zeros(len(train))
preds_NuSVC = np.zeros(len(test))
oof_KNN = np.zeros(len(train))
preds_KNN = np.zeros(len(test))
oof_MLP = np.zeros(len(train))
preds_MLP = 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 = test[test['wheezy-copper-turtle-magic']==k]
idx2 = test2.index
test2p = test2[(test2['target']<=0.01)|(test2['target']>=0.99)].copy()
test2p.loc[ test2p['target']>=0.5, 'target' ] = 1
test2p.loc[ test2p['target']<0.5, 'target' ] = 0
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])
 pca = PCA(svd_solver='full',n_components='mle' ).fit(train2p[cols])
 train4p = pca.transform(train2p[cols])
 train4 = pca.transform(train2[cols])
 test4 = pca.transform(test2[cols])
 sc1 = StandardScaler()
train4p = sc1.fit_transform(train4p)
 train4 = sc1.transform(train4)
 test4 = sc1.transform(test4)
 poly = PolynomialFeatures().fit(train3p)
 train5p = poly.transform(train3p)
 train5 = poly.transform(train3)
 test5 = poly.transform(test3)
 sc2 = StandardScaler()
train5p = sc2.fit_transform(train5p)
 train5 = sc2.transform(train5)
 test5 = sc2.transform(test5)
 skf = StratifiedKFold(n_splits=11, random_state=42, shuffle=True)
 for train_index, test_index in skf.split(train3p, train2p['target']):
test_index3 = test_index[ test_index<len(train3)]
clf_QDA = QuadraticDiscriminantAnalysis(reg_param=0.5)
 clf_QDA.fit(train3p[train_index,:],train2p.loc[train_index]['target'])
 oof_QDA[idx1[test_index3]] = clf_QDA.predict_proba(train3[test_index3,:])[:,1]
preds_QDA[idx2] += clf_QDA.predict_proba(test3)[:,1] / skf.n_splits
clf_NuSVC = NuSVC(probability=True,kernel='poly',degree=4,gamma='auto',nu=0.59, coef0=0.053)
 clf_NuSVC.fit(train4p[train_index,:],train2p.loc[train_index]['target'])
 oof_NuSVC[idx1[test_index3]] = clf_NuSVC.predict_proba(train4[test_index3,:])[:,1]
preds_NuSVC[idx2] += clf_NuSVC.predict_proba(test4)[:,1] / skf.n_splits
clf_KNN = KNeighborsClassifier(n_neighbors=10,weights='distance',p=2)
 clf_KNN.fit(train3p[train_index,:],train2p.loc[train_index]['target'])
 oof_KNN[idx1[test_index3]] = clf_KNN.predict_proba(train3[test_index3,:])[:,1]
preds_KNN[idx2] += clf_KNN.predict_proba(test3)[:,1] / skf.n_splits
clf_MLP = MLPClassifier(activation='relu', solver='lbfgs', tol=1e-06, hidden_layer_sizes=(250,), random_state=42)
 clf_MLP.fit(train5p[train_index,:],train2p.loc[train_index]['target'])
 oof_MLP[idx1[test_index3]] = clf_MLP.predict_proba(train5[test_index3,:])[:,1]
preds_MLP[idx2] += clf_MLP.predict_proba(test5)[:,1] / skf.n_splits
if k%64==0:
print(k, 'QDA oof auc : ', round(roc_auc_score(train['target'][idx1], oof_QDA[idx1]), 5))
print(k, 'NuSVC oof auc : ', round(roc_auc_score(train['target'][idx1], oof_NuSVC[idx1]), 5))
print(k, 'KNN oof auc : ', round(roc_auc_score(train['target'][idx1], oof_KNN[idx1]), 5))
print(k, 'MLP oof auc : ', round(roc_auc_score(train['target'][idx1], oof_MLP[idx1]), 5))<compute_test_metric>
LR = ReduceLROnPlateau(monitor="val_loss", factor=0.2, patience=2, min_lr=0.000001, verbose=1, cooldown=1 )
auc_QDA = roc_auc_score(train['target'],oof_QDA)
 print('Pseudo Labeled QDA scores CV: ',round(auc_QDA,5))
auc_NuSVC = roc_auc_score(train['target'],oof_NuSVC)
 print('Pseudo Labeled NuSVC scores CV: ',round(auc_NuSVC,5))
auc_KNN = roc_auc_score(train['target'],oof_KNN)
 print('Pseudo Labeled KNN scores CV: ',round(auc_KNN,5))
auc_MLP = roc_auc_score(train['target'],oof_MLP)
 print('Pseudo Labeled MLP scores CV: ',round(auc_MLP,5))<create_dataframe>
def exponential_decay_fn(epoch):
return 0.01 * 0.1**(epoch / 20 )
train_new = pd.DataFrame(np.concatenate(( oof_NuSVC.reshape(-1,1),oof_KNN.reshape(-1,1),oof_QDA.reshape(-1,1),oof_MLP.reshape(-1,1)) , axis=1))
test_new = pd.DataFrame(np.concatenate(( preds_NuSVC.reshape(-1,1),preds_KNN.reshape(-1,1),preds_QDA.reshape(-1,1),preds_MLP.reshape(-1,1)) , axis=1))<define_variables>
def exponential_decay(lr, s):
def exponential_decay_fn(epoch):
return lr * 0.1**(epoch / s)
 return exponential_decay_fn
exponential_decay_fn = exponential_decay(lr=0.001, s=20 )
param = {
'bagging_freq': 3,
'bagging_fraction': 0.8,
'boost_from_average':'False',
'boost': 'gbdt',
'feature_fraction': 1,
'learning_rate': 0.05,
'max_depth': 10,
'metric':'auc',
'min_data_in_leaf': 82,
'min_sum_hessian_in_leaf': 10.0,
'num_leaves': 10,
'objective': 'binary',
'verbosity': 1,
'seed': 42
}
N = 5
skf_lgb = StratifiedKFold(n_splits=N, random_state=42)
 oof_lgb = np.zeros(train_new.shape[0])
 pred_stack = np.zeros(len(test_new))
for fold_,(trn_idx, val_idx)in enumerate(skf_lgb.split(train_new, train['target'])) :
print("Fold {}".format(fold_+1))
x_train, y_train = train_new.iloc[trn_idx], train['target'].iloc[trn_idx]
x_val, y_val = train_new.iloc[val_idx], train['target'].iloc[val_idx]
x_train.head()
trn_data = lgb.Dataset(x_train, label=y_train)
 val_data = lgb.Dataset(x_val, label=y_val)
 classifier = lgb.train(param, trn_data, 100000, valid_sets = [trn_data, val_data], verbose_eval=200, early_stopping_rounds = 200)
 val_pred = classifier.predict(x_val, num_iteration=classifier.best_iteration)
 oof_lgb[val_idx] = val_pred
pred_stack += classifier.predict(test_new, num_iteration=classifier.best_iteration)/ N
print(roc_auc_score(y_val, val_pred))
auc_lgb = roc_auc_score(train['target'],oof_lgb)
 print('LGB auc: ',round(auc_lgb,5))<save_to_csv>
lr_scheduler = keras.callbacks.LearningRateScheduler(exponential_decay_fn )
submission_stack = pd.read_csv(".. /input/sample_submission.csv")
 submission_stack['target'] = pred_stack
submission_stack.head()
submission_stack.to_csv('submission.csv', index=False )<save_to_csv>
callbacks = [
checkpoint,
LR,
lr_scheduler]
train_new.to_csv('train_new.csv', index=False)
 test_new.to_csv('test_new.csv', index=False )<set_options>
VERBOSE = 1
history = model.fit_generator(generator.flow([X_train, X_train], y_train, batch_size=BATCH_SIZE),
validation_data=generator.flow([X_val, X_val], y_val, batch_size=BATCH_SIZE),
epochs=NP_EPOCHS,
verbose=VERBOSE,
steps_per_epoch=steps_per_epoch,
validation_steps=validation_steps,
class_weight=None,
initial_epoch=0,
callbacks=callbacks )
warnings.filterwarnings('ignore')
<load_from_csv>
values = {}
models = os.listdir(model_name)
 for model in models:
converted = model.replace(".h5", "")
 accuracy = float(converted.split("-")[-1])
 values.update({accuracy: model})
 key = max(values, key = values.get)
 best = values.get(key)
 print("Best model found: {}".format(best))
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 train['wheezy-copper-turtle-magic'] = train['wheezy-copper-turtle-magic'].astype('category')
 test['wheezy-copper-turtle-magic'] = test['wheezy-copper-turtle-magic'].astype('category' )<save_to_csv>
model_path = model_name + best
print("Absolute path found: {}".format(model_path))
magicNum = 131073
default_cols = [c for c in train.columns if c not in ['id', 'target','target_pred', 'wheezy-copper-turtle-magic']]
cols = [c for c in default_cols]
sub = pd.read_csv('.. /input/sample_submission.csv')
 sub.to_csv('submission.csv',index=False)
 train.shape,test.shape<compute_train_metric>
model = load_model(model_path )
y_perfect = [0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1]
y_flliped = [1,0,0,1,0,0,0,0,0,0,1,1,1,0,1,1,1,1,0,1]
roc_auc_score(y_perfect,y_flliped )<compute_train_metric>
predictions = model.predict([test, test] )
y_preds = [0.33,0.33,0.33,0.5,0.5,0,0,0,0,0,1,1,0.5,0.5,1,1,1,0.66,0.66,0.66]
roc_auc_score(y_flliped,y_preds )<prepare_x_and_y>
conversion = np.argmax(predictions, axis=1 )
if sub.shape[0] == magicNum:
[].shape
preds=np.zeros(len(test))
train_err=np.zeros(512)
 test_err=np.zeros(512)
 for i in range(512):
X = train[train['wheezy-copper-turtle-magic']==i].copy()
Y = X.pop('target' ).values
X_test = test[test['wheezy-copper-turtle-magic']==i].copy()
idx_train = X.index
idx_test = X_test.index
X.reset_index(drop=True,inplace=True)
 X = X[cols].values
X_test = X_test[cols].values
vt = VarianceThreshold(threshold=2 ).fit(X)
 X = vt.transform(X)
 X_test = vt.transform(X_test)
 X_all = np.concatenate([X,X_test])
 train_size = len(X)
 test1_size = test[:131073][test[:131073]['wheezy-copper-turtle-magic']==i].shape[0]
compo_cnt = 6
for ii in range(30):
gmm = GaussianMixture(n_components=compo_cnt,init_params='random',covariance_type='full',max_iter=100,tol=1e-10,reg_covar=0.0001 ).fit(X_all)
 labels = gmm.predict(X_all)
 cntStd = np.std([len(labels[labels==j])for j in range(compo_cnt)])
 if round(cntStd,4)== 0.4714:
check_labels = labels[:train_size]
cvt_labels=np.zeros(len(labels))
for iii in range(compo_cnt):
mean_val = Y[check_labels==iii].mean()
mean_val = 1 if mean_val > 0.5 else 0
cvt_labels[labels==iii] = mean_val
train_err[i] = len(Y[Y != cvt_labels[:train_size]])
 if(train_err[i] >= 10)and(train_err[i] <= 15):
train_err[i] = 12.5
exp_err = max(0,(25 - train_err[i])/(train_size + test1_size))
for iii in range(compo_cnt):
mean_val = Y[check_labels==iii].mean()
mean_val =(1-exp_err)if mean_val > 0.5 else exp_err
cvt_labels[labels==iii] = mean_val
preds[idx_test] = cvt_labels[train_size:]
break
sub['target'] = preds
sub.to_csv('submission.csv',index=False )<define_variables>
print("No.f Id's available in the test dataset: {}".format(len(test_df['id'])))
 print("No.f predictions done by the model on test data: {}".format(len(result)) )
np.random.seed(42)
 x = np.random.choice(np.arange(30),2)
 print('We will submit versions',x[0],'and',x[1])
 np.random.seed(None )<import_modules>
submission = pd.concat([test_df['id'], result],axis = 1 )
import numpy as np, pandas as pd, os
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
from sklearn.mixture import GaussianMixture
from sklearn.model_selection import StratifiedKFold
from sklearn.feature_selection import VarianceThreshold
from sklearn.metrics import roc_auc_score
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification<load_from_csv>
submission.to_csv("submission.csv", index=False )