Kaggle competition: Rossmann Store Sales
<SOS> metric: RootMeanSquarePercentageError Kaggle data source: rossmann-store-sales<set_options>
import numpy as np
import pandas as pd
import datetime
from xgboost import XGBRegressor
from sklearn.model_selection import GridSearchCV, KFold
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 )<define_variables>
train = pd.read_csv(r'.. /input/rossmann-store-sales/train.csv', parse_dates=['Date'], low_memory=False)
 train.head()
AUTO = tf.data.experimental.AUTOTUNE
EPOCHS_1 = 20
EPOCHS_2 = 2
BATCH_SIZE = 16 * strategy.num_replicas_in_sync
MAX_LEN = 192
SHUFFLE = 2048
VERBOSE = 1<load_from_csv>
test = pd.read_csv(r'.. /input/rossmann-store-sales/test.csv', parse_dates=['Date'], low_memory=False, index_col='Id')
 test.head()
train1 = pd.read_csv("/kaggle/input/jigsaw-multilingual-toxic-comment-classification/jigsaw-toxic-comment-train.csv")
 train2 = pd.read_csv("/kaggle/input/jigsaw-multilingual-toxic-comment-classification/jigsaw-unintended-bias-train.csv")
 train2.toxic = train2.toxic.round().astype(int)
 valid = pd.read_csv('/kaggle/input/val-en-df/validation_en.csv')
 test1 = pd.read_csv('/kaggle/input/test-en-df/test_en.csv')
 test2 = pd.read_csv('/kaggle/input/jigsaw-multilingual-toxic-test-translated/jigsaw_miltilingual_test_translated.csv')
 sub = pd.read_csv('/kaggle/input/jigsaw-multilingual-toxic-comment-classification/sample_submission.csv')
 sub1 = pd.read_csv('/kaggle/input/jigsaw-multilingual-toxic-comment-classification/sample_submission.csv')
 sub2 = pd.read_csv('/kaggle/input/jigsaw-multilingual-toxic-comment-classification/sample_submission.csv' )<feature_engineering>
store = pd.read_csv(r'.. /input/rossmann-store-sales/store.csv', index_col='Store')
 store.head()
train['comment_text'] = train.apply(lambda x: clean_text(x['comment_text']), axis=1)
 valid['comment_text_en'] = valid.apply(lambda x: clean_text(x['comment_text_en']), axis=1)
 test1['content'] = test1.apply(lambda x: clean_text(x['content_en']), axis=1)
 test2['content'] = test2.apply(lambda x: clean_text(x['translated']), axis=1)
 train['comment_text'] = train['comment_text'].apply(lambda x: text_process(x))
valid['comment_text_en'] = valid['comment_text_en'].apply(lambda x: text_process(x))
test1['content'] = test1['content_en'].apply(lambda x: text_process(x))
test2['content'] = test2['translated'].apply(lambda x: text_process(x))<prepare_x_and_y>
mean_dist = store['CompetitionDistance'].mean()
store.loc[store['CompetitionDistance'].isnull() , 'CompetitionDistance'] = mean_dist
store.loc[store['Promo2'] == 0, ['Promo2SinceWeek','Promo2SinceYear']] = 0
store['Promo2SinceWeek'] = store['Promo2SinceWeek'].astype('int')
 store['Promo2SinceYear'] = store['Promo2SinceYear'].astype('int')
 store['CompetitionOpen'] = 1
store.loc[store['CompetitionOpenSinceMonth'].isnull() ,['CompetitionOpen', 'CompetitionOpenSinceMonth', 'CompetitionOpenSinceYear']] = 0
store['CompetitionOpenSinceMonth'] = store['CompetitionOpenSinceMonth'].astype('int')
 store['CompetitionOpenSinceYear'] = store['CompetitionOpenSinceYear'].astype('int')
 label_map = {'PromoInterval' : {'Jan,Apr,Jul,Oct' : 1,
'Feb,May,Aug,Nov' : 2,
'Mar,Jun,Sept,Dec' : 3,
np.nan : 0},
'Assortment' : {'a':0,
'b':1,
'c':2}}
store.replace(label_map, inplace=True )
x_train = fast_encode(train1.comment_text.astype(str), fast_tokenizer, maxlen=MAX_LEN)
 x_valid = fast_encode(valid.comment_text_en.astype(str), fast_tokenizer, maxlen=MAX_LEN)
 x_test1 = fast_encode(test1.content_en.astype(str), fast_tokenizer, maxlen=MAX_LEN)
 x_test2 = fast_encode(test2.translated.astype(str), fast_tokenizer, maxlen=MAX_LEN)
 y_train = train1.toxic.values
y_valid = valid.toxic.values<categorify>
train.loc[train['StateHoliday'] != '0', 'StateHoliday'] = '1'
train['StateHoliday'] = train['StateHoliday'].astype('int')
 test.loc[test['StateHoliday'] != '0', 'StateHoliday'] = '1'
