Kaggle competition: Kannada MNIST
<SOS> metric: CategorizationAccuracy Kaggle data source: kannada-mnist<compute_test_metric>
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
sigma_opt = mean_absolute_error(y, pred[:, 1])
 unc = pred[:,2] - pred[:, 0]
sigma_mean = np.mean(unc)
 print(sigma_opt, sigma_mean )<prepare_output>
BASE_PATH = '/kaggle/input/Kannada-MNIST/'
np.random.seed(1973 )
sub['FVC1'] = pe[:, 1]
sub['Confidence1'] = pe[:, 2] - pe[:, 0]<prepare_output>
train_src_df = pd.read_csv(filepath_or_buffer=BASE_PATH+'train.csv')
 test_src_df = pd.read_csv(filepath_or_buffer=BASE_PATH+'test.csv')
 dm_src_df = pd.read_csv(filepath_or_buffer=BASE_PATH+'Dig-MNIST.csv')
 submission = pd.read_csv(filepath_or_buffer=BASE_PATH+'sample_submission.csv' )
subm = sub[['Patient_Week','FVC','Confidence','FVC1','Confidence1']].copy()<feature_engineering>
train_datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
zca_epsilon=1e-06,
rotation_range=15,
width_shift_range=0.2,
height_shift_range=0.2,
brightness_range=[0.7,1.0],
shear_range=0.2,
zoom_range=0.2,
channel_shift_range=0.0,
fill_mode='constant',
cval=0.0,
horizontal_flip=False,
vertical_flip=False,
rescale=1./255,
preprocessing_function=None,
data_format='channels_last',
validation_split=0.0,
dtype='float32')
 test_datagen = ImageDataGenerator(rescale=1./255)
 train_datagen.fit(X_train)
subm.loc[~subm.FVC1.isnull() ,'FVC'] = subm.loc[~subm.FVC1.isnull() ,'FVC1']
if sigma_mean<70:
subm['Confidence'] = sigma_opt
else:
subm.loc[~subm.FVC1.isnull() ,'Confidence'] = subm.loc[~subm.FVC1.isnull() ,'Confidence1']<save_to_csv>
def gelu(x):

 cdf = 0.5 *(1.0 + tf.tanh(
(np.sqrt(2 / np.pi)*(x + 0.044715 * tf.pow(x, 3)))))
 return x * cdf
subm[["Patient_Week","FVC","Confidence"]].to_csv("submission.csv", index=False )<train_model>
def create_model(fig_size=28, channel_num=1, class_num=10, activation=relu):
model = Sequential()
model.add(Conv2D(64,(5, 5), padding='same', activation=activation, input_shape=(fig_size, fig_size, channel_num)))
 model.add(Dropout(0.5))
model.add(Conv2D(64,(5, 5), padding='same', activation=activation))
model.add(Dropout(0.5))
model.add(MaxPooling2D(2))
model.add(Conv2D(128,(3, 3), padding='same', activation=activation))
model.add(Dropout(0.5))
model.add(Conv2D(128,(3, 3), padding='same', activation=activation))
model.add(Dropout(0.5))
model.add(MaxPooling2D(2))
model.add(Conv2D(256,(3, 3), padding='same', activation=activation))
model.add(Dropout(0.5))
model.add(Conv2D(256,(3, 3), padding='same', activation=activation))
model.add(Dropout(0.5))
model.add(Conv2D(256,(3, 3), padding='same', activation=activation))
model.add(Dropout(0.5))
model.add(MaxPooling2D(2))
model.add(Conv2D(1024,(1, 1), activation=activation))
model.add(Dropout(0.5))
model.add(DepthwiseConv2D(( 3, 3), padding='valid', activation=activation))
model.add(Dropout(0.5))
model.add(Flatten())
 model.add(Dense(512, activation=activation))
model.add(Dropout(0.5))
model.add(Dense(512, activation=activation))
model.add(Dropout(0.5))
model.add(Dense(class_num, activation='softmax'))
model.compile(optimizer=Adam() ,
loss='categorical_crossentropy',
metrics=['accuracy'])
 return model
<set_options>
rlr = ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=3, min_lr=1.0e-8, verbose=1)
 mcp = ModelCheckpoint(filepath='weights.hdf5', verbose=1, save_best_only=True )
