Kaggle competition: Digit Recognizer
sample = pd.read_csv(".. /input/tabular-playground-series-feb-2021/sample_submission.csv")
 print(sample.shape )<find_best_model_class>
class SnapshotEnsemble(Callback):
__snapshot_name_fmt = "snapshot_%d.hdf5"
def __init__(self, n_models, n_epochs_per_model, lr_max, verbose=1):

 self.n_epochs_per_model = n_epochs_per_model
self.n_models = n_models
self.n_epochs_total = self.n_models * self.n_epochs_per_model
self.lr_max = lr_max
self.verbose = verbose
self.lrs = []
def cosine_annealing(self, epoch):
cos_inner =(math.pi *(epoch % self.n_epochs_per_model)) / self.n_epochs_per_model
return self.lr_max / 2 *(math.cos(cos_inner)+ 1)
 def on_epoch_begin(self, epoch, logs={}):
lr = self.cosine_annealing(epoch)
 backend.set_value(self.model.optimizer.lr, lr)
 self.lrs.append(lr)
 def on_epoch_end(self, epoch, logs={}):
if(epoch + 1)% self.n_epochs_per_model == 0:
filename = self.__snapshot_name_fmt %(( epoch + 1)// self.n_epochs_per_model)
 self.model.save(filename)
 if self.verbose:
print('Epoch %d: snapshot saved to %s' %(epoch, filename))
def load_ensemble(self):
models = []
for i in range(self.n_models):
models.append(load_model(self.__snapshot_name_fmt %(i + 1)))
 return models
preds = np.zeros(( sample.shape[0]))
kf = KFold(n_splits=5,random_state=48,shuffle=True)
 for trn_idx, test_idx in kf.split(train[columns],target):
X_tr,X_val=train[columns].iloc[trn_idx],train[columns].iloc[test_idx]
y_tr,y_val=target.iloc[trn_idx],target.iloc[test_idx]
model = xgb.XGBRegressor(**Best_trial)
 model.fit(X_tr,y_tr,eval_set=[(X_val,y_val)],early_stopping_rounds=100,verbose=False)
 preds+=model.predict(test[columns])/kf.n_splits
rmse=mean_squared_error(y_val, model.predict(X_val),squared=False)
 print(rmse )<save_to_csv>
path = '/kaggle/input/digit-recognizer/'
train = pd.read_csv(path + 'train.csv')
 test = pd.read_csv(path + 'test.csv')
 target = train['label']
train.drop(columns=['label'], inplace=True)
 im_size = 28
train = train.to_numpy().reshape(( -1, im_size, im_size, 1))
test = test.to_numpy().reshape(( -1, im_size, im_size, 1))
train = train / 255
test = test / 255
train.shape, test.shape
subm = sample
subm['target'] = preds
subm.to_csv('submission.csv',index=False)
 subm<load_from_csv>
target = to_categorical(target)
 x_train, x_test, y_train, y_test = train_test_split(train, target, test_size=0.2, random_state=289)
 x_train.shape, x_test.shape
warnings.filterwarnings("ignore")
 data = pd.read_csv(".. /input/tabular-playground-series-feb-2021/train.csv")
 test = pd.read_csv(".. /input/tabular-playground-series-feb-2021/test.csv")
<load_from_csv>
model = Sequential()
model.add(Conv2D(16, 3, activation='relu', padding='same', input_shape=(im_size, im_size, 1)))
 model.add(Dropout(0.5))
model.add(Conv2D(16, 3, activation='relu', padding='same'))
model.add(Dropout(0.5))
model.add(Conv2D(16, 5, activation='relu', padding='same'))
model.add(MaxPooling2D())
 model.add(Conv2D(32, 3, activation='relu', padding='same'))
model.add(Dropout(0.5))
model.add(Conv2D(32, 3, activation='relu', padding='same'))
model.add(Dropout(0.5))
model.add(Conv2D(32, 5, activation='relu', padding='same'))
model.add(MaxPooling2D())
 model.add(Conv2D(64, 3, activation='relu', padding='same'))
model.add(Dropout(0.5))
model.add(Conv2D(64, 3, activation='relu', padding='same'))
model.add(Dropout(0.5))
model.add(Conv2D(64, 5, activation='relu', padding='same'))
model.add(MaxPooling2D())
 model.add(Flatten())
 model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.summary()
model.compile(
 loss='categorical_crossentropy',
optimizer='adam',
metrics=['acc']
)
train = pd.read_csv(DATA / "train.csv")
 test = pd.read_csv(DATA / "test.csv")
 smpl_sub = pd.read_csv(DATA / "sample_submission.csv")
 print("train: {}, test: {}, sample sub: {}".format(
 train.shape, test.shape, smpl_sub.shape 
))<prepare_output>
imagegen = ImageDataGenerator(
 rotation_range=15,
width_shift_range=0.2,
height_shift_range=0.2,
zoom_range=0.2
)
data.set_index("id",inplace=True)
 test.set_index("id",inplace=True )<compute_test_metric>
se_callback = SnapshotEnsemble(n_models=7, n_epochs_per_model=50, lr_max=.001)
 history = model.fit_generator(
 imagegen.flow(x_train, y_train, batch_size=32),
steps_per_epoch=len(x_train)/ 32,
epochs=se_callback.n_epochs_total,
verbose=0,
callbacks=[se_callback],
validation_data=(x_test, y_test)
 )
def rmse(y_true, y_pred):

 return np.sqrt(np.mean(( y_true - y_pred)** 2))<categorify>
def predict(models, data, weights=None):
if weights is None:
weights = [1 /(len(models)) ] * len(models)
 pred = np.zeros(( data.shape[0], 10))
for i, model in enumerate(models):
pred += model.predict(data)* weights[i]
return pred
def evaluate(preds, weights=None):
if weights is None:
weights = [1 / len(preds)] * len(preds)
 y_pred = np.zeros(( y_test.shape[0], 10))
for i, pred in enumerate(preds):
y_pred += pred * weights[i]
y_pred = np.argmax(y_pred, axis=1)
 y_true = np.argmax(y_test, axis=1)
 return accuracy_score(y_true, y_pred)
 models = se_callback.load_ensemble()
preds = []
for i, model in enumerate(models):
pred = predict([model], x_test)
 preds.append(pred)
 score = evaluate([pred])
 print(f'model {i + 1}: accuracy = {score:.4f}')
 ensemble_score = evaluate(preds)
 print(f'ensemble: accuracy = {ensemble_score:.4f}' )
def enc_scl_pipe(X_train, y_train, X_test, enc_method, scaler = StandardScaler()):
X_train_encoded = X_train.copy()
X_test_encoded= X_test.copy()
feature_to_encode = X_train.columns[X_train.dtypes == 'O'].tolist()
if enc_method == 'label':
for feat in feature_to_encode:
lbEncoder = LabelEncoder()
lbEncoder.fit(X_train[feat])
 X_train_encoded[feat] = lbEncoder.transform(X_train[feat])
 X_test_encoded[feat] = lbEncoder.transform(X_test[feat])
 elif enc_method == 'glmm':
GLMMEncoder = ce.glmm.GLMMEncoder(verbose =0 ,binomial_target=False)
 GLMMEncoder.fit(X_train[feature_to_encode],y_train)
 X_train_encoded[feature_to_encode] = GLMMEncoder.transform(X_train[feature_to_encode])
 X_test_encoded[feature_to_encode] = GLMMEncoder.transform(X_test[feature_to_encode])
 else:
raise 'No encoding method stated'
scaler.fit(X_train_encoded)
 X_train_scaled = pd.DataFrame(scaler.transform(X_train_encoded), columns=X_train_encoded.columns, index=X_train_encoded.index)
 X_test_scaled = pd.DataFrame(scaler.transform(X_test_encoded), columns=X_test_encoded.columns, index=X_test_encoded.index)
 return X_train_scaled, X_test_scaled, feature_to_encode<compute_train_metric>
best_score = ensemble_score
best_weights = None
no_improvements = 0
while no_improvements < 5000:
new_weights = np.random.uniform(size=(len(models),))
new_weights /= new_weights.sum()
new_score = evaluate(preds, new_weights)
 if new_score > best_score:
no_improvements = 0
best_score = new_score
best_weights = new_weights
print(f'improvement: {best_score:.4f}')
 else:
no_improvements += 1
print(f'best weights are {best_weights}' )
<init_hyperparams><EOS> 
pred = predict(models, test, best_weights)
 res = pd.DataFrame()
res['ImageId'] = np.arange(test.shape[0])+ 1
res['Label'] = np.argmax(pred, axis=1)
 res.to_csv('submission.csv', index=False)
 res.head(15 )