Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<count_values>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import keras
from sklearn.model_selection import train_test_split
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import layers, models, utils, callbacks
import tensorflow as tf
import os
train['Survived'].value_counts(normalize=True )<count_values>
nbr_of_clases = 10
validation_percentage = 0.2
resnet_path = '.. /input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'
training_data = pd.read_csv('.. /input/digit-recognizer/train.csv' )
train['Sex'].value_counts()<categorify>
def prepare_data(data_to_transform):
data = data_to_transform.copy()
data = data.reshape(-1, 28, 28, 1)/ 255
return data
embark=pd.get_dummies(train['Embarked'] )<categorify>
y = training_data['label'].values
X = training_data.drop('label',axis = 1 ).values
y = keras.utils.to_categorical(y, nbr_of_clases)
 X_rgb = prepare_data(X)
 X_train, X_val, y_train, y_val = train_test_split(X_rgb, y, test_size=validation_percentage )
gender=pd.get_dummies(train['Sex'] )<data_type_conversions>
model = models.Sequential([
layers.Conv2D(32, 3, activation='relu', input_shape=(128,128,1), padding='same'),
layers.Conv2D(32, 3, activation='relu', padding='same'),
layers.MaxPooling2D(padding='same'),
layers.Dropout(0.5),
layers.Conv2D(64, 3, activation='relu', padding='same'),
layers.Conv2D(64, 3, activation='relu', padding='same'),
layers.MaxPooling2D(padding='same'),
layers.Dropout(0.25),
layers.GlobalAveragePooling2D() ,
layers.Dense(nbr_of_clases, activation='softmax'),
])
 model.summary()
model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'], )
train['Age'].fillna(30,inplace=True)
 train['Age']=train['Age'].astype('int' )<feature_engineering>
def get_fitted_data_generator(data):
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
zoom_range = 0.1,
height_shift_range=0.1, 
)
 datagen.fit(data)
 return datagen
def fit_model_generator(model, X_train, y_train, epochs=1, batch=32, X_val=None, y_val=None):
image_nbr = np.size(X_train, 0)
 training_data_generator = get_fitted_data_generator(X_train)
 es = callbacks.EarlyStopping(monitor='val_loss', patience=5, verbose=1, restore_best_weights=True)
 rlp = callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=2, min_lr=1e-10, mode='min', verbose=1)
 return model.fit_generator(training_data_generator.flow(X_train, y_train, batch_size=batch), steps_per_epoch=(image_nbr//batch),callbacks=[es, rlp],epochs=epochs, validation_data=(X_val, y_val), verbose=1 )
train['Age_grp']=train['Age'].apply(lambda x: age_grp(x))<categorify>
full_data_model = fit_model_generator(model, X_train, y_train, epochs=150,X_val=X_val,y_val=y_val )
age=pd.get_dummies(train['Age_grp'] )<concatenate>
testing_data = pd.read_csv('.. /input/digit-recognizer/test.csv' ).values
testing_data = prepare_data(testing_data)
 def get_predictions(model, data):
return np.array([np.argmax(prediction)for prediction in model.predict(data)])
 final_predictions = get_predictions(model, testing_data )
train_df=pd.concat([train,embark,gender,age],axis=1 )<feature_engineering>
submission_filename = 'submission.csv'
answers = pd.DataFrame({'ImageId':range(1, final_predictions.size + 1),'Label':final_predictions})
 answers.to_csv(submission_filename, index=False )
def is_var(val):
if val>0:
return 1
else:
return 0<feature_engineering>
submission_filename = '/kaggle/working/submission.csv'
answers.to_csv(submission_filename, index=False )