Kaggle competition: Kannada MNIST
model_5 = get_model_5(embedding_matrix_wiki)
 model_5.fit(X_train, y_train, batch_size=512, epochs=3, validation_data=(X_val, y_val))<train_model>
from keras.optimizers import SGD
from keras.models import Sequential
from keras.utils import to_categorical
from keras.preprocessing.image import ImageDataGenerator
from keras.layers.normalization import BatchNormalization
from keras.layers import Dense, Dropout, Conv2D, Flatten, MaxPool2D
from keras.callbacks import EarlyStopping, History, LearningRateScheduler
model_6 = get_model_6(embedding_matrix_wiki)
 model_6.fit(X_train, y_train, batch_size=512, epochs=2, validation_data=(X_val, y_val))<predict_on_test>
data_dir = "/kaggle/input/Kannada-MNIST/"
pred_val_1 = model_1.predict([X_val], batch_size=512, verbose=1)
 pred_val_2 = model_2.predict([X_val], batch_size=512, verbose=1)
 pred_val_3 = model_3.predict([X_val], batch_size=512, verbose=1)
 pred_val_4 = model_4.predict([X_val], batch_size=512, verbose=1)
 pred_val_5 = model_5.predict([X_val], batch_size=512, verbose=1)
 pred_val_6 = model_6.predict([X_val], batch_size=512, verbose=1 )<prepare_x_and_y>
train_df = pd.read_csv(data_dir+"train.csv")
 train_df.head(2 )
pred_val_1_df = np.reshape(pred_val_1,(pred_val_1.shape[0]))
pred_val_2_df = np.reshape(pred_val_2,(pred_val_2.shape[0]))
pred_val_3_df = np.reshape(pred_val_3,(pred_val_3.shape[0]))
pred_val_4_df = np.reshape(pred_val_4,(pred_val_4.shape[0]))
pred_val_5_df = np.reshape(pred_val_5,(pred_val_5.shape[0]))
pred_val_6_df = np.reshape(pred_val_6,(pred_val_6.shape[0]))
pred_val_df = np.reshape(y_val,(y_val.shape[0]))
validation_df = pd.DataFrame({'val_1': pred_val_1_df, 'val_2': pred_val_2_df, 'val_3': pred_val_3_df, 'val_4': pred_val_4_df, 'val_5': pred_val_5_df, 'val_6': pred_val_6_df, 'prediction': pred_val_df})
 validation_df.to_csv('validation.csv', index=False )<feature_engineering>
y = train_df.values[:, 0]
y = to_categorical(y)
 print("Shape of y: ", y.shape)
 print("Sample of y: ", y[0] )
validation_df['val_12_mean'] =(validation_df['val_1'] + validation_df['val_2'])/ 2.0
validation_df['val_34_mean'] =(validation_df['val_3'] + validation_df['val_4'])/ 2.0
validation_df['val_56_mean'] =(validation_df['val_5'] + validation_df['val_6'])/ 2.0
validation_df['val_123_mean'] =(validation_df['val_1'] + validation_df['val_2'] + validation_df['val_3'])/ 3.0
validation_df['val_456_mean'] =(validation_df['val_4'] + validation_df['val_5'] + validation_df['val_6'])/ 3.0
validation_df['val_1_log'] = np.log(validation_df['val_1'])
 validation_df['val_2_log'] = np.log(validation_df['val_2'])
 validation_df['val_3_log'] = np.log(validation_df['val_3'])
 validation_df['val_4_log'] = np.log(validation_df['val_4'])
 validation_df['val_5_log'] = np.log(validation_df['val_5'])
 validation_df['val_6_log'] = np.log(validation_df['val_6'])
 validation_df['val_12_log_mean'] =(validation_df['val_1_log'] + validation_df['val_2_log'])/ 2.0
validation_df['val_34_log_mean'] =(validation_df['val_3_log'] + validation_df['val_4_log'])/ 2.0
validation_df['val_56_log_mean'] =(validation_df['val_5_log'] + validation_df['val_6_log'])/ 2.0
validation_df['val_123_log_mean'] =(validation_df['val_1_log'] + validation_df['val_2_log'] + validation_df['val_3_log'])/ 3.0
validation_df['val_456_log_mean'] =(validation_df['val_4_log'] + validation_df['val_5_log'] + validation_df['val_6_log'])/ 3.0
validation_df = validation_df[[c for c in validation_df.columns if c != 'prediction'] + ['prediction']]
validation_df.head()<prepare_x_and_y>
X = train_df.values[:, 1:]/255.0
X = X.reshape(-1, 28, 28, 1)
 print("Shape of X: ", X.shape)
 print("Sample of X: ")
 plt.imshow(X[0].reshape(28, 28))
