Kaggle competition: Allstate Claims Severity
LR_START = 0.0001
LR_MAX = 0.00005 * strategy.num_replicas_in_sync
LR_MIN = 0.00001
LR_RAMPUP_EPOCHS = 4
LR_SUSTAIN_EPOCHS = 6
LR_EXP_DECAY =.8
def lrfn(epoch):
if epoch < LR_RAMPUP_EPOCHS:
lr = np.random.random_sample() * LR_START
elif epoch < LR_RAMPUP_EPOCHS + LR_SUSTAIN_EPOCHS:
lr = LR_MAX
else:
lr =(LR_MAX - LR_MIN)* LR_EXP_DECAY**(epoch - LR_RAMPUP_EPOCHS - LR_SUSTAIN_EPOCHS)+ LR_MIN
return lr
lr_callback = LearningRateScheduler(lrfn, verbose=True )<choose_model_class>
train=pd.read_csv('/kaggle/input/allstate-claims-severity/train.csv', index_col='id')
 test=pd.read_csv('/kaggle/input/allstate-claims-severity/test.csv', index_col='id')
 submission=pd.read_csv('/kaggle/input/allstate-claims-severity/sample_submission.csv', index_col='id')
 print(train.shape, test.shape, submission.shape )
checkpoint = ModelCheckpoint(filepath='best_model.h5', monitor='loss',mode='min', save_best_only=True, verbose=1 )<train_model>
train=train.drop(train.loc[train['loss']>40000].index )
if VALIDATION:
history1 = model1.fit(get_training_dataset() ,
steps_per_epoch=STEPS_PER_EPOCH,
epochs=40,
validation_data=get_validation_dataset() ,
callbacks = [checkpoint, lr_callback] 
)
 model1.load_weights("best_model.h5")
 else:
history1 = model1.fit(get_training_dataset() ,
steps_per_epoch=STEPS_PER_EPOCH,
epochs=30,
callbacks = [checkpoint, lr_callback])
 model1.load_weights("best_model.h5" )<train_model>
train['loss']=np.log(train['loss']+1)
 Ytrain=train['loss']
lr_callback = LearningRateScheduler(lrfn, verbose=True)
 checkpoint = ModelCheckpoint(filepath='best_model.h5', monitor='loss',mode='min', save_best_only=True, verbose=1)
 if VALIDATION:
history2 = model2.fit(get_training_dataset() ,
steps_per_epoch=STEPS_PER_EPOCH,
epochs=40,
validation_data=get_validation_dataset() ,
callbacks = [checkpoint, lr_callback] 
)
 model2.load_weights("best_model.h5")
 else:
history2 = model2.fit(get_training_dataset() ,
steps_per_epoch=STEPS_PER_EPOCH,
epochs=35,
callbacks = [checkpoint, lr_callback] 
)
 model2.load_weights("best_model.h5")
<train_model>
data=train
train=train[list(test)]
all_data=pd.concat(( train, test))
all_data.shape
lr_callback = LearningRateScheduler(lrfn, verbose=True)
 checkpoint = ModelCheckpoint(filepath='best_model.h5', monitor='loss',mode='min', save_best_only=True, verbose=1)
 if VALIDATION:
history3 = model3.fit(get_training_dataset() ,
steps_per_epoch=STEPS_PER_EPOCH,
epochs=40,
validation_data=get_validation_dataset() ,
callbacks = [checkpoint, lr_callback] 
)
 model3.load_weights("best_model.h5")
 else:
history3 = model3.fit(get_training_dataset() ,
steps_per_epoch=STEPS_PER_EPOCH,
epochs=35,
callbacks = [checkpoint, lr_callback] 
)
 model3.load_weights("best_model.h5")
<predict_on_test>
cat_features=list(np.where(all_data.dtypes==np.object)[0])
 print(cat_features )
if VALIDATION:
cmdataset = get_validation_dataset(ordered=True)
 images_ds = cmdataset.map(lambda image, label: image)
 labels_ds = cmdataset.map(lambda image, label: label ).unbatch()
cm_correct_labels = next(iter(labels_ds.batch(NUM_VALIDATION_IMAGES)) ).numpy()
cm_probabilities =(model1.predict(images_ds)+model2.predict(images_ds)) /2
cm_predictions = np.argmax(cm_probabilities, axis=-1)
 print("Correct labels: ", cm_correct_labels.shape, cm_correct_labels)
 print("Predicted labels: ", cm_predictions.shape, cm_predictions )<compute_test_metric>
