Kaggle competition: Digit Recognizer
from sklearn.model_selection import train_test_split<split>
train = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 test = pd.read_csv("/kaggle/input/digit-recognizer/test.csv" )
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42 )<import_modules>
Y_train = train["label"]
X_train = train.drop(labels = ["label"],axis = 1)
 X_train = X_train / 255.0
test = test / 255.0
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV<define_search_space>
X_train = X_train.values.reshape(-1,28,28,1)
 test = test.values.reshape(-1,28,28,1 )
param_grid = {'max_depth':[4,5,6,7,8,9,10]}<train_on_grid>
Y_train = keras.utils.to_categorical(Y_train, 10 )
forest = RandomForestClassifier(random_state=42)
 grid = GridSearchCV(forest, param_grid, cv=10)
 grid.fit(X_train, y_train )<find_best_params>
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1 )
grid.best_params_<import_modules>
batch_size = 86
num_classes = 10
epochs = 10
input_shape =(28, 28, 1 )
from sklearn.metrics import accuracy_score, classification_report<predict_on_test>
batch_size = 86
num_classes = 10
epochs = 10
input_shape =(28, 28, 1)
 model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',kernel_initializer='he_normal',input_shape=input_shape))
model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',kernel_initializer='he_normal'))
model.add(MaxPool2D(( 2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(Conv2D(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"))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.RMSprop() ,
metrics=['accuracy'])
 learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.0001 )
test_predictions = grid.predict(X_test)
 print(accuracy_score(y_test, test_predictions))<compute_test_metric>
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 )
print(classification_report(y_test, test_predictions))<categorify>
mnist_test = pd.read_csv(".. /input/mnist-in-csv/mnist_test.csv")
 mnist_train = pd.read_csv(".. /input/mnist-in-csv-train/mnist_train.csv" )
X_final = complete_df[891:].drop(['PassengerId', 'Survived', 'Name', 'Ticket', 'Cabin'] ,axis=1)
 X_final = pd.get_dummies(X_final )<predict_on_test>
sample_submission = pd.read_csv(".. /input/digit-recognizer/sample_submission.csv")
 test = pd.read_csv(".. /input/digit-recognizer/test.csv")
 train = pd.read_csv(".. /input/digit-recognizer/train.csv" )
forest = RandomForestClassifier(max_depth=6, random_state=42)
 forest.fit(X,y)
 final_preds = forest.predict(X_final )<load_from_csv>
test['dataset'] = 'test'
submission = pd.read_csv('.. /input/titanic/gender_submission.csv')
 submission<prepare_output>
train['dataset'] = 'train'
submission['Survived'] = final_preds
submission<data_type_conversions>
dataset = pd.concat([train.drop('label', axis=1), test] ).reset_index()
submission['Survived'] = submission['Survived'].astype(int)
 submission<save_to_csv>
mnist = pd.concat([mnist_train, mnist_test] ).reset_index(drop=True)
 labels = mnist['label'].values
mnist.drop('label', axis=1, inplace=True)
 mnist.columns = cols
submission.to_csv('submission.csv', index=False )<save_to_csv>
idx_mnist = mnist.sort_values(by=list(mnist.columns)).index
dataset_from = dataset.sort_values(by=list(mnist.columns)) ['dataset'].values
original_idx = dataset.sort_values(by=list(mnist.columns)) ['index'].values
submission.to_csv('submission.csv', index=False )<set_options>
for i in range(len(idx_mnist)) :
if dataset_from[i] == 'test':
sample_submission.loc[original_idx[i], 'Label'] = labels[idx_mnist[i]]
sns.set_style("whitegrid")
 %matplotlib inline<set_options>
sample_submission.to_csv('submission.csv', index=False )