Kaggle competition: Kannada MNIST
<SOS> metric: CategorizationAccuracy Kaggle data source: kannada-mnist<predict_on_test>
%matplotlib inline
Conv2D,
MaxPooling2D,
MaxPool2D,
Dropout,
BatchNormalization,
Flatten 
)
pred_val_y, pred_test_y, best_score = train_pred(model_cnn(embedding_matrix), epochs = 2)
 outputs.append([pred_val_y, pred_test_y, best_score, '2d CNN'] )<predict_on_test>
train_id = pd.read_csv('.. /input/Kannada-MNIST/train.csv')
 test_id = pd.read_csv('.. /input/Kannada-MNIST/test.csv')
 dig_id = pd.read_csv('.. /input/Kannada-MNIST/Dig-MNIST.csv' )
pred_val_y, pred_test_y, best_score = train_pred(model_cnn_flip(embedding_matrix), epochs = 2)
 outputs.append([pred_val_y, pred_test_y, best_score, '2d CNN flip'] )<predict_on_test>
x, y, z = 28, 28, 1
qtd_classes = 10
images = []
images_labels = []
valid = []
valid_labels = []
for count,(index, row)in enumerate(train_id.iterrows()):
images.append(row.values[1:].reshape(x, y, z))
images_labels.append(row.values[:1])
 for count,(index, row)in enumerate(dig_id.iterrows()):
valid.append(row.values[1:].reshape(x, y, z))
valid_labels.append(row.values[:1] )
pred_val_y, pred_test_y, best_score = train_pred(model_cnn(embedding_matrix_1), epochs = 2)
 outputs.append([pred_val_y, pred_test_y, best_score, '2d CNN GloVe'] )<predict_on_test>
TESTER = []
for count,(index, row)in enumerate(test_id.iterrows()):
TESTER.append(row.values[1:].reshape(x, y, z))
pred_val_y, pred_test_y, best_score = train_pred(model_cnn_flip(embedding_matrix_1), epochs = 2)
 outputs.append([pred_val_y, pred_test_y, best_score, '2d CNN GloVe flip'] )<predict_on_test>
SUBMIT = np.asarray(TESTER)/ 255.
 TEST = np.asarray(valid)/ 255.
 TRAIN = np.asarray(images)/ 255.
 TRAIN_labels = to_categorical(np.asarray(images_labels), qtd_classes)
 TEST_labels = to_categorical(np.asarray(valid_labels), qtd_classes )
pred_val_y, pred_test_y, best_score = train_pred(model_lstm_du(embedding_matrix), epochs = 2)
 outputs.append([pred_val_y, pred_test_y, best_score, 'LSTM DU'] )<predict_on_test>
train = np.concatenate([TEST, TRAIN])
 labels = np.concatenate([TRAIN_labels, TEST_labels] )
pred_val_y, pred_test_y, best_score = train_pred(model_lstm_atten(embedding_matrix), epochs = 3)
 outputs.append([pred_val_y, pred_test_y, best_score, '2 LSTM w/ attention'] )<predict_on_test>
x_train, x_test, y_train, y_test = train_test_split(train, labels, test_size = 0.3 )
pred_val_y, pred_test_y, best_score = train_pred(model_lstm_atten(embedding_matrix_1), epochs = 3)
 outputs.append([pred_val_y, pred_test_y, best_score, '2 LSTM w/ attention GloVe'] )<predict_on_test>
NUM_CLASSES = 10
EPOCHS = 15
BATCH_SIZE = 64
inputShape =(x, y, z )
pred_val_y, pred_test_y, best_score = train_pred(model_lstm_atten(embedding_matrix_3), epochs = 3)
 outputs.append([pred_val_y, pred_test_y, best_score, '2 LSTM w/ attention Para'] )<sort_values>
model = Sequential()
model.add(Conv2D(16, kernel_size = 3, padding="same", activation='relu', input_shape = inputShape))
model.add(Conv2D(32, kernel_size = 3, padding="same", activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(64, kernel_size = 3, padding="same", activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(128, kernel_size = 3, padding="same", activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(256, kernel_size = 3, padding="same", activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(NUM_CLASSES, activation='softmax'))
optmize = keras.optimizers.Adam(learning_rate=0.001, beta_1=0.9, beta_2=0.999, amsgrad=False)
 model.compile(loss = keras.losses.categorical_crossentropy, optimizer = optmize, metrics=['accuracy'])
 History = model.fit(x_train, y_train,
batch_size = BATCH_SIZE,
epochs = EPOCHS,
verbose = 1,
validation_data=(x_test, y_test))
outputs.sort(key=lambda x: x[2])
 weights = [i for i in range(1, len(outputs)+ 1)]
weights = [float(i)/ sum(weights)for i in weights]
print(weights )<train_model>
loss, accu = model.evaluate(x_test, y_test)
 print("%s: %.2f%%" %('Accuracy...', accu))
print("%s: %.2f" %('Loss...', loss))
X = np.asarray([outputs[i][0] for i in range(len(outputs)) ])
 X = X[...,0]
reg = LinearRegression().fit(X.T, val_y)
 print(reg.score(X.T, val_y),reg.coef_ )<find_best_params>
History = model.fit(train, labels,
batch_size = BATCH_SIZE,
epochs = 100,
verbose = 1 )
pred_val_y = np.sum([outputs[i][0] * reg.coef_[i] for i in range(len(outputs)) ], axis = 0)
 thresholds = []
for thresh in np.arange(0.1, 0.501, 0.01):
thresh = np.round(thresh, 2)
 res = metrics.f1_score(val_y,(pred_val_y > thresh ).astype(int))
thresholds.append([thresh, res])
 print("F1 score at threshold {0} is {1}".format(thresh, res))
thresholds.sort(key=lambda x: x[1], reverse=True)
 best_thresh = thresholds[0][0]
print("Best threshold: ", best_thresh )<save_to_csv>
results = model.predict(SUBMIT)
 results = np.argmax(results,axis = 1)
 data_out = pd.DataFrame({'id': range(len(SUBMIT)) , 'label': results} )
pred_test_y = np.sum([outputs[i][1] * reg.coef_[i] for i in range(len(outputs)) ], axis = 0)
 pred_test_y =(pred_test_y > best_thresh ).astype(int)
 test_df = pd.read_csv(".. /input/test.csv", usecols=["qid"])
 out_df = pd.DataFrame({"qid":test_df["qid"].values})
 out_df['prediction'] = pred_test_y
out_df.to_csv("submission.csv", index=False )<import_modules>
data_out.to_csv('submission.csv', index = None )