Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<load_from_csv>
%matplotlib inline
def load_data() :
data_dir = Path(".. /input/house-prices-advanced-regression-techniques/")
 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])
 df = clean(df)
 df = encode(df)
 df = impute_plus(df)
 df_train = df.loc[df_train.index, :]
df_test = df.loc[df_test.index, :]
return df_train, df_test<load_from_csv>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
data_dir = Path(".. /input/house-prices-advanced-regression-techniques/")
 df = pd.read_csv(data_dir / "train.csv", index_col="Id")
 df.Exterior2nd.unique()<feature_engineering>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
def clean(df):
df['Exterior2nd'] = df['Exterior2nd'].replace({'Brk Cmn': 'BrkComm'})
 df['GarageYrBlt'] = df['GarageYrBlt'].where(df.GarageYrBlt <= 2010, df.YearBuilt)
 df.rename(columns={
'1stFlrSF': 'FirstFlrSF',
'2ndFlrSF': 'SecondFlrSF',
'3SsnPorch': 'Threeseasonporch'
}, inplace=True)
 return df<define_variables>
X_train = train.drop(labels = ["label"],axis = 1)
 Y_train = train["label"]
len(Y_train )
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"]
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", "Maj1", "Maj2", "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": ["MnWw", "GdWo", "MnPrv", "GdPrv"],
}
ordered_levels = {key: ["None"] + value for key, value in
ordered_levels.items() }
def encode(df):
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)
 for name, levels in ordered_levels.items() :
df[name] = df[name].astype(CategoricalDtype(levels,
ordered=True))
return df<data_type_conversions>
X_train = X_train / 255.0
test = test / 255.0
def impute_plus(df):
cols_with_missing = [col for col in df.columns if col != 'SalePrice' and df[col].isnull().any() ]
for col in cols_with_missing:
df[col + '_was_missing'] = df[col].isnull()
df[col + '_was_missing'] =(df[col + '_was_missing'])* 1
for name in df.select_dtypes("number"):
df[name] = df[name].fillna(0)
 for name in df.select_dtypes("category"):
df[name] = df[name].fillna("None")
 return df<split>
img_width = 28
img_height = 28
n_channels = 1
X_train = X_train.values.reshape(-1,img_height,img_width,n_channels)
 test = test.values.reshape(-1,img_height,img_width,n_channels )
df_train, df_test = load_data()<init_hyperparams>
Y_train = to_categorical(Y_train, num_classes = 10 )
xgb_params = dict(
 max_depth=3,
learning_rate=0.1,
n_estimators=100,
min_child_weight=1,
colsample_bytree=1,
subsample=1,
reg_alpha=0,
reg_lambda=1,
num_parallel_tree=1,
)<compute_train_metric>
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=2 )
def score_dataset(X, y, model=XGBRegressor(**xgb_params)) :
for colname in X.select_dtypes(["category"]):
X[colname] = X[colname].cat.codes
log_y = np.log(y)
 score = cross_val_score(
 model, X, log_y, cv=5, scoring='neg_mean_squared_error' 
)
 score = -1 * score.mean()
score = np.sqrt(score)
 return score<compute_test_metric>
print("Total Images:",len(Y_train)+len(Y_val))
print("Training Images:",len(Y_train))
print("Validation Images:",len(Y_val))
X = df_train.copy()
y = X.pop("SalePrice")
 baseline_score = score_dataset(X, y)
 print(f"Baseline score: {baseline_score:.5f} RMSE" )<normalization>
model = Sequential()
model.add(Convolution2D(filters = 32, kernel_size =(5,5),padding = 'Same', activation ='relu', input_shape = input_shape))
model.add(Convolution2D(filters = 32, kernel_size =(5,5),padding = 'Same', activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Convolution2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(Convolution2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))
mi_scores = make_mi_scores(X, y)
<drop_column>
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
 model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"] )
def drop_uninformative(df, mi_scores, threshold=0.0):
return df.loc[:, mi_scores > threshold]<drop_column>
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, width_shift_range=0.1, height_shift_range=0.1,
horizontal_flip=False, vertical_flip=False)
 datagen.fit(X_train )
drop_uninformative(X, mi_scores )<prepare_x_and_y>
Model = model.fit_generator(datagen.flow(X_train, Y_train,batch_size=200),epochs=30,verbose=1,validation_data=(X_val, Y_val))
X = df_train.copy()
y = X.pop("SalePrice")
 mi_scores = make_mi_scores(X, y)
 X["AllPub"] = X["Utilities"] == "AllPub"
mi_scores = make_mi_scores(X, y)
 X = drop_uninformative(X, mi_scores)
 X.head()
score_dataset(X, y )<categorify>
model.save("cnn_digit_recognizer.h5" )
def label_encode(df):
X = df.copy()
for colname in X.select_dtypes(['category']):
X[colname] = X[colname].cat.codes
return X<feature_engineering>
score = model.evaluate(X_train, Y_train, verbose=1)
 print('Train Loss:', score[0])
 print('Train Accuracy:', score[1] )
def mathematical_transforms(df):
X = pd.DataFrame()
X['LivLotRatio'] = df.GrLivArea / df.LotArea
X['Spaciousness'] =(df.FirstFlrSF + df.SecondFlrSF)/ df.TotRmsAbvGrd
X['AgeAtTOS'] = df.YrSold - df.YearBuilt
return X<categorify>
score = model.X_valuate(X_val, Y_val, verbose=1)
 print('Validation Loss:', score[0])
 print('Validation Accuracy:', score[1] )
def interactions(df):
X_inter_1 = pd.get_dummies(df.BldgType, prefix='Bldg')
 X_inter_1 = X_inter_1.mul(df.GrLivArea, axis=0)
 X_inter_2 = pd.get_dummies(df.BsmtCond, prefix='BsmtCond')
 X_inter_2 = X_inter_2.mul(df.TotalBsmtSF, axis=0)
 X_inter_3 = pd.get_dummies(df.GarageQual, prefix='GarageQual')
 X_inter_3 = X_inter_3.mul(df.GarageArea, axis=0)
 X = X_inter_1.join(X_inter_2)
 return X<prepare_x_and_y>
Y_pred = model.predict(X_val)
 Y_pred_classes = np.argmax(Y_pred,axis = 1)
 Y_true = np.argmax(Y_val,axis = 1)
 confusion_Matrix = confusion_matrix(Y_true, Y_pred_classes)
 print(confusion_Matrix )
def counts(df):
X = pd.DataFrame()
X['PorchTypes'] = df[['WoodDeckSF',
'OpenPorchSF',
'EnclosedPorch',
'Threeseasonporch',
'ScreenPorch'
]].gt(0.0 ).sum(axis=1)
 X['TotalHalfBath'] = df.BsmtFullBath + df.BsmtHalfBath
X['TotalRoom'] = df.TotRmsAbvGrd + df.FullBath + df.HalfBath
return X<create_dataframe>
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )
def group_transforms(df):
X = pd.DataFrame()
X['MedNhbdArea'] = df.groupby('Neighborhood')['GrLivArea'].transform('median')
 X['MeanAgeAtTOS'] = df.groupby('Neighborhood')['AgeAtTOS'].transform('mean')
 return X<define_variables>
final_Result = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 final_Result.to_csv("cnn_mnist_datagen.csv",index=False )