X_val = validation_df[validation_df.columns.values[:-1]].values
y_val = validation_df[validation_df.columns.values[-1]].values
clf = VotingClassifier(estimators=
[('gnb', GaussianNB()), 
('xgb', xgb.XGBClassifier(max_dept=100, n_estimators=15, learning_rate=0.05,
colsample_bytree=0.5, gamma=0.01, reg_alpha=4,
objective='binary:logistic')) , 
('knn', KNeighborsClassifier(n_neighbors=500)) , 
('rf', RandomForestClassifier(max_depth=3, n_estimators=100)) , 
('lr', LogisticRegression()), 
('dt', DecisionTreeClassifier(max_depth=3, criterion='entropy')) , 
('adb', AdaBoostClassifier(base_estimator=DecisionTreeClassifier(max_depth=3, criterion='entropy'), learning_rate=0.05)) , 
('gb', GradientBoostingClassifier(max_depth=3, learning_rate=0.05,
n_estimators=100)) , 
('qda', QuadraticDiscriminantAnalysis()), 
('lda', LinearDiscriminantAnalysis())
 ],
voting='soft')
 clf.fit(X_val, y_val )<predict_on_test>
validation_split =.15
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=validation_split, stratify=y, random_state=seed )
pred_test_1 = model_1.predict([X_test], batch_size=512, verbose=1)
 pred_test_2 = model_2.predict([X_test], batch_size=512, verbose=1)
 pred_test_3 = model_3.predict([X_test], batch_size=512, verbose=1)
 pred_test_4 = model_4.predict([X_test], batch_size=512, verbose=1)
 pred_test_5 = model_5.predict([X_test], batch_size=512, verbose=1)
 pred_test_6 = model_6.predict([X_test], batch_size=512, verbose=1 )<prepare_x_and_y>
class CNN_Modeling() :
def __init__(self, model_confs):
self.epochs = 30
self.batch_size = 80
self.annealer = LearningRateScheduler(lambda x: 1e-3 * 0.95 ** x, verbose=0)
 self.models = []
self.history = []
self.model_names = []
self.model_confs = model_confs
def build_models(self, feature_maps, kernel_size, dense_size, drop_rate):
model = Sequential()
for i, fm in enumerate(feature_maps):
model.add(Conv2D(fm, kernel_size=kernel_size, padding='same', activation='relu', input_shape=(28, 28, 1)))
 if i == len(feature_maps)-1:
model.add(MaxPool2D())
 else:
model.add(MaxPool2D(padding='same'))
model.add(Dropout(drop_rate))
model.add(Flatten())
 for dns in dense_size:
if dns>0:
model.add(Dense(dns, activation='relu'))
model.add(Dropout(drop_rate))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
 return model
def get_model_name(self, conf):
name = "-".join(
 ["{}C{}P1".format(ft, conf['kernel_size'])for ft in conf['feature_maps']])
 name = name + "-" + "-".join(map(str, conf['dense_size']))
name = name + '-D%d'%round(conf["drop_rate"]*100)
 return name
def train_models(self, _x_train, _y_train, _x_val, _y_val):
self.models = [None]*len(self.model_confs)
 self.history = [None]*len(self.model_confs)
 self.model_names = [None]*len(self.model_confs)
 for i, model_conf in enumerate(self.model_confs):
self.models[i] = self.build_models(**model_conf)
 self.model_names[i] = self.get_model_name(model_conf)
 self.history[i] = self.models[i].fit(_x_train,_y_train, batch_size=self.batch_size, epochs=self.epochs,
validation_data =(_x_val,_y_val), callbacks=[self.annealer], verbose=0)
 print("CNN {0}: Epochs={1:d}, Train accuracy={2:.5f}, Validation accuracy={3:.5f}".format(
 self.model_names[i], self.epochs, max(self.history[i].history['accuracy']),
max(self.history[i].history['val_accuracy'])) 
)
 def plot_accuracy_chart(self, accuracy='val_accuracy'):
plt.figure(figsize=(15,5))
for i in range(len(self.models)) :
sns.lineplot(
 x=range(self.epochs),
y=self.history[i].history[accuracy],
label=self.model_names[i] 
)
 def predict(self, model_name, _x_predict):
given_model = self.models[self.model_names.index(model_name)]
return given_model.predict_classes(_x_predict)
 def get_model(self, model_name):