test['StateHoliday'] = test['StateHoliday'].astype('int')
 train['year'] = train['Date'].dt.year
train['month'] = train['Date'].dt.month
train['day'] = train['Date'].dt.day
test['year'] = test['Date'].dt.year
test['month'] = test['Date'].dt.month
test['day'] = test['Date'].dt.day
test.loc[test['Open'].isnull() , 'Open'] = 1
test['Open'] = test['Open'].astype('int')
 train.drop('Customers', axis=1, inplace=True )
train_dataset =(
 tf.data.Dataset 
.from_tensor_slices(( x_train, y_train)) 
.repeat() 
.shuffle(SHUFFLE)
.batch(BATCH_SIZE)
.prefetch(AUTO)
)
 valid_dataset =(
 tf.data.Dataset 
.from_tensor_slices(( x_valid, y_valid)) 
.batch(BATCH_SIZE)
.cache() 
.prefetch(AUTO)
)
 test1_dataset =(
 tf.data.Dataset 
.from_tensor_slices(x_test1)
.batch(BATCH_SIZE)
)
 test2_dataset =(
 tf.data.Dataset 
.from_tensor_slices(x_test2)
.batch(BATCH_SIZE)
 )<choose_model_class>
kfold = KFold(n_splits=5, random_state=2021, shuffle=True)
 parameters = {'learning_rate' : [0.1,0.2,0.35]}
clf = XGBRegressor(random_state=2021, use_label_encoder=False, n_estimators=100, max_depth=4 )
lrs = ReduceLROnPlateau(monitor='val_accuracy', mode ='max', factor = 0.7, min_lr= 1e-7, verbose = 1, patience = 2)
 es1 = EarlyStopping(monitor='val_accuracy', mode='max', verbose = 1, patience = 5, restore_best_weights=True)
 es2 = EarlyStopping(monitor='accuracy', mode='max', verbose = 1, patience = 1, restore_best_weights=True)
 callbacks_list1 = [lrs,es1]
callbacks_list2 = [lrs,es2]<choose_model_class>
submit_frame = pd.DataFrame(columns=['Id','Sales'] )
def build_model(transformer, max_len=512):
input_word_ids = Input(shape=(max_len,), dtype=tf.int32, name="input_word_ids")
 sequence_output = transformer(input_word_ids)[0]
cls_token = sequence_output[:, 0, :]
x = tf.keras.layers.Dropout(0.4 )(cls_token)
 out = Dense(1, activation='sigmoid' )(cls_token)
 model = Model(inputs=input_word_ids, outputs=out)
 model.compile(Adam(lr=1e-5), loss='binary_crossentropy', metrics=[tf.keras.metrics.AUC(name='auc'), 'accuracy'])
 return model<train_model>
for st_no in range(1,1116):
train_t = train.loc[train['Store'] == st_no].copy()
train_t.drop('Store', axis=1, inplace=True)
 test_t = test.loc[test['Store'] == st_no].copy()
test_t.drop('Store',axis=1,inplace=True)
 st_t = store.loc[store.index==st_no].iloc[0,:]
if test_t.shape[0] > 0:
train_t['Promo2'] = 0
train_t['NewPromoAge'] = 0
train_t['Competition'] = 0
test_t['Promo2'] = 0
test_t['NewPromoAge'] = 0
test_t['Competition'] = 0
if st_t['Promo2'] == 1:
train_t['Promo2'], train_t['NewPromoAge'] = zip(*train_t['Date'].map(calc_promo))
test_t['Promo2'], test_t['NewPromoAge'] = zip(*test_t['Date'].map(calc_promo))
if st_t['CompetitionOpen']:
train_t['Competition'] = train_t['Date'].map(calc_comp)
 test_t['Competition'] = test_t['Date'].map(calc_comp)
 train_t.drop('Date', axis=1, inplace=True)
 test_t.drop('Date', axis=1, inplace=True)
 X_train_t = train_t.drop('Sales',axis=1)
 y_train_t = train_t['Sales']
cv = GridSearchCV(clf,
param_grid=parameters,
cv=kfold,
scoring='neg_mean_squared_error')
 cv.fit(X_train_t, y_train_t)
 y_pred_t = cv.predict(test_t)
 y_pred_t[y_pred_t < 0] = 0
out_frame = pd.DataFrame([test_t.index, y_pred_t] ).T
out_frame.columns = ['Id','Sales']
out_frame['Id'] = out_frame['Id'].astype('int')
 submit_frame = submit_frame.append(out_frame)
 print(f'Predicted on : {st_no}, train_rows={train_t.shape[0]}, test_rows={len(y_pred_t)}')
 else:
print(f'Skipped: {st_no}, train_rows={train_t.shape[0]}' )
<train_model><EOS> 
submit_frame.sort_values('Id', inplace=True)
 print(submit_frame.shape)
 print(submit_frame.head())
 submit_frame.to_csv(r'submission.csv', index=False )