Kaggle competition: Digit Recognizer
test_data.loc[test_data['Type']=='MB', 'Type'] = 'DT'<data_type_conversions>
sample_sub = '.. /input/digit-recognizer/sample_submission.csv'
test = '.. /input/digit-recognizer/test.csv'
train = '.. /input/digit-recognizer/train.csv'
df['Open Date'] = pd.to_datetime(df['Open Date'])
 test_data['Open Date'] = pd.to_datetime(test_data['Open Date'] )<drop_column>
%matplotlib inline
df.drop('Id',axis=1,inplace=True)
 df.drop('City', axis=1, inplace = True)
 test_data.drop('City', axis = 1, inplace = True)
 df.head()<feature_engineering>
train_data = pd.read_csv(train)
 test_data = pd.read_csv(test )
launch_date = datetime.datetime(2015, 3, 23)
 df['Days Open'] =(launch_date - df['Open Date'] ).dt.days / 1000
test_data['Days Open'] =(launch_date - test_data['Open Date'] ).dt.days / 1000
df.drop('Open Date', axis=1, inplace=True)
 test_data.drop('Open Date', axis=1, inplace=True )<categorify>
y_train = train_data ['label']
X_train = train_data.drop(labels = ["label"], axis = 1)
 X_test = test_data
columnsToEncode = df.select_dtypes(include=[object] ).columns
df = pd.get_dummies(df, columns=columnsToEncode, drop_first=False)
 test_data = pd.get_dummies(test_data, columns=columnsToEncode, drop_first=False )<split>
X_train = X_train/255
X_test = X_test/255
df['revenue'] = np.log1p(df['revenue'])
 X, y = df.drop('revenue', axis=1), df['revenue']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42 )<train_model>
y_train = to_categorical(y_train, num_classes = 10 )
clf = LinearRegression().fit(X_train, y_train)
 clf.score(X_train, y_train )<train_on_grid>
datagen = ImageDataGenerator(
 rotation_range = 10,
zoom_range = 0.1,
width_shift_range = 0.1,
height_shift_range = 0.1 )
params_grid = {
'max_depth': [10, 30, 35, 50, 65, 75, 100],
'max_features': [.3,.4,.5,.6],
'min_samples_leaf': [3, 4, 5],
'min_samples_split': [8, 10, 12],
'n_estimators': [30, 50, 100, 200]
}
rf_regressor = RandomForestRegressor()
rf_cv_regressor = GridSearchCV(rf_regressor, params_grid, scoring='neg_root_mean_squared_error', cv = 10, n_jobs = -1)
 rf_cv_regressor.fit(X_train, y_train)
 print('Best params {}'.format(rf_cv_regressor.best_params_))
print('Best score {}'.format(rf_cv_regressor.best_score_))<train_model>
model = Sequential()
model.add(Conv2D(32,kernel_size=3, activation='relu', input_shape=(28,28,1)))
 model.add(BatchNormalization())
 model.add(Conv2D(32,kernel_size=3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(32,kernel_size=5, strides=2, padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Conv2D(64, kernel_size=3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size=3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size=5, strides=2, padding='same' ,activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Flatten())
 model.add(Dense(128, activation ='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
rf_regressor = RandomForestRegressor(max_depth = 10,
max_features = 0.6,
min_samples_leaf = 3,
min_samples_split = 8,
n_estimators = 50)
 rf_regressor.fit(X_train, y_train )<compute_test_metric>
model.compile(optimizer = "adam", loss = "categorical_crossentropy", metrics = ["accuracy"] )
y_train_pred = rf_regressor.predict(X_train)
 y_pred = rf_regressor.predict(X_test)
 train_rmse = np.sqrt(mean_squared_error(y_train_pred, y_train))
test_rmse = np.sqrt(mean_squared_error(y_test, y_pred))
print(f'Train RMSE: {train_rmse:.4f}')
 print(f'Test RMSE: {test_rmse:.4f}' )<save_to_csv>
X_train, X_val, Y_train, Y_val = train_test_split(X_train, y_train, test_size = 0.2, random_state = 64 )
submission = pd.DataFrame(columns=['Id','Prediction'])
 submission['Id'] = test_data['Id']
rf_pred = rf_regressor.predict(test_data.drop('Id', axis=1))
submission['Prediction'] = np.expm1(rf_pred)
 submission.to_csv('submission_rf.csv',index=False )<import_modules>
history = model.fit_generator(datagen.flow(X_train, Y_train, batch_size = 64),
epochs = 50, steps_per_epoch = X_train.shape[0]//64, validation_data =(X_val, Y_val), verbose=1 )
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename)) 
<load_from_csv>
predictions = model.predict(X_test)
 predictions = np.argmax(predictions, axis = 1)
 predictions = pd.Series(predictions, name = "Label" )
<prepare_x_and_y><EOS> 
submission = pd.concat([pd.Series(range(1, 28001), name = "ImageId"), predictions], axis = 1)
 submission.to_csv("MNIST_top_CNN_submission.csv", index = False )