Kaggle competition: Kannada MNIST
<SOS> metric: CategorizationAccuracy Kaggle data source: kannada-mnist<save_to_csv>
sns.set(font_scale=1.5)
sub["target"] =(prediction + prediction2)/2
sub.to_csv("submission4.csv", index=False )<save_to_csv>
train_path = ".. /input/Kannada-MNIST/train.csv"
test_path = ".. /input/Kannada-MNIST/test.csv"
train = pd.read_csv(train_path)
 test = pd.read_csv(test_path )
sub["target"] =(prediction + prediction1 + prediction2)/3
sub.to_csv("submission5.csv", index=False )<load_from_csv>
train_images = train.iloc[:,1:]/255 - 0.5
train_labels = train.iloc[:,0]
test_images = test.iloc[:,1:]/255 - 0.5
test_id = test.iloc[:,0]
X_train=train_images.values.reshape(-1,28,28,1)
 X_test=test_images.values.reshape(-1,28,28,1)
 Y_train=to_categorical(train_labels)
 fig=plt.figure(figsize=(16, 16))
for i in range(1, 5):
img = X_train[i].squeeze()
fig.add_subplot(1, 5, i)
 plt.imshow(img,cmap=plt.cm.binary)
 plt.axis('off')
 plt.show()
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<load_from_csv>
def elastic_transform(image, alpha_range, sigma, random_state=None):

 if random_state is None:
random_state = np.random.RandomState(None)
 if np.isscalar(alpha_range):
alpha = alpha_range
else:
alpha = np.random.uniform(low=alpha_range[0], high=alpha_range[1])
 shape = image.shape
dx = gaussian_filter(( random_state.rand(*shape)* 2 - 1), sigma)* alpha
dy = gaussian_filter(( random_state.rand(*shape)* 2 - 1), sigma)* alpha
x, y, z = np.meshgrid(np.arange(shape[0]), np.arange(shape[1]), np.arange(shape[2]), indexing='ij')
 indices = np.reshape(x+dx,(-1, 1)) , np.reshape(y+dy,(-1, 1)) , np.reshape(z,(-1, 1))
return map_coordinates(image, indices, order=1, mode='reflect' ).reshape(shape )
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<define_variables>
datagen = ImageDataGenerator(
 rotation_range=10,
zoom_range = 0.10,
width_shift_range=0.1,
height_shift_range=0.1
)
TARGET='target'
ID='ID_code'<sort_values>
nets=3
model = [0] * nets
for j in range(nets):
model[j] = Sequential()
model[j].add(Conv2D(32, kernel_size = 3, activation='relu', input_shape =(28, 28, 1)))
 model[j].add(BatchNormalization())
 model[j].add(Conv2D(32, kernel_size = 3, activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(32, kernel_size = 5, strides=2, padding='same', activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Dropout(0.1))
model[j].add(Conv2D(64, kernel_size = 3, activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(64, kernel_size = 3, activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(64, kernel_size = 5, strides=2, padding='same', activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Dropout(0.15))
model[j].add(Conv2D(128, kernel_size = 4, activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Flatten())
 model[j].add(Dropout(0.2))
model[j].add(Dense(10, activation='softmax'))
model[j].compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
 model[0].summary()
correlations = correlations.sort_values(by=TARGET)
 correlations.head(10)[TARGET]<import_modules>
decay = LearningRateScheduler(lambda x: 1e-3 * 0.95 ** x)
 epochs = 30
batch_size = 64
history = [0] * nets
for j in range(nets):
X_train2, X_val2, Y_train2, Y_val2 = train_test_split(X_train, Y_train, test_size = 0.1)
 history[j] = model[j].fit_generator(datagen.flow(X_train2, Y_train2, batch_size=batch_size),\
epochs = epochs,steps_per_epoch = X_train2.shape[0]//64,\
validation_data =(X_val2,Y_val2),callbacks = [decay], verbose = True)
 model_json = model[j].to_json()
with open("model"+str(j)+".json", "w")as json_file:
json_file.write(model_json)
 model[j].save_weights("modelw"+str(j)+".h5")
 print("Saved model to disk")
from sklearn.linear_model import LogisticRegression
from lightgbm import LGBMClassifier
from sklearn.model_selection import StratifiedKFold, KFold
from sklearn.metrics import roc_auc_score
from sklearn.naive_bayes import GaussianNB<train_model>
predictions = np.zeros(( X_test.shape[0],10))
for j in range(nets):
predictions = predictions + model[j].predict(X_test)
 test_labels = np.argmax(predictions, axis=1)
 submission = pd.DataFrame(test_labels)
 submission.index = test_id
submission.columns = ["label"]
submission.to_csv("submission.csv" )