Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<create_dataframe>
import pandas as pd
import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.utils import to_categorical
from sklearn.model_selection import train_test_split
def indicate_outliers(df, verbose=False):
X_new = pd.DataFrame()
X_new["Outlier"] =(df.Neighborhood == "Edwards")&(df.SaleCondition == "Partial")
 return X_new<categorify>
df_train = pd.read_csv('.. /input/train.csv' )
class CrossFoldEncoder:
def __init__(self, encoder, **kwargs):
self.encoder_ = encoder
self.kwargs_ = kwargs
self.cv_ = KFold(n_splits=5)
 def fit_transform(self, X, y, cols):
self.fitted_encoders_ = []
self.cols_ = cols
X_encoded = []
for idx_encode, idx_train in self.cv_.split(X):
fitted_encoder = self.encoder_(cols=cols, **self.kwargs_)
 fitted_encoder.fit(
 X.iloc[idx_encode, :], y.iloc[idx_encode], 
)
 X_encoded.append(fitted_encoder.transform(X.iloc[idx_train, :])[cols])
 self.fitted_encoders_.append(fitted_encoder)
 X_encoded = pd.concat(X_encoded)
 X_encoded.columns = [name + "_encoded" for name in X_encoded.columns]
return X_encoded
def transform(self, X):
X_encoded_list = []
for fitted_encoder in self.fitted_encoders_:
X_encoded = fitted_encoder.transform(X)
 X_encoded_list.append(X_encoded[self.cols_])
 X_encoded = reduce(
 lambda x, y: x.add(y, fill_value=0), X_encoded_list 
)/ len(X_encoded_list)
 X_encoded.columns = [name + "_encoded" for name in X_encoded.columns]
return X_encoded<drop_column>
X_train = df_train.drop('label', axis = 1)
 y_train = df_train['label']
X_train = X_train / 255
X_train = X_train.values.reshape(-1,28,28,1)
 y_train = to_categorical(y_train)
 train_datagen = ImageDataGenerator(
 rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
shear_range=0.1, 
)
 train_datagen.fit(X_train)
 model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(16,(3,3), activation='relu', padding='same', input_shape=(28, 28, 1)) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Conv2D(16,(3,3), activation='relu', padding='same'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Conv2D(32,(3,3), activation='relu', padding='same'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Conv2D(32,(3,3), activation='relu', padding='same'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Conv2D(64,(3,3), activation='relu', padding='same'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Conv2D(64,(3,3), activation='relu', padding='same'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(10, activation='softmax')
 ])
 model.compile(loss='categorical_crossentropy',
optimizer=Adam(0.0001),
metrics=['acc'] )
def drop_unhelpful_features(df, features_to_drop=None, verbose=False):
if features_to_drop is None:
return df
else:
if verbose:
print(len(features_to_drop), "features dropped: ")
 print(" ", features_to_drop)
 return df.drop(features_to_drop, axis=1)
<drop_column>
batch_size = 32
history = model.fit_generator(
 train_datagen.flow(X_train, y_train, batch_size=batch_size),
epochs=20,
)
<normalization><EOS> 
test = pd.read_csv('.. /input/test.csv')
 test = test / 255
test = test.values.reshape(-1,28,28,1)
 pred = model.predict(test)
 pred = np.argmax(pred, axis = 1)
 pred_csv = pd.DataFrame(pred, columns= ['Label'])
 pred_csv.index += 1
print(pred_csv)
 pred_csv.to_csv('submission.csv', index_label='ImageId' )