Kaggle competition: Rossmann Store Sales
<SOS> metric: RootMeanSquarePercentageError Kaggle data source: rossmann-store-sales<install_modules>

 matplotlib.use("Agg")
 def create_feature_map(features):
outfile = open('xgb.fmap', 'w')
 for i, feat in enumerate(features):
outfile.write('{0}\t{1}\tq
'.format(i, feat))
outfile.close()
def rmspe(y, yhat):
return np.sqrt(np.mean(( yhat/y-1)** 2))
def rmspe_xg(yhat, y):
y = np.expm1(y.get_label())
 yhat = np.expm1(yhat)
 return "rmspe", rmspe(y,yhat)
 def build_features(features, data):
data.fillna(0, inplace=True)
 data.loc[data.Open.isnull() , 'Open'] = 1
features.extend(['Store', 'CompetitionDistance', 'CompetitionOpenSinceMonth',
'CompetitionOpenSinceYear', 'Promo', 'Promo2', 'Promo2SinceWeek',
'Promo2SinceYear'])
 features.append('SchoolHoliday')
 data['SchoolHoliday'] = data['SchoolHoliday'].astype(float)
 features.extend(['StoreType', 'Assortment', 'StateHoliday'])
 mappings = {'0':0, 'a':1, 'b':2, 'c':3, 'd':4}
data.StoreType.replace(mappings, inplace=True)
 data.Assortment.replace(mappings, inplace=True)
 data.StateHoliday.replace(mappings, inplace=True)
 features.extend(['DayOfWeek', 'month', 'day', 'year'])
 data['year'] = data.Date.dt.year
data['month'] = data.Date.dt.month
data['day'] = data.Date.dt.day
data['DayOfWeek'] = data.Date.dt.dayofweek
!pip install -q efficientnet<import_modules>
print("Load the training, test and store data using pandas")
 types = {'CompetitionOpenSinceYear': np.dtype(int),
'CompetitionOpenSinceMonth': np.dtype(int),
'StateHoliday': np.dtype(str),
'Promo2SinceWeek': np.dtype(int),
'SchoolHoliday': np.dtype(float),
'PromoInterval': np.dtype(str)}
train = pd.read_csv(".. /input/train.csv", parse_dates=[2], dtype=types)
 test = pd.read_csv(".. /input/test.csv", parse_dates=[3], dtype=types)
 store = pd.read_csv(".. /input/store.csv")
 print("Assume store open, if not provided")
 train.fillna(1, inplace=True)
 test.fillna(1, inplace=True)
 print("Consider only open stores for training.Closed stores wont count into the score.")
 train = train[train["Open"] != 0]
print("Use only Sales bigger then zero.Simplifies calculation of rmspe")
 train = train[train["Sales"] > 0]
print("Join with store")
 train = pd.merge(train, store, on='Store')
 test = pd.merge(test, store, on='Store')
import math, re, os
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from kaggle_datasets import KaggleDatasets
import tensorflow as tf
import tensorflow.keras.layers as L
import efficientnet.tfkeras as efn
from sklearn import metrics
from sklearn.model_selection import train_test_split<set_options>
features = []
print("augment features")
 build_features(features, train)
 build_features([], test)
 print(features)
 print('training data processed')
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
tpu = None
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 else:
strategy = tf.distribute.get_strategy()
print("REPLICAS: ", strategy.num_replicas_in_sync)
 GCS_DS_PATH = KaggleDatasets().get_gcs_path()
EPOCHS = 40
BATCH_SIZE = 16 * strategy.num_replicas_in_sync<define_variables>
params = {"objective": "reg:linear",
"booster" : "gbtree",
"eta": 0.09,
"max_depth": 10,
"subsample": 0.9,
"colsample_bytree": 0.7,
"silent": 1,
"seed": 1301
}
num_boost_round = 800
print("Train a XGBoost model")
 X_train, X_valid = train_test_split(train, test_size=0.012, random_state=10)
 y_train = np.log1p(X_train.Sales)
 y_valid = np.log1p(X_valid.Sales)
 dtrain = xgb.DMatrix(X_train[features], y_train)
 dvalid = xgb.DMatrix(X_valid[features], y_valid)
 watchlist = [(dtrain, 'train'),(dvalid, 'eval')]
gbm = xgb.train(params, dtrain, num_boost_round, evals=watchlist, \
early_stopping_rounds=100, feval=rmspe_xg, verbose_eval=True)
 print("Validating")
 yhat = gbm.predict(xgb.DMatrix(X_valid[features]))
error = rmspe(X_valid.Sales.values, np.expm1(yhat))
print('RMSPE: {:.6f}'.format(error))
print("Make predictions on the test set")
 dtest = xgb.DMatrix(test[features])
 test_probs = gbm.predict(dtest)
 result = pd.DataFrame({"Id": test["Id"], 'Sales': np.expm1(test_probs)})
 result.to_csv("xgboost_10_submission.csv", index=False)
 create_feature_map(features)
 importance = gbm.get_fscore(fmap='xgb.fmap')
 importance = sorted(importance.items() , key=operator.itemgetter(1))
df = pd.DataFrame(importance, columns=['feature', 'fscore'])
 df['fscore'] = df['fscore'] / df['fscore'].sum()
featp = df.plot(kind='barh', x='feature', y='fscore', legend=False, figsize=(6, 10))
plt.title('XGBoost Feature Importance')
 plt.xlabel('relative importance')
 fig_featp = featp.get_figure()
fig_featp.savefig('feature_importance_xgb.png', bbox_inches='tight', pad_inches=1 )