Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<define_variables>
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from keras.utils.np_utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, BatchNormalization
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import LearningRateScheduler
from tensorflow.keras.applications import EfficientNetB3
temp = np.where(np.isnan(train_data.Age))
len(temp[0] )<feature_engineering>
train = pd.read_csv(".. /input/digit-recognizer/train.csv")
 test = pd.read_csv(".. /input/digit-recognizer/test.csv" )
for item in temp[0]:
train_data.Age.at[item] = np.mean(train_data.Age)
 temp = np.where(np.isnan(train_data.Age))
len(temp[0] )<feature_engineering>
Y_train = train["label"]
X_train = train.drop(labels = ["label"],axis = 1)
 X_train = X_train / 255.0
X_test = test / 255.0
X_train = X_train.values.reshape(-1,28,28,1)
 X_test = X_test.values.reshape(-1,28,28,1)
 Y_train = to_categorical(Y_train, num_classes = 10 )
temp = np.where(np.isnan(test_data.Fare))
for item in temp[0]:
test_data.Fare.at[item] = np.mean(test_data.Fare)
 temp = np.where(np.isnan(test_data.Fare))
len(temp[0] )<feature_engineering>
datagen = ImageDataGenerator(
 rotation_range=10,
zoom_range = 0.10,
width_shift_range=0.1,
height_shift_range=0.1 )
temp = np.where(np.isnan(test_data.Age))
for item in temp[0]:
test_data.Age.at[item] = np.mean(train_data.Age)
 temp = np.where(np.isnan(test_data.Age))
len(temp[0] )<categorify>
enet = EfficientNetB3(input_shape=(32, 32, 3), weights='imagenet',include_top=False )
y = train_data['Survived']
features = ['Pclass', 'Sex', 'SibSp', 'Parch','Fare', 'Age']
x = pd.get_dummies(train_data[features])
 x_test = pd.get_dummies(test_data[features])
 model = RandomForestClassifier(n_estimators = 100, max_depth = 5, random_state = 1)
 model.fit(x, y)
 predictions = model.predict(x_test )<save_to_csv>
X_train = np.pad(X_train,(( 0,0),(2,2),(2,2),(0,0)) , mode='constant')
 X_train.shape
output = pd.DataFrame({'PassengerId' : test_data.PassengerId, 'Survived' : predictions})
 output.to_csv('submission.csv', index = False)
 print("Your submission was successfully saved!" )<load_from_csv>
X_train = np.squeeze(X_train, axis=-1)
 X_train = stacked_img = np.stack(( X_train,)*3, axis=-1)
 X_train.shape
df = pd.read_csv('.. /input/titanic/train.csv')
 test_df = pd.read_csv('.. /input/titanic/test.csv' )<count_values>
nets = 2
model = [0] *nets
for j in range(nets):
model[j] = Sequential(enet)
 model[j].add(Flatten())
 model[j].add(Dense(units=1024, use_bias=True, activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Dense(units=512, use_bias=True, activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Dropout (.4))
model[j].add(Dense(units=256, use_bias=True, activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Dropout (.4))
model[j].add(Dense(units=10, use_bias=True, activation='softmax'))
model[j].compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"] )
df['Sex'].value_counts()<concatenate>
annealer = LearningRateScheduler(lambda x: 1e-3 * 0.95 ** x)
 history = [0] * nets
epochs = 45
for j in range(nets):
X_train2, X_val2, Y_train2, Y_val2 = train_test_split(X_train, Y_train, test_size = 0.1)
 history[j] = model[j].fit_generator(datagen.flow(X_train2,Y_train2, batch_size=64),
epochs = epochs, steps_per_epoch = X_train2.shape[0]//64,
validation_data =(X_val2,Y_val2), callbacks=[annealer], verbose=0)
 print("EffNet {0:d}: Epochs={1:d}, Train accuracy={2:.5f}, Validation accuracy={3:.5f}".format(
 j+1,epochs,max(history[j].history['accuracy']),max(history[j].history['val_accuracy'])) )
complete_df = pd.concat([df, test_df] )<count_missing_values>
X_test = np.pad(X_test,(( 0,0),(2,2),(2,2),(0,0)) , mode='constant')
 X_test = np.squeeze(X_test, axis=-1)
 X_test = stacked_img = np.stack(( X_test,)*3, axis=-1)
 X_test.shape
complete_df.isnull().sum()<filter>
results = np.zeros(( X_test.shape[0],10))
for j in range(nets):
results = results + model[j].predict(X_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("MNIST_EffNet_Ensemble.csv",index=False )
<feature_engineering><EOS> 
X_test1a = test / 255.0
X_test1a = X_test1a.values.reshape(-1,28,28,1 )