Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<normalization>
!pip install keras-tuner
test_data_scaler = scaler.transform(test_data_final )<predict_on_test>
train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 labels = train.iloc[:,0].values.astype('int32')
 X_train =(train.iloc[:,1:].values ).astype('float32')
 X_test =(pd.read_csv('.. /input/digit-recognizer/test.csv' ).values ).astype('float32')
 X_train = X_train.reshape(-1,28,28,1)
 X_test = X_test.reshape(-1,28,28,1)
 y_train = tf.keras.utils.to_categorical(labels)
 print("Check data")
 print(labels)
 print(X_train[0].shape)
 print(y_train )
test_data_final = pd.DataFrame(test_data_scaler)
 test_data_final["Kmean"] = kmeans.predict(test_data_final)
<predict_on_test>
( train_imagesRaw, train_labelsRaw),(test_imagesRaw, test_labelsRaw)= mnist.load_data()
test_data_pred_proba = classifier.predict_proba(test_data_final)
 keys = label_cat.classes_ 
<prepare_output>
X_train_keras = train_imagesRaw.reshape(-1,28,28,1)
 X_test_keras = test_imagesRaw.reshape(-1,28,28,1)
 print("X_train_keras",X_train_keras.shape)
 print("X_test_keras",X_test_keras.shape)
 train_labels_keras = tf.keras.utils.to_categorical(train_labelsRaw)
 test_labels_keras = tf.keras.utils.to_categorical(test_labelsRaw)
 print("train_labels_keras ",train_labels_keras.shape)
 print("test_labels_keras ", test_labels_keras.shape )
result = pd.DataFrame(data=test_data_pred_proba,columns=keys)
 result.head(3 )<save_to_csv>
train_images = np.concatenate(( X_train_keras,X_train,X_test_keras), axis=0)
 print("new Concatenated train_images ", train_images.shape)
 print("_"*50)
 train_labels = np.concatenate(( train_labels_keras,y_train,test_labels_keras), axis=0)
 print("new Concatenated train_labels ", train_labels.shape )
result.to_csv(path_or_buf="classifier_sf.csv",index=True, index_label = 'Id' )<load_from_csv>
scale = np.max(train_images)
 train_images /= scale
X_test /= scale
print("Max: {}".format(scale))
train = pd.read_csv(".. /input/titanic/train.csv", index_col='PassengerId')
 test = pd.read_csv('.. /input/titanic/test.csv', index_col='PassengerId')
 train.head(10 )<sort_values>
X_train, X_val, y_train, y_val = train_test_split(train_images, train_labels, test_size=0.10 )
train.corr().abs() ['Survived'].sort_values(ascending=False )<count_missing_values>
input_size = X_train.shape
n_logits = y_train.shape[1]
print("Input: {}".format(input_size))
print("Output: {}".format(n_logits))
print(train.isnull().sum())
 print('
The observation ratio of missing Cabin values is',round(train['Cabin'].isnull().sum() /len(train),2))
print('The observation ratio of missing Age values is',round(train['Age'].isnull().sum() /len(train),2))
print('
')
 print(test.isnull().sum())
 print('
The observation ratio of missing Cabin values is',round(test['Cabin'].isnull().sum() /len(test),2))
print('The observation ratio of missing Age values is',round(test['Age'].isnull().sum() /len(test),2))<prepare_output>
def build_model(hp):

 num_layers = hp.Int('num_layers', min_value=2, max_value=16, step=2)
 lr = hp.Choice('learning_rate', [1e-3, 5e-4])
 inputs = layers.Input(shape=(28, 28, 1))
x = inputs
for idx in range(num_layers):
idx = str(idx)
 filters = hp.Int('filters_' + idx, 32, 256, step=32, default=64)
 x = layers.Conv2D(filters=filters, kernel_size=3, padding='same',
activation='relu' )(x)
 if x.shape[1] >= 8:
pool_type = hp.Choice('pool_' + idx, values=['max', 'avg'])
 if pool_type == 'max':
x = layers.MaxPooling2D(2 )(x)
 elif pool_type == 'avg':
x = layers.AveragePooling2D(2 )(x)
 x = layers.Flatten()(x)
 x = layers.Dense(256, activation='relu' )(x)
 x = layers.Dense(256, activation='relu' )(x)
 x = layers.Dense(256, activation='relu' )(x)
 x = layers.Dropout(0.5 )(x)
 outputs = layers.Dense(n_logits, activation='softmax' )(x)
 model = keras.Model(inputs, outputs)
 model.compile(optimizer=Adam(lr),
loss='categorical_crossentropy',
metrics=['accuracy'])
 return model
train_copy = train.copy()
train_copy['Cabin'] = train_copy['Cabin'].apply(lambda x: 0 if pd.isnull(x)else 1 )<sort_values>
tuner = RandomSearch(
 build_model,
objective='val_accuracy',
max_trials=8,
executions_per_trial=3,
directory='my_dir',
project_name='mnist')
 tuner.search_space_summary()
train.corr().abs() ['Age'].sort_values(ascending=False )<train_model>
tuner.search(X_train, y_train,
epochs=30,
validation_data=(X_val, y_val))
<prepare_output><EOS> 
predictions_vector = model.predict(X_test, verbose=0)
 predictions = np.argmax(predictions_vector,axis=1)
 pd.DataFrame({"ImageId": list(range(1,len(predictions)+1)) , "Label": predictions} ).to_csv("preds.csv", index=False, header=True )