Kaggle competition: Digit Recognizer
X = train[cat_col + cont_col]
y = train['target']
X_test = test[cat_col + cont_col]<init_hyperparams>
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, BatchNormalization
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
from keras.callbacks import ModelCheckpoint, EarlyStopping
from keras.optimizers import Adam
from keras.utils import np_utils
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ReduceLROnPlateau
lgbm_params = {
'bagging_freq': 1,
'reg_alpha': 2.4766410381355457,
'reg_lambda': 2.644144282261626,
'colsample_bytree': 0.3,
'subsample': 0.6,
'learning_rate': 0.008,
'max_depth': 20,
'num_leaves': 139,
'min_child_samples': 176,
'random_state': 48,
'n_estimators': 20000,
'metric': 'rmse',
'cat_smooth': 9}<train_model>
from sklearn.model_selection import train_test_split
results = np.zeros(X_test.shape[0])
 models = []
loss = []
num = 1
for tr, te in kfold.split(X, y):
print(f'{num} Fold Start')
 X_train, X_val = X.iloc[tr], X.iloc[te]
y_train, y_val = y.iloc[tr], y.iloc[te]
model = LGBMRegressor(**lgbm_params)
 model.fit(X_train, y_train, eval_set=(X_val, y_val), eval_metric = 'rmse', early_stopping_rounds = 500, verbose=500)
 results += model.predict(X_test)
 loss.append(model.best_score_['valid_0']['rmse'])
 models.append(model)
 num += 1
results = results / len(models)
 loss = np.mean(loss )<train_model>
from sklearn.model_selection import train_test_split
print(loss )<load_from_csv>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
 y_train = train["label"]
x_train = train.drop(labels = ["label"],axis = 1)
 y_train.value_counts()
submission = pd.read_csv('.. /input/tabular-playground-series-feb-2021/sample_submission.csv')
 submission['target'] = results
submission<save_to_csv>
x_train = x_train / 255.0
test = test / 255.0
now = datetime.datetime.now().strftime('%Y-%m-%d:%H:%M')
 submission.to_csv(f'./{now}_submission.csv', index= False )<load_from_csv>
random_seed = 2
train = pd.read_csv(input_path / 'train.csv', index_col='id')
 display(train.head() )<load_from_csv>
x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size = 0.1, random_state=random_seed )
test = pd.read_csv(input_path / 'test.csv', index_col='id')
 display(test.head() )<load_from_csv>
print('x_train shape:', x_train.shape)
 print(x_train.shape[0], 'train samples')
 print(x_val.shape[0], 'test samples')
 y_train = np_utils.to_categorical(y_train)
 y_val = np_utils.to_categorical(y_val)
 print("Number of Classes: " + str(y_val.shape[1]))
num_classes = y_val.shape[1]
num_pixels = x_train.shape[1] * x_train.shape[2]
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
padding = "same",
input_shape=(28,28,1)))
 model.add(BatchNormalization())
 model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
padding = "same",
input_shape=(28,28,1)))
 model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(64,(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
optimizer = keras.optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)
 model.compile(loss = 'categorical_crossentropy',
optimizer = optimizer,
metrics = ['accuracy'])
 print(model.summary())
submission = pd.read_csv(input_path / 'sample_submission.csv', index_col='id')
 display(submission.head() )<categorify>
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 )
for c in train.columns:
if train[c].dtype=='object':
lbl = LabelEncoder()
lbl.fit(list(train[c].values)+ list(test[c].values))
train[c] = lbl.transform(train[c].values)
 test[c] = lbl.transform(test[c].values)
 display(train.head() )<drop_column>
batch_size = 128
epochs = 50
learning_rate_reduction = ReduceLROnPlateau(monitor='val_loss',
patience=2,
verbose=1,
factor=0.5,
min_lr=0.00001)
 earlystop = EarlyStopping(monitor = 'val_loss',
min_delta = 0,
patience = 5,
verbose = 1,
restore_best_weights = True)
 callbacks = [earlystop, learning_rate_reduction]
history = model.fit_generator(datagen.flow(x_train,y_train, batch_size=batch_size),
epochs=epochs,
verbose=1,
callbacks=callbacks,
steps_per_epoch = x_train.shape[0] // batch_size,
validation_data =(x_val, y_val))
score = model.evaluate(x_val, y_val, verbose=0)
 print('Test loss:', score[0])
 print('Test accuracy:', score[1] )
target = train.pop('target' )<normalization>
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )
scaler = StandardScaler()
train = scaler.fit_transform(train)
 test = scaler.transform(test )<create_dataframe>
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("2.csv",index=False )