return self.models[self.model_names.index(model_name)]
pred_test_1_df = np.reshape(pred_test_1,(pred_test_1.shape[0]))
pred_test_2_df = np.reshape(pred_test_2,(pred_test_2.shape[0]))
pred_test_3_df = np.reshape(pred_test_3,(pred_test_3.shape[0]))
pred_test_4_df = np.reshape(pred_test_4,(pred_test_4.shape[0]))
pred_test_5_df = np.reshape(pred_test_5,(pred_test_5.shape[0]))
pred_test_6_df = np.reshape(pred_test_6,(pred_test_6.shape[0]))
test_df1 = pd.DataFrame({'val_1': pred_test_1_df, 'val_2': pred_test_2_df, 'val_3': pred_test_3_df, 'val_4': pred_test_4_df, 'val_5': pred_test_5_df, 'val_6': pred_test_6_df})
 test_df1.to_csv('test.csv', index=False )<feature_engineering>
val_pred_labels = cnn_model_before_data_aug.predict("32C5P1-64C5P1-128C5P1-256-D30", X_val)
 y_val_labels = np.argmax(y_val, axis=1)
 misclassses = np.where(np.argmax(y_val, axis=1)!= val_pred_labels)[0]
print("Number of misclassifications: ", len(misclassses))
test_df1['val_12_mean'] =(test_df1['val_1'] + test_df1['val_2'])/ 2.0
test_df1['val_34_mean'] =(test_df1['val_3'] + test_df1['val_4'])/ 2.0
test_df1['val_56_mean'] =(test_df1['val_5'] + test_df1['val_6'])/ 2.0
test_df1['val_123_mean'] =(test_df1['val_1'] + test_df1['val_2'] + test_df1['val_3'])/ 3.0
test_df1['val_456_mean'] =(test_df1['val_4'] + test_df1['val_5'] + test_df1['val_6'])/ 3.0
test_df1['val_1_log'] = np.log(test_df1['val_1'])
 test_df1['val_2_log'] = np.log(test_df1['val_2'])
 test_df1['val_3_log'] = np.log(test_df1['val_3'])
 test_df1['val_4_log'] = np.log(test_df1['val_4'])
 test_df1['val_5_log'] = np.log(test_df1['val_5'])
 test_df1['val_6_log'] = np.log(test_df1['val_6'])
 test_df1['val_12_log_mean'] =(test_df1['val_1_log'] + test_df1['val_2_log'])/ 2.0
test_df1['val_34_log_mean'] =(test_df1['val_3_log'] + test_df1['val_4_log'])/ 2.0
test_df1['val_56_log_mean'] =(test_df1['val_5_log'] + test_df1['val_6_log'])/ 2.0
test_df1['val_123_log_mean'] =(test_df1['val_1_log'] + test_df1['val_2_log'] + test_df1['val_3_log'])/ 3.0
test_df1['val_456_log_mean'] =(test_df1['val_4_log'] + test_df1['val_5_log'] + test_df1['val_6_log'])/ 3.0
test_df1.head()<predict_on_test>
datagen = ImageDataGenerator(
 rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1 
)
 model_name = "32C5P1-64C5P1-128C5P1-256-D30"
bes_model_so_far = cnn_model_before_data_aug.get_model(model_name)
 epochs = 50
history = bes_model_so_far.fit_generator(datagen.flow(X_train, y_train, batch_size=64),
epochs=epochs, steps_per_epoch=X_train.shape[0]//64,validation_data=(X_val,y_val),
callbacks=[cnn_model_before_data_aug.annealer], verbose=0)
 print("CNN {0}: Epochs={1:d}, Train accuracy={2:.5f}, Validation accuracy={3:.5f}".format(
 model_name,epochs,max(history.history['accuracy']),max(history.history['val_accuracy'])) )
X_test = test_df1.values
y_test_pred = clf.predict(X_test)
 y_test_pred = np.reshape(y_test_pred,(y_test_pred.shape[0]))<save_to_csv>
predict_df = pd.read_csv(data_dir+"test.csv")
 predict_df.head(2 )
submission_df = pd.DataFrame({'qid': test_df['qid'].values, 'prediction': y_test_pred})
 submission_df.to_csv('submission.csv', index=False )<import_modules>
predicted_labels = bes_model_so_far.predict_classes(X_predict )
import numpy as np
import pandas as pd
import tensorflow as tf
from tqdm import tqdm
import glob
import os
from trackml.dataset import load_event, load_dataset
from trackml.score import score_event
from IPython.display import display
from keras.models import Sequential
from keras.layers import Dense, Dropout, BatchNormalization
from keras.layers.embeddings import Embedding<load_from_csv>
final_prediction = pd.DataFrame()
final_prediction['id'] = img_ids
final_prediction['label'] = predicted_labels
final_prediction.to_csv("submission.csv", index=False )