Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<define_variables>
clear_output()
categorical_columns = set(train.dtypes[train.dtypes==object].index )<feature_engineering>
tensorflow_backend._get_available_gpus()
scale_strategies = ["none", "standard_scale", "standard_scale_exclude_categorcial_features"]
scale_strategy = scale_strategies[2]
if scale_strategy == scale_strategies[1]:
train_features =(train_features - mean_value)/ std_value
val_features =(val_features - mean_value)/ std_value
test_features =(test_features - mean_value)/ std_value
if scale_strategy == scale_strategies[2]:
for column in train_features.columns:
is_categorical_feature = False
components = column.split("_")
 if len(components)== 2 and components[0] in categorical_columns:
is_categorical_feature = True
if is_categorical_feature == False:
for features in [train_features, val_features, test_features]:
features.loc[:, column] =(features.loc[:, column] - mean_value[column])/ std_value[column]<drop_column>
train=pd.read_csv(r'.. /input/train.csv')
 test=pd.read_csv(r'.. /input/test.csv' )
use_correlated_columns = True
if use_correlated_columns:
train_features = train_features[correlated_columns]
val_features = val_features[correlated_columns]
test_features = test_features[correlated_columns]<choose_model_class>
labels=pd.get_dummies(train.iloc[:,0] ).values
def build_model(hp):
model = keras.Sequential()
model.add(keras.Input(( train_features.shape[1])))
 for depth in range(hp.Choice('depth', [3, 4, 5, 6, 7])) :
model.add(keras.layers.Dense(hp.Choice('width', [4, 8, 16, 32, 64]), activation=hp.Choice('activation', ["relu", "elu", "tanh", "selu", "sigmoid"]), kernel_regularizer=keras.regularizers.l2()))
model.add(keras.layers.Dense(1))
adam = keras.optimizers.Adam(learning_rate=hp.Choice("learing_rate", [1e-3, 5e-3, 1e-4, 5e-4, 1e-5]))
model.compile(loss='mse', optimizer=adam, metrics=["mae", rmlse])
 return model<train_model>
X_train,X_val,y_train,y_val=train_test_split(data,labels,test_size=0.33,random_state=42 )
early_stop = keras.callbacks.EarlyStopping(patience=10)
 reduce_lr = keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=3, min_lr=1e-7)
 model_checkpoint_path = "model.h5"
model_checkpoint = keras.callbacks.ModelCheckpoint(model_checkpoint_path, save_best_model=True)
 history = best_model.fit(train_features, train_targets, epochs=300, validation_data=(val_features, val_targets), callbacks=[early_stop, model_checkpoint, reduce_lr])
 for metric in ["loss", "mae", "rmlse"]:
pd.DataFrame(history.history, columns=[metric, "val_" + metric] ).plot()
plt.show()<save_to_csv>
aug_data=ImageDataGenerator(
 rotation_range=10,
zoom_range = 0.10,
width_shift_range=0.1,
height_shift_range=0.1 )
SalePrice = best_model.predict(test_features ).reshape(-1)
 submission = pd.DataFrame({"Id": test_ids, "SalePrice": SalePrice})
 submission.to_csv("submission.csv", index=False )<load_from_csv>
generator=aug_data.flow(X_train,y_train,batch_size=64 )
data_path = '/kaggle/input/cat-in-the-dat/'
train = pd.read_csv(data_path + 'train.csv', index_col='id')
 test = pd.read_csv(data_path + 'test.csv', index_col='id')
 submission = pd.read_csv(data_path + 'sample_submission.csv', index_col='id' )<concatenate>
def create_model() :
model=Sequential()
model.add(Conv2D(32,(3,3),input_shape=(28,28,1),activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(32,(3,3),activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(32,(5,5),strides=(2,2),padding='same',activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(64,(3,3),activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64,(3,3),activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64,(5,5),strides=(2,2),padding='same',activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(128,(4,4),activation='relu'))
model.add(BatchNormalization())
 model.add(Flatten())
 model.add(Dense(128,activation='relu'))
model.add(Dropout(0.4))
model.add(Dense(10,activation='softmax'))
model.compile(optimizer='adam',loss=categorical_crossentropy,metrics=['accuracy'])
 return model
all_data = pd.concat([train, test])
 all_data = all_data.drop('target', axis=1 )<categorify>
model=create_model()
all_data['bin_3'] = all_data['bin_3'].map({'F':0, 'T':1})
 all_data['bin_4'] = all_data['bin_4'].map({'N':0, 'Y':1} )<categorify>
history=model.fit_generator(generator,epochs=50,validation_data=(X_val,y_val),steps_per_epoch=data.shape[0]//64 )
ord1dict = {'Novice':0, 'Contributor':1,
'Expert':2, 'Master':3, 'Grandmaster':4}
ord2dict = {'Freezing':0, 'Cold':1, 'Warm':2,
'Hot':3, 'Boiling Hot':4, 'Lava Hot':5}
all_data['ord_1'] = all_data['ord_1'].map(ord1dict)
 all_data['ord_2'] = all_data['ord_2'].map(ord2dict )<categorify>
generator=aug_data.flow(data,labels,batch_size=64 )
ord_345 = ['ord_3', 'ord_4', 'ord_5']
ord_encoder = OrdinalEncoder()
all_data[ord_345] = ord_encoder.fit_transform(all_data[ord_345])
 for feature, categories in zip(ord_345, ord_encoder.categories_):
print(feature)
 print(categories )<define_variables>
model=create_model()
nom_features = ['nom_' + str(i)for i in range(10)]<categorify>
history=model.fit_generator(generator,epochs=50,validation_data=(X_val,y_val),steps_per_epoch=data.shape[0]//64 )
onehot_encoder = OneHotEncoder()
encoded_nom_matrix = onehot_encoder.fit_transform(all_data[nom_features])
 encoded_nom_matrix<drop_column>
x_test=test.values.reshape(-1,28,28,1)/255
all_data = all_data.drop(nom_features, axis=1 )<categorify>
pred=np.argmax(model.predict(x=x_test),1 )
date_features = ['day', 'month']
encoded_date_matrix = onehot_encoder.fit_transform(all_data[date_features])
 all_data = all_data.drop(date_features, axis=1)
 encoded_date_matrix<normalization>
output=np.concatenate(( np.arange(1,test.shape[0]+1 ).reshape(-1,1),pred.reshape(-1,1)) ,axis=1 )
ord_features = ['ord_' + str(i)for i in range(6)]
all_data[ord_features] = MinMaxScaler().fit_transform(all_data[ord_features] )<concatenate>
submission=pd.DataFrame(output,columns=['ImageId','Label'] )
all_data_sprs = sparse.hstack([sparse.csr_matrix(all_data),
encoded_nom_matrix,
encoded_date_matrix],
format='csr' )<prepare_x_and_y>
submission.to_csv('Submission.csv',index=False )
num_train = train.shape[0]
X_train = all_data_sprs[:num_train]
X_test = all_data_sprs[num_train:]
y = train['target']<split>
from IPython.display import FileLink
<choose_model_class><EOS> 
model.save('model.h5' )