Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<set_options>
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
train = pd.read_csv(".. /input/digit-recognizer/train.csv")
 test = pd.read_csv(".. /input/digit-recognizer/test.csv")
 print(train.shape)
 print(test.shape)
warnings.filterwarnings('ignore')
<categorify>
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)
 print(X_train.shape)
 print(Y_train.shape)
 print(X_test.shape )
def preprocess_data(verbose=False):
if verbose:
print("*** BEGIN PREPROCESSING ***")
 data_directory = Path(".. /input/house-prices-advanced-regression-techniques/")
 df, train_indices, test_indices = load(data_directory, verbose)
 df = clean(df, verbose)
 df = order_ordinals(df, verbose)
 df = encode_nominative_categoricals(df, verbose)
 df = impute(df, verbose)
 df_train = df.loc[train_indices, :]
df_test = df.loc[test_indices, :]
if verbose:
print("*** END PREPROCESSING ***
")
 return df_train, df_test<load_from_csv>
nets = 15
model = [0] *nets
ks1=3
ks2=3
ks3=5
ks4=5
ks5=7
ks6=7
init = he_normal(seed=82)
 for j in range(nets):
model[j] = Sequential()
model[j].add(Conv2D(32, kernel_size=ks1, activation='relu', kernel_initializer = init, input_shape =(28, 28, 1)))
 model[j].add(BatchNormalization())
 model[j].add(Conv2D(32, kernel_size=ks2, activation='relu', kernel_initializer = init))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(32, kernel_size=ks3, activation='relu', kernel_initializer = init))
model[j].add(BatchNormalization())
 model[j].add(Dropout(0.4))
model[j].add(Conv2D(64, kernel_size=ks4, activation='relu', kernel_initializer = init))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(64, kernel_size=ks5, activation='relu', kernel_initializer = init))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(64, kernel_size=ks6, activation='relu', kernel_initializer = init))
model[j].add(BatchNormalization())
 model[j].add(Dropout(0.4))
model[j].add(Conv2D(128, kernel_size=4, activation='relu', kernel_initializer = init))
model[j].add(BatchNormalization())
 model[j].add(Flatten())
 model[j].add(Dropout(0.4))
model[j].add(Dense(10, activation='softmax'))
optA = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)
 model[j].compile(optimizer=optA, loss="categorical_crossentropy", metrics=["accuracy"])
 model[0].summary()
def load(data_dir, verbose):
df_train = pd.read_csv(data_dir / "train.csv", index_col="Id")
 df_test = pd.read_csv(data_dir / "test.csv", index_col="Id")
 df = pd.concat([df_train, df_test])
 if verbose:
print("Train and test splits read from CSV and merged.")
 print("Missing values before any preprocessing: ", df.isnull().values.sum())
 return df, df_train.index, df_test.index<feature_engineering>
datagen = ImageDataGenerator(rotation_range=10, zoom_range = 0.10, width_shift_range=0.1, height_shift_range=0.1 )
def clean(df, verbose):
cleaned_features = ["Exterior2nd", "GarageYrBlt"]
df["Exterior2nd"] = df["Exterior2nd"].replace({"Brk Cmn": "BrkComm"})
 df["Exterior2nd"] = df["Exterior2nd"].replace({"Wd Shng": "WdShing"})
 df["Exterior2nd"] = df["Exterior2nd"].replace({"CmentBd": "CemntBd"})
 df["GarageYrBlt"] = df["GarageYrBlt"].where(df.GarageYrBlt <= 2010, df.YearBuilt)
 name_pairs = {"1stFlrSF": "FirstFlrSF",
"2ndFlrSF": "SecondFlrSF",
"3SsnPorch": "ThreeSeaPorch",}
df.rename(columns=name_pairs, inplace=True)
 if verbose:
