Kaggle competition: Digit Recognizer
train_df.isnull().sum()<count_missing_values>
random_seed = 2020
np.random.seed(random_seed)
test_df.isnull().sum()<groupby>
train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test = pd.read_csv('.. /input/digit-recognizer/test.csv')
 Y = train['label']
X = train.drop(labels="label", axis=1)
 X = X.values.reshape(-1, 28, 28, 1)/ 255
test = test.values.reshape(-1, 28, 28, 1)/ 255
print(X.shape, test.shape )
keyword_dist = train_df.groupby("keyword")['target'].value_counts().unstack(fill_value=0)
 keyword_dist = keyword_dist.add_prefix(keyword_dist.columns.name ).rename_axis(columns=None ).reset_index()<sort_values>
learning_rate_reduction = ReduceLROnPlateau(monitor = 'val_acc',
patience = 3,
verbose = 1,
factor = 0.5,
min_lr = 0.0001)
 es = EarlyStopping(monitor='val_loss',
mode='min',
verbose=1,
patience=15,
restore_best_weights=True)
 def new_model(hidden=512, learning_rate=0.00128):
INPUT = Input(( 28, 28, 1))
inputs = Conv2D(64,(5, 5), activation='relu', padding='same' )(INPUT)
 inputs = MaxPool2D(pool_size=(3,3), strides=(1,1))(inputs)
 inputs = BatchNormalization()(inputs)
 inputs = Activation('relu' )(inputs)
 inputs = Dropout(0.25 )(inputs)
 tower_1 = Conv2D(64,(1, 1), activation='relu', padding='same' )(inputs)
 tower_1 = Conv2D(128,(2, 2), activation='relu', padding='same' )(tower_1)
 tower_1 = Dropout(0.5 )(tower_1)
 tower_1 = Conv2D(256,(3, 3), activation='relu', padding='same' )(tower_1)
 tower_1 = MaxPool2D(pool_size=(3,3), strides=(2,2))(tower_1)
 tower_1 = BatchNormalization()(tower_1)
 tower_2 = Conv2D(64,(2, 2), activation='relu', padding='same' )(inputs)
 tower_2 = Conv2D(128,(3, 3), activation='relu', padding='same' )(tower_2)
 tower_2 = Dropout(0.5 )(tower_2)
 tower_2 = Conv2D(256,(5, 5), activation='relu', padding='same' )(tower_2)
 tower_2 = MaxPool2D(pool_size=(3,3), strides=(2,2))(tower_2)
 tower_2 = BatchNormalization()(tower_2)
 tower_3 = Conv2D(64,(1, 1), activation='relu', padding='same' )(inputs)
 tower_3 = Conv2D(128,(3, 3), activation='relu', padding='same' )(tower_3)
 tower_3 = Dropout(0.5 )(tower_3)
 tower_3 = Conv2D(256,(5, 5), activation='relu', padding='same' )(tower_3)
 tower_3 = MaxPool2D(pool_size=(3,3), strides=(2,2))(tower_3)
 tower_3 = BatchNormalization()(tower_3)
 x = Add()([tower_1, tower_2, tower_3])
 x = Activation('relu' )(x)
 x = Conv2D(256,(5, 5), activation='relu', padding='same' )(x)
 x = MaxPool2D(pool_size=(5,5), strides=(4,4))(x)
 x = BatchNormalization()(x)
 x = Activation('relu' )(x)
 x = Flatten()(x)
 x = Dense(hidden, activation='relu' )(x)
 x = Dropout(0.5 )(x)
 x = Dense(hidden//4, activation='relu' )(x)
 x = Dropout(0.5 )(x)
 preds = Dense(10, activation='softmax', name='preds' )(x)
 model = Model(inputs=INPUT, outputs=preds)
 optimizer = Adam(lr=learning_rate)
 model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy', metrics=['acc'])
 return model
model = new_model()
keyword_dist.sort_values('target1',ascending = False ).head(10 )<sort_values>
datagen = ImageDataGenerator(featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range=0.1,
shear_range=0.02,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False )
keyword_dist.sort_values('target0',ascending = False ).head(10 )<feature_engineering>
epochs = 200
batch_size = 128
print("Learning Properties: Epoch:%i \t Batch Size:%i" %(epochs, batch_size))
predict_accumulator = np.zeros(model.predict(test ).shape)
 accumulated_history = []
for i in range(1, 6):
X_train, X_val, Y_train, Y_val = train_test_split(X, Y, test_size=0.20, shuffle=True, random_state=random_seed*i)
 model = new_model(512, 0.01)
 datagen.fit(X_train)
 history = model.fit_generator(datagen.flow(X_train, Y_train, batch_size=batch_size),
epochs=epochs, validation_data=(X_val, Y_val), verbose=1,
steps_per_epoch=X_train.shape[0]//batch_size,
callbacks=[learning_rate_reduction, es],
workers=4)
 loss, acc = model.evaluate(X, Y)
 if acc > 0.99:
predict_accumulator += model.predict(test)*acc
accumulated_history.append(history)
 print("Current Predictions on fold number %i" %i)
 print(*np.argmax(predict_accumulator, axis=1), sep='\t' )
train_df['word_count'] = train_df['text'].apply(lambda x : len(str(x ).split()))
test_df['word_count'] = test_df['text'].apply(lambda x : len(str(x ).split()))
train_df['unique_word_count'] = train_df['text'].apply(lambda x : len(set(str(x ).split())))
 test_df['unique_word_count'] = test_df['text'].apply(lambda x : len(set(str(x ).split())))
 train_df['count_letters'] = train_df['text'].apply(lambda x : len(str(x)))
 test_df['count_letters'] = test_df['text'].apply(lambda x : len(str(x)))
 train_df['count_punctuations'] = train_df['text'].apply(lambda x: len([c for c in str(x)if c in string.punctuation]))
test_df['count_punctuations'] = test_df['text'].apply(lambda x: len([c for c in str(x)if c in string.punctuation]))
train_df['stop_word_count'] = train_df['text'].apply(lambda x: len([w for w in str(x ).lower().split() if w in eng_stopwords]))
test_df['stop_word_count'] = test_df['text'].apply(lambda x: len([w for w in str(x ).lower().split() if w in eng_stopwords]))
train_df['hashtag_count'] = train_df['text'].apply(lambda x : len([c for c in str(x)if c == '
test_df['hashtag_count'] = test_df['text'].apply(lambda x : len([c for c in str(x)if c == '
train_df['mention_count'] = train_df['text'].apply(lambda x : len([c for c in str(x)if c=='@']))
test_df['mention_count'] = test_df['text'].apply(lambda x : len([c for c in str(x)if c=='@']))<categorify>
print("Completed Training.")
 results = np.argmax(predict_accumulator, axis=1)
 results = pd.Series(results, name="Label")
 print("Saving prediction to output...")
 submission = pd.concat([pd.Series(range(1, 1+test.shape[0]), name="ImageId"), results], axis=1)
 submission.to_csv('submission.csv', index=False )
<categorify><EOS> 
end_time = time.time()
total_time = int(end_time - start_time)
 print("Total time spent: %i hours, %i minutes, %i seconds" \
%(( total_time//3600),(total_time%3600)//60,(total_time%60)) )