Kaggle competition: Digit Recognizer
train = DataFrame(train)
 test = DataFrame(test )<split>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
X_train, X_test, y_train, y_test = train_test_split(train, target, train_size=0.90 )<choose_model_class>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
rf = ensemble.RandomForestRegressor()
rf.fit(X_train,y_train)
 y_preds = rf.predict(X_test)
 print(mean_squared_error(y_test,y_preds))<compute_train_metric>
X_train = train.drop('label',axis = 1)
 y_train = train.label
lgbm = LGBMRegressor()
lgbm.fit(X_train,y_train)
 y_pred = lgbm.predict(X_test)
 mse_l = mean_squared_error(y_test,y_pred)
 print(mse_l )<compute_train_metric>
y_train.value_counts()
xgr = xg.XGBRegressor()
xgr.fit(X_train,y_train)
 y_preds = xgr.predict(X_test)
 print(mean_squared_error(y_test,y_preds))<choose_model_class>
X_train.isnull().any().sum()
estimators=[('RandomForest', rf),('LightGBM',lgbm),('xgboost', xgr)]
ensemble = VotingRegressor(estimators )<predict_on_test>
test.isnull().any().sum()
ensemble.fit(X_train,y_train)
 y_preds = ensemble.predict(X_test)
 print(mean_squared_error(y_test,y_preds))<save_to_csv>
X_train /= 255.
 test /= 255 .
lgbm.fit(train, target)
 submission['target'] = lgbm.predict(test)
 submission.to_csv('lgbm.csv')
<load_from_csv>
y_train = to_categorical(y_train, num_classes = 10 )
BASE = ".. /input/tabular-playground-series-feb-2021"
Test = pd.read_csv(BASE + '/test.csv')
 train = pd.read_csv(BASE + '/train.csv')
 sample_sub = pd.read_csv(BASE + '/sample_submission.csv' )<import_modules>
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size = 0.1, random_state= 3 )
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns 
<set_options>
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, BatchNormalization
sns.set_theme() 
<count_missing_values>
model = Sequential()
model.add(Conv2D(filters = 16, kernel_size =(3, 3), activation='relu',
input_shape =(28, 28, 1)))
 model.add(BatchNormalization())
 model.add(Conv2D(filters = 16, kernel_size =(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 32, kernel_size =(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 32, kernel_size =(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(512, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
print('Rows and Columns in train dataset:', sum(train.isnull().sum()))
print('Rows and Columns in test dataset:', sum(Test.isnull().sum()))<define_variables>
optimizer = Adamax(lr=0.001 )
cat_features = [feature for feature in train.columns if 'cat' in feature]
cont_features = [feature for feature in train.columns if 'cont' in feature]<set_options>
model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"] )
warnings.filterwarnings('ignore')
<count_values>
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )
Count_diagram(train )<count_values>
datagen = ImageDataGenerator(
 rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1)
 datagen.fit(X_train )
Count_diagram(Test )<import_modules>
%%time
model.fit_generator(datagen.flow(X_train,y_train, batch_size= 86),
epochs = 100, validation_data =(X_val,y_val),
verbose = 2, steps_per_epoch=X_train.shape[0] // 86,
callbacks=[learning_rate_reduction] )
shap.initjs()<prepare_x_and_y>
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )
<categorify><EOS> 
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("cnn_mnist_datagen.csv",index=False )