Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<concatenate>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPool2D, Flatten, Dropout, BatchNormalization
from keras.callbacks import EarlyStopping, ReduceLROnPlateau
from keras.preprocessing.image import ImageDataGenerator
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
import itertools
def concat_keyword_text(row):
return(str(row["text"])+ " " + str(row["keyword"]))<feature_engineering>
train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv' )
train_samples = train.apply(concat_keyword_text, axis = 1)
 train_samples.head()<init_hyperparams>
X_train = train.iloc[:,1:]
y_train = train.iloc[:,0]
vectorizer = TextVectorization()
text_ds = tf.data.Dataset.from_tensor_slices(train_samples ).batch(128)
 vectorizer.adapt(text_ds )<feature_engineering>
X_train = X_train.values.reshape(-1, 28, 28, 1)/255.
 test = test.values.reshape(-1, 28, 28, 1)/255.
 y_train = to_categorical(y_train, 10 )
voc = vectorizer.get_vocabulary()<define_variables>
datagen = ImageDataGenerator(
 rotation_range=15,
width_shift_range=0.1,
height_shift_range=0.1,
zoom_range=0.1,
shear_range=0.2
)
num_tokens = len(voc)
 embedding_dim = len(nlp('The' ).vector)
 embedding_matrix = np.zeros(( num_tokens, embedding_dim))<feature_engineering>
def create_model() :
model = Sequential()
model.add(Conv2D(32,(3,3), padding='same', input_shape=X_train.shape[1:], activation='relu'))
model.add(Conv2D(32,(3,3), padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(2,2))
model.add(Dropout(0.2))
model.add(Conv2D(64,(3,3), padding='same', activation='relu'))
model.add(Conv2D(64,(3,3), padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(2,2))
model.add(Dropout(0.2))
model.add(Conv2D(128,(3,3), padding='same', activation='relu'))
model.add(Conv2D(128,(3,3), padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(2,2))
model.add(Dropout(0.2))
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
 return model
for i, word in enumerate(voc):
embedding_matrix[i] = nlp(word ).vector<feature_engineering>
EPOCHS = 30
BATCH_SIZE = 50
ENSEMBLES = 5
result_list = []
histories = []
results = np.zeros(( test.shape[0],10))
callback_list = [
ReduceLROnPlateau(monitor='val_loss', factor=0.25, patience=2),
EarlyStopping(monitor='val_loss', min_delta=0.0005, patience=4)
 ]
for i in range(ENSEMBLES):
X_train_tmp, X_val, y_train_tmp, y_val = train_test_split(X_train, y_train, test_size=0.1, random_state=i)
 model = create_model()
print('training No.', i)
 history = model.fit(datagen.flow(X_train_tmp, y_train_tmp, batch_size=BATCH_SIZE),
verbose=0,
epochs=EPOCHS,
callbacks=callback_list,
validation_data=(X_val, y_val),
steps_per_epoch=X_train_tmp.shape[0] // BATCH_SIZE)
 histories.append(history)
 result = model.predict(test)
 results += result
result_list.append(result )
<categorify><EOS> 
results = np.argmax(results, axis=1)
 results = pd.Series(results, name='Label')
 submission = pd.concat([pd.Series(range(1,28001), name='ImageID'), results], axis=1)
 submission.to_csv('submission.csv', index=False )