print("Cleaned: ", cleaned_features, sep='
')
 print("Renamed(From, To): ", *name_pairs.items() , sep='
')
 return df<define_variables>
learning_rate_reduction = ReduceLROnPlateau(monitor='val_loss', patience=5, verbose=1, factor=0.31623, min_delta=1e-4)
 early_stopping = EarlyStopping(monitor='val_acc', min_delta=1e-5, patience=10, verbose=1, mode='max', restore_best_weights=True)
five_levels = ["Po", "Fa", "TA", "Gd", "Ex"]
ten_levels = list(range(10))
ordered_levels = {
"OverallQual": ten_levels,
"OverallCond": ten_levels,
"ExterQual": five_levels,
"ExterCond": five_levels,
"BsmtQual": five_levels,
"BsmtCond": five_levels,
"HeatingQC": five_levels,
"KitchenQual": five_levels,
"FireplaceQu": five_levels,
"GarageQual": five_levels,
"GarageCond": five_levels,
"PoolQC": five_levels,
"LotShape": ["Reg", "IR1", "IR2", "IR3"],
"LandSlope": ["Sev", "Mod", "Gtl"],
"BsmtExposure": ["No", "Mn", "Av", "Gd"],
"BsmtFinType1": ["Unf", "LwQ", "Rec", "BLQ", "ALQ", "GLQ"],
"BsmtFinType2": ["Unf", "LwQ", "Rec", "BLQ", "ALQ", "GLQ"],
"Functional": ["Sal", "Sev", "Maj2", "Maj1", "Mod", "Min2", "Min1", "Typ"],
"GarageFinish": ["Unf", "RFn", "Fin"],
"PavedDrive": ["N", "P", "Y"],
"Utilities": ["NoSeWa", "NoSewr", "AllPub"],
"CentralAir": ["N", "Y"],
"Electrical": ["Mix", "FuseP", "FuseF", "FuseA", "SBrkr"],
"Fence": ["MnPrv", "MnWw", "GdWo", "GdPrv"],
}
ordered_levels = {key: ["None"] + value for key, value in
ordered_levels.items() }
def print_n_per_line(items, n):
for idx, item in enumerate(items):
print(" " + item + ''.join([' ' for i in range(14 - len(item)) ]), end='')
 if idx % n == n-1:
print()
print()
def order_ordinals(df, verbose):
for name, levels in ordered_levels.items() :
df[name] = df[name].astype(CategoricalDtype(levels, ordered=True))
if verbose:
print("Created ordinal categories and ordered with levels: ")
 ')
 print_n_per_line(list(ordered_levels.keys()), 5)
 return df<categorify>
history = [0] * nets
epochs = 50
for j in range(nets):
rs = 10*j + 1
X_train2, X_val2, Y_train2, Y_val2 = train_test_split(X_train, Y_train, train_size = 0.9, random_state = rs)
 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=[learning_rate_reduction, early_stopping], verbose=0)
 maxpos = history[j].history['val_acc'].index(max(history[j].history['val_acc']))
print("N{0:d}:: Max val_acc={1:.5f} at Epoch {2:d} Min_tr_loss={3:.5f} Min_val_loss={4:.5f}".format(j+1,max(history[j].history['val_acc']),maxpos+1, min(history[j].history['loss']), min(history[j].history['val_loss'])))
features_nom = ["MSSubClass", "MSZoning", "Street", "Alley", "LandContour", "LotConfig", "Neighborhood", "Condition1", "Condition2", "BldgType", "HouseStyle", "RoofStyle", "RoofMatl", "Exterior1st", "Exterior2nd", "MasVnrType", "Foundation", "Heating", "CentralAir", "GarageType", "MiscFeature", "SaleType", "SaleCondition"]
def encode_nominative_categoricals(df, verbose):
for name in features_nom:
df[name] = df[name].astype("category")
 if "None" not in df[name].cat.categories:
df[name].cat.add_categories("None", inplace=True)
 if verbose:
print("Created nominative categories(mostly conversions from numericals): ")
 '
print_n_per_line(features_nom, 5)
 return df<categorify>
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_32C3C3C564C5C7C7128C4FC10_50e_LR10_RLROP_ES_BN_DA_DO40_TR90_N15_ADAM_xx.csv",index=False)
 print(submission.shape)