%matplotlib inline
roc_curve, recall_score, classification_report, f1_score,
precision_recall_fscore_support )<load_from_csv>
BATCH_SIZE = 64
EPOCH_NUM = 30
train = pd.read_csv('.. /input/train.csv')
 train = pd.concat(( train, pd.read_csv('.. /input/train_v2.csv')) ,axis=0, ignore_index=True ).reset_index(drop=True)
 test = pd.read_csv('.. /input/sample_submission_v2.csv' )<load_from_csv>
history_relu = model_relu.fit_generator(
 train_datagen.flow(X_train, y_train, batch_size=BATCH_SIZE),
validation_data=test_datagen.flow(X_valid, y_valid, batch_size=BATCH_SIZE),
epochs=EPOCH_NUM,
callbacks=[rlr, mcp] 
)
 model_relu.load_weights('weights.hdf5' )
transactions = pd.read_csv('.. /input/transactions.csv', usecols=['msno'])
 transactions = pd.concat(( transactions, pd.read_csv('.. /input/transactions_v2.csv', usecols=['msno'])) , axis=0, ignore_index=True ).reset_index(drop=True)
 transactions = pd.DataFrame(transactions['msno'].value_counts().reset_index())
 transactions.columns = ['msno','trans_count']
train = pd.merge(train, transactions, how='left', on='msno')
 test = pd.merge(test, transactions, how='left', on='msno' )<merge>
history_gelu = model_gelu.fit_generator(
 train_datagen.flow(X_train, y_train, batch_size=BATCH_SIZE),
validation_data=test_datagen.flow(X_valid, y_valid, batch_size=BATCH_SIZE),
epochs=EPOCH_NUM,
callbacks=[rlr, mcp] 
)
 model_gelu.load_weights('weights.hdf5' )
transactions = pd.read_csv('.. /input/transactions_v2.csv')
 transactions = transactions.sort_values(by=['transaction_date'], ascending=[False] ).reset_index(drop=True)
 transactions = transactions.drop_duplicates(subset=['msno'], keep='first')
 train = pd.merge(train, transactions, how='left', on='msno')
 test = pd.merge(test, transactions, how='left', on='msno')
 transactions=[]<merge>
print('val_acc
relu:{}
gelu:{}
val_loss
relu:{}
gelu:{}'.format(max(history_relu.history['val_acc']), max(history_gelu.history['val_acc']), min(history_relu.history['val_loss']), min(history_gelu.history['val_loss'])) )
user_logs = pd.read_csv('.. /input/user_logs_v2.csv', usecols=['msno'])
 user_logs = pd.DataFrame(user_logs['msno'].value_counts().reset_index())
 user_logs.columns = ['msno','logs_count']
train = pd.merge(train, user_logs, how='left', on='msno')
 test = pd.merge(test, user_logs, how='left', on='msno')
 user_logs = [];<remove_duplicates>
pred_ret = None
if min(history_relu.history['val_loss'])< min(history_gelu.history['val_loss']):
pred_ret = model_relu.predict_generator(test_datagen.flow(test_data, None, shuffle=False, batch_size=BATCH_SIZE))
else:
pred_ret = model_gelu.predict_generator(test_datagen.flow(test_data, None, shuffle=False, batch_size=BATCH_SIZE))
pred_ids = np.argmax(pred_ret, axis=1)
 pred_ids.shape
def transform_df(df):
df = pd.DataFrame(df)
 df = df.sort_values(by=['date'], ascending=[False])
 df = df.reset_index(drop=True)
 df = df.drop_duplicates(subset=['msno'], keep='first')
 return df
def transform_df2(df):
df = df.sort_values(by=['date'], ascending=[False])
 df = df.reset_index(drop=True)
 df = df.drop_duplicates(subset=['msno'], keep='first')
 return df<merge>
submission['label'] = pred_ids
submission.head()
last_user_logs = []
last_user_logs.append(transform_df(pd.read_csv('.. /input/user_logs_v2.csv')))
 last_user_logs = pd.concat(last_user_logs, axis=0, ignore_index=True ).reset_index(drop=True)
 last_user_logs = transform_df2(last_user_logs)
 print('merging user logs features...')
 train = pd.merge(train, last_user_logs, how='left', on='msno')
 test = pd.merge(test, last_user_logs, how='left', on='msno')
 last_user_logs=[]<merge>
submission.to_csv(path_or_buf='submission.csv', index=False )