le=LabelEncoder()
non_numeric=list(all_data.select_dtypes(np.object))
for cols in non_numeric:
le.fit(all_data[cols])
 all_data[cols]=le.transform(all_data[cols] )
if VALIDATION:
cmat = confusion_matrix(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)))
 score = f1_score(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)) , average='macro')
 precision = precision_score(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)) , average='macro')
 recall = recall_score(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)) , average='macro')
 display_confusion_matrix(cmat, score, precision, recall)
 print('f1 score: {:.3f}, precision: {:.3f}, recall: {:.3f}'.format(score, precision, recall))<save_to_csv>
from catboost import CatBoostRegressor
from xgboost import XGBRegressor
from lightgbm import LGBMRegressor
from sklearn.model_selection import cross_val_score
test_ds = get_test_dataset(ordered=True)
 print('Computing predictions...')
 test_images_ds = test_ds.map(lambda image, idnum: image)
 probabilities =(model1.predict(test_images_ds)+model2.predict(test_images_ds)+model3.predict(test_images_ds)) /3
predictions = np.argmax(probabilities, axis=-1)
 print(predictions)
 print('Generating submission.csv file...')
 test_ids_ds = test_ds.map(lambda image, idnum: idnum ).unbatch()
test_ids = next(iter(test_ids_ds.batch(NUM_TEST_IMAGES)) ).numpy().astype('U')
 np.savetxt('submission.csv', np.rec.fromarrays([test_ids, predictions]), fmt=['%s', '%d'], delimiter=',', header='id,label', comments='')
 !head submission.csv<define_variables>
model_xgb=XGBRegressor(tree_method='gpu_hist', seed=18, objective='reg:linear', n_jobs=-1, verbosity=0,
colsample_bylevel=0.764115402027029, colsample_bynode=0.29243734009596956,
colsample_bytree= 0.7095719673041723, gamma= 4.127534050725986, learning_rate= 0.02387231810322894,
max_depth=14, min_child_weight=135, n_estimators=828,reg_alpha=0.3170105723222332,
reg_lambda= 0.3660379465131937, subsample=0.611471430211575)
 model_xgb
if VALIDATION:
dataset = get_validation_dataset()
dataset = dataset.unbatch().batch(20)
 batch = iter(dataset )<predict_on_test>
model_LGB=LGBMRegressor(objective='regression_l1', random_state=18, subsample_freq=1,
colsample_bytree=0.3261853512759363, min_child_samples=221, n_estimators=2151, num_leaves= 45,
reg_alpha=0.9113713668943361, reg_lambda=0.8220990333713991, subsample=0.49969995651550947,
max_bin=202, learning_rate=0.02959820893211799)
 model_LGB
if VALIDATION:
images, labels = next(batch)
 probabilities =(model1.predict(images)+model2.predict(images)) /2
predictions = np.argmax(probabilities, axis=-1)
 display_batch_of_images(( images, labels), predictions )<import_modules>
model_Cat=CatBoostRegressor(loss_function='MAE', random_seed=18, task_type='GPU', cat_features=cat_features, verbose=False,
iterations=2681, learning_rate=0.2127106032536721, depth=7, l2_leaf_reg=5.266150673910493,
random_strength=7.3001140226199315, bagging_temperature=0.26098669708900213)
 model_Cat
import numpy as np
import pandas as pd
import gc
import os 
<set_options>
model_Cat.fit(Xtrain, Ytrain)
 model_LGB.fit(Xtrain, Ytrain)
 model_xgb.fit(Xtrain, Ytrain)
 lgb_predictions=model_LGB.predict(Xtest)
 cat_predictions=model_Cat.predict(Xtest)
 xgb_predictions=model_xgb.predict(Xtest )
!pip install -q efficientnet
seed = 42
def seed_everything(seed):
os.environ['PYTHONHASHSEED']=str(seed)
 random.seed(seed)
 np.random.seed(seed)
 seed_everything(seed)
 print("Tensorflow version " + tf.__version__)
 AUTO = tf.data.experimental.AUTOTUNE<set_options>
predictions=(lgb_predictions + cat_predictions + xgb_predictions)/3
predictions=np.exp(predictions)-1
submission['loss']=predictions
submission.to_csv('Result.csv')
 submission.head()