Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<randomize_order>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPool2D, Flatten, BatchNormalization, Dropout
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
df_train = train.sample(frac=0.7, random_state=0)
 df_valid = train.drop(df_train.index )<prepare_x_and_y>
train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv' )
X_train = df_train.drop(['target'], axis=1)
 X_valid = df_valid.drop(['target'], axis=1)
 y_train = df_train['target']
y_valid = df_valid['target']<data_type_conversions>
X_train = train.iloc[:,1:]
y_train = train.iloc[:,0]
x_train = vectorizer(np.array([[s] for s in X_train["text"]])).numpy()
x_valid = vectorizer(np.array([[s] for s in X_valid["text"]])).numpy()<categorify>
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 )
y_train_ = np_utils.to_categorical(y_train.values)
 y_valid_ = np_utils.to_categorical(y_valid.values )<choose_model_class>
random_seed = 0
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=random_seed )
int_sequences_input = keras.Input(shape=(None,), dtype="int64")
 embedded_sequences = embedding_layer(int_sequences_input)
 x = layers.Conv1D(64, 5, activation="relu",padding='same' )(embedded_sequences)
 x = layers.MaxPooling1D(3 )(x)
 x = layers.Conv1D(32, 5, activation="relu",padding='same' )(x)
 x = layers.MaxPooling1D(3 )(x)
 x = layers.Conv1D(16, 5, activation="relu",padding='same' )(x)
 x = layers.GlobalMaxPooling1D()(x)
 x = layers.Dense(16, activation="relu" )(x)
 x = layers.Dropout(0.5 )(x)
 preds = layers.Dense(2, activation="softmax" )(x)
 model = keras.Model(int_sequences_input, preds)
 model.summary()<train_model>
datagen = ImageDataGenerator(
 rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
zoom_range=0.1
)
early_stopping = callbacks.EarlyStopping(
 min_delta=0.001,
patience=20,
restore_best_weights=True, 
)
 model.compile(
 optimizer='adam',
loss='categorical_crossentropy',
metrics ='accuracy' 
)
 history = model.fit(
 x_train, y_train_,
validation_data=(x_valid, y_valid_),
batch_size=128,
epochs=500,
callbacks=[early_stopping],
verbose=1,
)<data_type_conversions>
model = Sequential()
model.add(Conv2D(32,(5,5), padding='same', input_shape=X_train.shape[1:], activation='relu'))
model.add(Conv2D(32,(5,5), padding='same', activation='relu'))
model.add(MaxPool2D(2,2))
model.add(Conv2D(64,(3,3), padding='same', activation='relu'))
model.add(Conv2D(64,(3,3), padding='same', activation='relu'))
model.add(MaxPool2D(2,2))
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.summary()
x_test = vectorizer(np.array([[s] for s in test["text"]])).numpy()<predict_on_test>
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'] )
predictions = model.predict(x_test )<load_from_csv>
EPOCHS = 30
BATCH_SIZE = 20
callback_list = [
ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=1),
EarlyStopping(monitor='val_loss', min_delta=0.0005, patience=4)
 ]
history = model.fit(datagen.flow(X_train, y_train, batch_size=BATCH_SIZE),
epochs=EPOCHS,
callbacks=callback_list,
validation_data=(X_val, y_val),
steps_per_epoch=X_train.shape[0] // BATCH_SIZE )
sub = pd.read_csv('.. /input/nlp-getting-started/sample_submission.csv')
 sub.head()<save_to_csv>
results = model.predict(test)
 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 )