Kaggle competition: Digit Recognizer
train = train.drop(['1stFlrSF', 'GarageArea', 'TotRmsAbvGrd'], axis = 1)
 test = test.drop(['1stFlrSF', 'GarageArea', 'TotRmsAbvGrd'], axis = 1 )<drop_column>
df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv' )
train.drop(['MiscVal', 'MoSold','YrSold'], axis = 1, inplace = True)
 test.drop(['MiscVal', 'MoSold','YrSold'], axis = 1, inplace = True )<prepare_x_and_y>
y_train = df_train['label']
x_train = df_train.drop(labels = ['label'] , axis=1)
 del df_train
X = train.drop(['SalePrice'], axis = 1)
 col_to_use = list(X.columns)
 y = train['SalePrice']
print(X.shape)
 print(y.shape )<define_variables>
y_train.value_counts()
num_cols = [col for col in col_to_use if train[col].dtype in ['int64', 'float64']]
cat_cols = [col for col in col_to_use if train[col].dtype == 'object']
num_cols<choose_model_class>
x_train = x_train/255.0
df_test = df_test/255.0
num_processor = Pipeline(steps = [ 
('imputer', SimpleImputer(strategy='most_frequent')) , 
('scaler', MinMaxScaler())
 ] )<categorify>
y_train = to_categorical(y_train , num_classes = 10 )
cat_processor = Pipeline(steps = [ 
('imputer', SimpleImputer(strategy = 'most_frequent')) , 
('ohe', OneHotEncoder(handle_unknown = 'ignore', sparse = False))
] )<feature_engineering>
x_train , x_val , y_train , y_val = train_test_split(x_train , y_train , test_size =.1 , random_state = 0 )
preprocessor = ColumnTransformer([ 
('num', num_processor, num_cols), 
('cat', cat_processor, cat_cols)
 ] )<split>
classifier = Sequential()
classifier.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu', input_shape =(28,28,1)))
 classifier.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu'))
classifier.add(MaxPool2D(pool_size=(2,2)))
 classifier.add(Dropout(0.25))
classifier.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
classifier.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
classifier.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 classifier.add(Dropout(0.25))
classifier.add(Flatten())
 classifier.add(Dense(256, activation = "relu"))
classifier.add(Dropout(0.5))
classifier.add(Dense(10, activation = "softmax"))
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 42 )<choose_model_class>
classifier.compile(optimizer = 'adam' , loss = "categorical_crossentropy", metrics=["accuracy"] )
model = LinearRegression()
model1 = Lasso()
model2 = Ridge()
model3 = DecisionTreeRegressor(max_leaf_nodes = 30, random_state = 42)
 model4 = RandomForestRegressor(n_estimators = 500, random_state = 42)
 model5 = XGBRegressor(n_estimators = 1000, learning_rate = 0.05, random_state = 42)
 model6 = GradientBoostingRegressor(learning_rate = 0.1, n_estimators = 165, random_state=42)
 model7 = CatBoostRegressor(random_state=0,verbose=False, depth = 5, early_stopping_rounds=300, learning_rate= 0.1 )<compute_train_metric>
epochs = 30
batch_size = 126
def build_model(model):
clf = Pipeline(steps = [ 
('preprocessor', preprocessor), 
('model', model)
 ])
 clf.fit(X_train, y_train)
 print(model)
 print("Train set score:", clf.score(X_train, y_train))
print("Test set score:", clf.score(X_test, y_test))
print("
")
 print("Train set rmse:", mean_squared_error(y_train, clf.predict(X_train),squared=False))
print("Test set rmse:", mean_squared_error(y_test, clf.predict(X_test),squared=False))
print("
")
 predictions = clf.predict(test)
 return predictions<load_pretrained>
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 )
predictions = build_model(model7 )<prepare_output>
history = classifier.fit_generator(datagen.flow(x_train,y_train, batch_size=batch_size),
epochs = epochs, validation_data =(x_val,y_val),
verbose = 2, steps_per_epoch=x_train.shape[0] // batch_size
)
predictions = np.exp(predictions )<save_to_csv>
results = classifier.predict(df_test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )
output = pd.DataFrame({'Id': Id, 'SalePrice': predictions})
 output.to_csv('submission.csv', index = False)
 sub = pd.read_csv('./submission.csv')
 sub<import_modules>
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("cnn_mnist_datagen.csv",index=False )