Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<choose_model_class>
np.random.seed(0)
 %matplotlib inline
params = {
"C": [1e-3, 1e-2, 1e-1, 1],
"max_iter": [30000]
}
classifier = LinearSVC
classifier_df = trainClassifier(X_train, y_train, "LinearSVC", classifier, params, accuracy_score)
 results = results.append(classifier_df )<train_model>
sns.set(style = 'white' , context = 'notebook' , palette = 'deep')
 rcParams['figure.figsize'] = 10,6
params = {
"kernel" : ["rbf"],
"C": [1e-4, 1e-3, 1e-2, 1e-1, 1, 10],
"gamma": [1e-3, 1e-2, 1e-1, 1, 10]
}
classifier = SVC
classifier_df = trainClassifier(X_train, y_train, "SVC", classifier, params, accuracy_score)
 results = results.append(classifier_df )<choose_model_class>
data_path = ".. /input/digit-recognizer/"
train = pd.read_csv(join(data_path,"train.csv"))
test = pd.read_csv(join(data_path,"test.csv"))
params = {
"C": [1e-3, 1e-2, 1e-1, 1, 10]
}
classifier = LogisticRegression
classifier_df = trainClassifier(X_train, y_train, "LogisticRegression", classifier, params, accuracy_score)
 results = results.append(classifier_df )<choose_model_class>
def process_data(data):
data = data/255.0
data = data.values.reshape(-1,28,28,1)
 return data
X_train = process_data(X_train)
 Y_train = to_categorical(Y_train,num_classes = 10 )
params = {"max_depth": [3, 5, 7, 10, None],
"n_estimators":[3, 5,10, 25, 50],
"max_features": [1, 2, "auto"]}
classifier = RandomForestClassifier
classifier_df = trainClassifier(X_train, y_train, "RandomForests", classifier, params, accuracy_score)
 results = results.append(classifier_df )<prepare_output>
num = 5
model = [0]*num
for i in range(num):
model[i] = Sequential()
model[i].add(Conv2D(filters = 32, kernel_size =(5,5), padding = "same", activation = "relu", input_shape =(28,28,1)))
 model[i].add(BatchNormalization())
 model[i].add(Conv2D(filters = 32, kernel_size =(5,5), padding = "same", activation = "relu"))
model[i].add(BatchNormalization())
 model[i].add(MaxPool2D(pool_size =(2,2)))
 model[i].add(Dropout(0.25))
model[i].add(Conv2D(filters = 64, kernel_size =(3,3), padding = "same", activation = "relu"))
model[i].add(BatchNormalization())
 model[i].add(Conv2D(filters = 64, kernel_size =(3,3), padding = "same", activation = "relu"))
model[i].add(BatchNormalization())
 model[i].add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model[i].add(Dropout(0.25))
model[i].add(Conv2D(filters = 64, kernel_size =(3,3), padding = "same", activation = "relu"))
model[i].add(BatchNormalization())
 model[i].add(Dropout(0.25))
model[i].add(Flatten())
 model[i].add(Dense(1024, activation = "relu"))
model[i].add(BatchNormalization())
 model[i].add(Dropout(0.25))
model[i].add(Dense(10, activation = "softmax"))
model[i].compile(optimizer = "adam", loss = "categorical_crossentropy", metrics = ["accuracy"])
 model[0].summary()
results = results.set_index("model_name")
 results<save_to_csv>
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
)
classifier = RandomForestClassifier
best_params = results.loc["RandomForests"]["parameters"]
submission_model = classifier(**best_params)
 submission_model.fit(X_train, y_train)
 X_test = pd.get_dummies(test_data[features])
 X_test = imputer.transform(X_test)
 X_test = scaler.transform(X_test)
 y_hat = submission_model.predict(X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': y_hat})
 output.to_csv('submission.csv', index=False)
 print("Your submission was successfully saved!")
<load_from_csv>
epochs = 32
batch_size = 256
history = [0]*num
for i in range(num):
random_seed = i
X_train_, X_val_, Y_train_, Y_val_ = train_test_split(X_train, Y_train, test_size = 0.2, random_state=random_seed)
 learning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001)
 early_stopping = EarlyStopping(monitor='val_accuracy',
min_delta=0,
patience=10,
verbose =1,
mode='auto')
 history[i] = model[i].fit_generator(datagen.flow(X_train_, Y_train_, batch_size = batch_size), epochs = epochs, validation_data =(X_val_,Y_val_), verbose = 1, steps_per_epoch = X_train.shape[0]//batch_size, callbacks=[learning_rate_reduction,early_stopping])
 print("CNN {0:d}: Epochs={1:d}, Train accuracy={2:.5f}, Validation accuracy={3:.5f}".format(i+1,epochs,max(history[i].history['accuracy']),max(history[i].history['val_accuracy'])) )
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head()<load_from_csv>
def predict(X_data):
results = np.zeros(( X_data.shape[0],10))
for j in range(num):
results = results + model[j].predict(X_data)
 return results
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head()<create_dataframe>
test = process_data(test)
 results = predict(test)
 results = np.argmax(results,axis = 1 )
<define_variables><EOS> 
results = pd.Series(results,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("keras_cnn_mnist_aug_ensemble2.csv",index=False )