Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<drop_column>
%matplotlib inline
passengerid_test = test.PassengerId
COLS_TO_DROP.extend(['PassengerId', 'Name', 'Ticket', 'Surname'])
 drop_cols(COLS_TO_DROP)
 display(train.head() )<split>
train=pd.read_csv('.. /input/train.csv')
 test=pd.read_csv('.. /input/test.csv' )
y = train[['Survived']].copy()
train.drop(columns='Survived', inplace=True)
 X_train, X_valid, y_train, y_valid = train_test_split(train, y, train_size=0.8, random_state=RNG_SEED)
 X_test = test.copy()<choose_model_class>
train_images=train.iloc[:,1:].values
train_labels=train.iloc[:,0:1].values
test_X=test.iloc[:,:].values
WANT_HYPERPARAMETERS = False
if WANT_HYPERPARAMETERS:
params = dict(max_depth = [n for n in range(3, 9)],
min_samples_split = [n for n in range(2, 4)],
min_samples_leaf = [n for n in range(2, 4)],
n_estimators = [20, 40, 60, 80],)
 model = GridSearchCV(RandomForestClassifier(random_state=RNG_SEED),
params, cv=5, scoring='accuracy')
 model.fit(X_train, y_train)
 print(f'Best parameters {model_random_forest.best_params_}')
 print(f'Mean cross-validated accuracy of the best parameters: {model.best_score_:.4f}')
 else:
model = RandomForestClassifier(n_estimators=50, max_depth=5,
min_samples_leaf=2, min_samples_split=2,
random_state=RNG_SEED)
 model.fit(X_train, y_train )<compute_train_metric>
train_images = train_images.reshape(( -1, 28, 28, 1))
train_images = train_images.astype('float32')/ 255
test = test.values.reshape(-1,28,28,1)
 test = test / 255.0
predictions_valid = model.predict(X_valid)
 score = mean_absolute_error(y_valid, predictions_valid)
 print('MAE for validation set prediction:', round(score, 4))
folds = 5
scores = -1 * cross_val_score(model, X_valid, y_valid, cv=folds, scoring='neg_mean_absolute_error')
 print('Average MAE score(across experiments using {} folds):'.format(folds))
print(round(scores.mean() , 4))<save_to_csv>
train_labels = to_categorical(train_labels )
predictions_test = model.predict(X_test)
 predictions_test = predictions_test.astype(int)
 output = pd.DataFrame({'PassengerId': passengerid_test,
'Survived': predictions_test})
 output.to_csv('submission.csv', index=False )<feature_engineering>
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(train_images )
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
os.environ["KMP_SETTINGS"] = "false"<load_from_csv>
model = models.Sequential()
model.add(layers.Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',activation ='relu', input_shape =(28,28,1)))
 model.add(layers.Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',activation ='relu'))
model.add(layers.MaxPool2D(pool_size=(2,2)))
 model.add(layers.Dropout(0.25))
model.add(layers.Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(layers.Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(layers.MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(layers.Dropout(0.25))
model.add(layers.Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(layers.Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(layers.MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(layers.Dropout(0.25))
model.add(layers.Flatten())
 model.add(layers.Dropout(0.25))
model.add(layers.Dense(256, activation='relu',kernel_regularizer=regularizers.l2(0.001)))
 model.add(layers.Dropout(0.35))
model.add(layers.Dense(10, activation='softmax'))
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'] )
train_file_path = ".. /input/titanic/train.csv"
X = pd.read_csv(train_file_path)
 features = ["Pclass", "Sex", "Age", "Survived"]
X = pd.get_dummies(X[features])
 X = pd.concat([X.drop('Pclass', axis=1), pd.get_dummies(X['Pclass'], prefix="Pclass")], axis=1)
 imputer = SimpleImputer()
imputed_X = pd.DataFrame(imputer.fit_transform(X))
imputed_X.columns = X.columns
X = imputed_X
learnoutput = X.Survived
learninput = X.drop("Survived", axis=1)
 test_file_path = ".. /input/titanic/test.csv"
test_data = pd.read_csv(test_file_path)
 features = ["Pclass", "Sex", "Age"]
test = pd.get_dummies(test_data[features])
 testinput = pd.concat([test.drop('Pclass', axis=1), pd.get_dummies(test['Pclass'], prefix="Pclass")], axis=1 )<split>
history = model.fit_generator(datagen.flow(train_images,train_labels, batch_size=64),
epochs = 100,
verbose = 2, steps_per_epoch=train_images.shape[0] // 64
)
x_train_split, x_val_split, y_train_split, y_val_split = train_test_split(learninput, learnoutput, random_state=0 )<choose_model_class>
results = model.predict(test )
model = keras.Sequential([
layers.Dense(
 units=128,
activation = "tanh",
input_shape = [6] 
),
layers.Dropout(0.2),
layers.Dense(
 units = 256,
activation = "tanh", 
),
layers.Dropout(0.2),
layers.Dense(
 units = 512,
activation = "tanh", 
),
layers.Dropout(0.2),
layers.Dense(
 units = 1024,
activation = "tanh", 
),
layers.Dropout(0.2),
layers.Dense(
 units = 1,
activation = "sigmoid" 
)
 ] )<train_model>
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("cnn_mnist_datagen.csv",index=False )
<compute_test_metric><EOS> 
model.save('digit_clfr.h5' )