Kaggle competition: Kannada MNIST
<SOS> metric: CategorizationAccuracy Kaggle data source: kannada-mnist<feature_engineering>
import numpy as np
import pandas as pd
concat_sub['m_median'] = concat_sub.iloc[:, 1:].median(axis=1)
 concat_sub['m_max'] = concat_sub.iloc[:, 1:].max(axis=1)
 concat_sub['m_min'] = concat_sub.iloc[:, 1:].min(axis=1 )<define_variables>
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
tf.keras.backend.set_session(tf.Session(config=config))
cutoff_lo = 0.8
cutoff_hi = 0.2<feature_engineering>
from keras.layers import LeakyReLU
from keras.layers.normalization import BatchNormalization
rank = np.tril(corr.values,-1)
 rank[rank<0.92] = 1
m =(rank>0 ).sum() -(rank>0.97 ).sum()
m_gmean, s = 0, 0
for n in range(m):
mx = np.unravel_index(rank.argmin() , rank.shape)
 w =(m-n)/m
m_gmean += w*(np.log(concat_sub.iloc[:,mx[0]])+np.log(concat_sub.iloc[:,mx[1]])) /2
s += w
rank[mx] = 1
m_gmean = np.exp(m_gmean/s )<save_to_csv>
train = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')
 test = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')
 sample_sub = pd.read_csv('/kaggle/input/Kannada-MNIST/sample_submission.csv')
 dig = pd.read_csv('/kaggle/input/Kannada-MNIST/Dig-MNIST.csv' )
concat_sub['Label'] = m_gmean
concat_sub['m_mean'] = m_gmean
concat_sub['ID'] = concat_sub.index
concat_sub[['ID', 'Label']].to_csv('rsna_simple_gmean.csv', index=False )<save_to_csv>
x_train = train.values[:,1:].reshape(train.shape[0], 28, 28, 1)
 x_train = x_train.astype('float32')
 x_train = x_train / 255.0
y_train = train.values[:,0]
x_train, x_valid, y_train, y_valid = train_test_split(x_train, y_train, test_size = 0.1, random_state=2019)
 x_test = test.values[:,1:].reshape(test.shape[0], 28, 28, 1)
 x_test = x_test.astype('float32')
 x_test = x_test / 255.0
concat_sub['Label'] = concat_sub['m_median']
concat_sub[['ID', 'Label']].to_csv('stack_median.csv',
index=False, float_format='%.6f' )<save_to_csv>
lb = preprocessing.LabelBinarizer()
y_train = lb.fit_transform(y_train)
 y_valid = lb.fit_transform(y_valid )
concat_sub['Label'] = np.where(np.all(concat_sub.iloc[:,1:6] > cutoff_lo, axis=1), 1,
np.where(np.all(concat_sub.iloc[:,1:6] < cutoff_hi, axis=1),
0, concat_sub['m_median']))
concat_sub[['ID', 'Label']].to_csv('stack_pushout_median.csv',
index=False, float_format='%.6f' )<feature_engineering>
modelo = Sequential()
modelo.add(Conv2D(64,(3,3), padding='same', input_shape=(28, 28, 1)))
 modelo.add(BatchNormalization(momentum=0.5, epsilon=1e-5, gamma_initializer="uniform"))
modelo.add(LeakyReLU(alpha=0.1))
modelo.add(Conv2D(128,(3,3), padding='same'))
modelo.add(BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer="uniform"))
modelo.add(LeakyReLU(alpha=0.1))
modelo.add(MaxPooling2D(2, 2))
modelo.add(Dropout(0.2))
modelo.add(Conv2D(128,(3,3), padding='same'))
modelo.add(BatchNormalization(momentum=0.2, epsilon=1e-5, gamma_initializer="uniform"))
modelo.add(LeakyReLU(alpha=0.1))
modelo.add(Conv2D(256,(3,3), padding='same'))
modelo.add(BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer="uniform"))
modelo.add(LeakyReLU(alpha=0.1))
modelo.add(MaxPooling2D(2, 2))
modelo.add(Dropout(0.2))
modelo.add(Conv2D(256,(3,3), padding='same'))
modelo.add(BatchNormalization(momentum=0.2, epsilon=1e-5, gamma_initializer="uniform"))
modelo.add(LeakyReLU(alpha=0.1))
modelo.add(Conv2D(256,(3,3), padding='same'))
modelo.add(BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer="uniform"))
modelo.add(LeakyReLU(alpha=0.1))
modelo.add(MaxPooling2D(2,2))
modelo.add(Dropout(0.2))
modelo.add(Flatten())
 modelo.add(Dense(256))
modelo.add(LeakyReLU(alpha=0.1))
modelo.add(BatchNormalization())
 modelo.add(Dense(10, activation='softmax'))
concat_sub['Label'] = np.where(np.all(concat_sub.iloc[:,1:6] > cutoff_lo, axis=1),
concat_sub['m_max'],
np.where(np.all(concat_sub.iloc[:,1:6] < cutoff_hi, axis=1),
concat_sub['m_min'],
concat_sub['m_mean']))
concat_sub[['ID', 'Label']].to_csv('stack_minmax_mean.csv',
index=False, float_format='%.6f' )<feature_engineering>
epochs=50
batch_size=200
history = modelo.fit(x_train, y_train,
epochs=epochs,
batch_size= batch_size,
validation_data=(x_valid, y_valid))
concat_sub['Label'] = np.where(np.all(concat_sub.iloc[:,1:6] > cutoff_lo, axis=1),
concat_sub['m_max'],
np.where(np.all(concat_sub.iloc[:,1:6] < cutoff_hi, axis=1),
concat_sub['m_min'],
concat_sub['m_median']))
concat_sub[['ID', 'Label']].to_csv('stack_minmax_median.csv',
index=False, float_format='%.6f' )<load_from_csv>
scores = modelo.predict(x_test)
 score_classes = np.argmax(scores, axis = 1)
 x_dig=dig.drop('label',axis=1 ).iloc[:,:].values
print(x_dig.shape)
 x_dig = x_dig.reshape(x_dig.shape[0], 28, 28,1)
 print(x_dig.shape)
 y_dig=dig.label
print(y_dig.shape)
 preds_dig=modelo.predict_classes(x_dig/255)
 metrics.accuracy_score(preds_dig, y_dig )
ll = 6
df_suball = []
for i in range(0,ll):
df_suball.append(0)
 for i in range(0,ll):
df_suball[i] = pd.read_csv(glob.glob(".. /input/alltogether/*.csv")[i] )<filter>
output = pd.DataFrame({'id': sample_sub['id'],
'label': score_classes})
 output.head()
df_suball[0].loc[df_suball[0]['ID'] == 'ID_000012eaf_any'] 
<filter>
output.to_csv('submission.csv', index=False )