Kaggle competition: M5 Forecasting - Accuracy
<SOS> metric: M5_WRMSSE Kaggle data source: m5-forecasting-accuracy<save_to_csv>
import gc
import numpy as np
import pandas as pd
import lightgbm as lgb
from datetime import datetime, timedelta
test_df_output.to_csv('submission.csv', index=False )<set_options>
calendarDTypes = {"event_name_1": "category",
"event_name_2": "category",
"event_type_1": "category",
"event_type_2": "category",
"weekday": "category",
'wm_yr_wk': 'int16',
"wday": "int16",
"month": "int16",
"year": "int16",
"snap_CA": "float32",
'snap_TX': 'float32',
'snap_WI': 'float32' }
calendar = pd.read_csv(".. /input/m5-forecasting-accuracy/calendar.csv",
dtype = calendarDTypes)
 calendar["date"] = pd.to_datetime(calendar["date"])
 for col, colDType in calendarDTypes.items() :
if colDType == "category":
calendar[col] = calendar[col].cat.codes.astype("int16")
 calendar[col] -= calendar[col].min()
calendar.head()
warnings.filterwarnings("ignore")
 tqdm.pandas()
CountVectorizer,\
HashingVectorizer
stopword=set(STOPWORDS)
 lem = WordNetLemmatizer()
tokenizer=TweetTokenizer()
np.random.seed(0)
<choose_model_class>
priceDTypes = {"store_id": "category",
"item_id": "category",
"wm_yr_wk": "int16",
"sell_price":"float32"}
prices = pd.read_csv(".. /input/m5-forecasting-accuracy/sell_prices.csv",
dtype = priceDTypes)
 for col, colDType in priceDTypes.items() :
if colDType == "category":
prices[col] = prices[col].cat.codes.astype("int16")
 prices[col] -= prices[col].min()
prices.head()
AUTO = tf.data.experimental.AUTOTUNE
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 print(strategy.num_replicas_in_sync)
 BATCH_SIZE = 16 * strategy.num_replicas_in_sync<string_transform>
firstDay = 250
lastDay = 1913
numCols = [f"d_{day}" for day in range(firstDay, lastDay+1)]
catCols = ['id', 'item_id', 'dept_id','store_id', 'cat_id', 'state_id']
dtype = {numCol: "float32" for numCol in numCols}
dtype.update({catCol: "category" for catCol in catCols if catCol != "id"})
 ds = pd.read_csv(".. /input/m5-forecasting-accuracy/sales_train_validation.csv",
usecols = catCols + numCols, dtype = dtype)
 for col in catCols:
if col != "id":
ds[col] = ds[col].cat.codes.astype("int16")
 ds[col] -= ds[col].min()
ds = pd.melt(ds,
id_vars = catCols,
value_vars = [col for col in ds.columns if col.startswith("d_")],
var_name = "d",
value_name = "sales")
 ds = ds.merge(calendar, on = "d", copy = False)
 ds = ds.merge(prices, on = ["store_id", "item_id", "wm_yr_wk"], copy = False)
 ds.head()
MODEL = 'jplu/tf-xlm-roberta-base'
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
 text = "[CLS] Who was Jim Henson ? [SEP] Jim Henson was a puppeteer [SEP]"
tokenized_text = tokenizer.tokenize(text )<define_variables>
dateFeatures = {"wday": "weekday",
"week": "weekofyear",
"month": "month",
"quarter": "quarter",
"year": "year",
"mday": "day"}
for featName, featFunc in dateFeatures.items() :
if featName in ds.columns:
ds[featName] = ds[featName].astype("int16")
 else:
ds[featName] = getattr(ds["date"].dt, featFunc ).astype("int16" )
masked_index = 8
tokenized_text[masked_index] = '[MASK]'
assert tokenized_text == ['[CLS]', 'who', 'was', 'jim', 'henson', '?', '[SEP]', 'jim', '[MASK]', 'was', 'a', 'puppet', '
indexed_tokens = tokenizer.convert_tokens_to_ids(tokenized_text)
 segments_ids = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1]
tokens_tensor = torch.tensor([indexed_tokens])
 segments_tensors = torch.tensor([segments_ids] )<predict_on_test>
ds.dropna(inplace = True)
 unusedCols = ["id", "date", "sales","d", "wm_yr_wk", "weekday"]
trainCols = ds.columns[~ds.columns.isin(unusedCols)]
X_train = ds[trainCols]
y_train = ds["sales"]
model = BertForMaskedLM.from_pretrained('bert-base-uncased')
 model.eval()
device = 'cuda' if torch.cuda.is_available() else 'cpu'
print(device)
 tokens_tensor = tokens_tensor.to(device)
 segments_tensors = segments_tensors.to(device)
 model.to(device)
 with torch.no_grad() :
outputs = model(tokens_tensor, token_type_ids=segments_tensors)
 predictions = outputs[0]
predicted_index = torch.argmax(predictions[0, masked_index] ).item()
predicted_token = tokenizer.convert_ids_to_tokens([predicted_index])[0]
assert predicted_token == 'henson'
print('Predicted token is:',predicted_token )<import_modules>
np.random.seed(777)
 catFeats = ['item_id', 'dept_id','store_id', 'cat_id', 'state_id'] + \
["event_name_1", "event_name_2", "event_type_1", "event_type_2"]
validInds = np.random.choice(X_train.index.values, 2_000_000, replace = False)
 trainInds = np.setdiff1d(X_train.index.values, validInds)
 trainData = lgb.Dataset(X_train.loc[trainInds], label = y_train.loc[trainInds],
categorical_feature = catFeats, free_raw_data = False)
 validData = lgb.Dataset(X_train.loc[validInds], label = y_train.loc[validInds],
categorical_feature = catFeats, free_raw_data = False )
import pandas as pd<import_modules>
del ds, X_train, y_train, validInds, trainInds ; gc.collect()
import pandas as pd<load_from_csv>
params = {
"objective" : "poisson",
"metric" :"rmse",
"force_row_wise" : True,
"learning_rate" : 0.075,
"sub_row" : 0.75,
"bagging_freq" : 1,
"lambda_l2" : 0.1,
"metric": ["rmse"],
'verbosity': 1,
'num_iterations' : 1200,
'num_leaves': 128,
"min_data_in_leaf": 100,
}
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)
 train3 = pd.read_csv('.. /input/jigsaw-train-multilingual-coments-google-api/jigsaw-toxic-comment-train-google-es-cleaned.csv')
 train4 = pd.read_csv('.. /input/jigsaw-train-multilingual-coments-google-api/jigsaw-toxic-comment-train-google-es-cleaned.csv')
 valid = pd.read_csv('/kaggle/input/jigsaw-multilingual-toxic-comment-classification/validation.csv')
 test = pd.read_csv('/kaggle/input/jigsaw-multilingual-toxic-comment-classification/test.csv')
 sub = pd.read_csv('/kaggle/input/jigsaw-multilingual-toxic-comment-classification/sample_submission.csv')
 toxic = len(train2[['comment_text', 'toxic']].query('toxic==1'))
train = pd.concat([
train1[['comment_text', 'toxic']],
train2[['comment_text', 'toxic']].query('toxic==1'),
train3[['comment_text', 'toxic']].query('toxic==0'),
train3[['comment_text', 'toxic']].query('toxic==1'),
train4[['comment_text', 'toxic']].query('toxic==0'),
train4[['comment_text', 'toxic']].query('toxic==1'),
train2[['comment_text', 'toxic']].query('toxic==0' ).sample(n=(toxic+(toxic//3)) , random_state=101)
 ])
 test_data = test
train_data = train
maxlen = 192<load_from_csv>
m_lgb = lgb.train(params, trainData, valid_sets = [validData], verbose_eval = 20 )
df_valid_en = pd.read_csv('.. /input/val-en-df/validation_en.csv')
 df_valid_en = df_valid_en.drop(columns=['id', 'comment_text','lang'])
 df_valid_en = df_valid_en.rename(columns={"comment_text_en": "comment_text"})
 columns_titles = ["comment_text","toxic"]
df_valid_en=df_valid_en.reindex(columns=columns_titles)

print(df_valid_en.head(3))
train_data = pd.concat([train_data , df_valid_en], axis=0 ).reset_index(drop=True)
 train_data = train_data.sample(frac=1 ).reset_index(drop=True)
 print(len(train2[['comment_text', 'toxic']].query('toxic==1')))
 print(len(train_data))
train_data.head() 
<feature_engineering>
m_lgb.save_model("model.lgb" )
val = valid
train = train_data
def clean(text):
text = text.fillna("fillna" ).str.lower()
text = text.map(lambda x: re.sub('\
',' ',str(x)))
 text = text.map(lambda x: re.sub("\[\[User.*",'',str(x)))
 text = text.map(lambda x: re.sub("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}",'',str(x)))
 text = text.map(lambda x: re.sub("\(http://.*?\s\(http://.*\)",'',str(x)))
 return text
val["comment_text"] = clean(val["comment_text"])
 test_data["content"] = clean(test_data["content"])
 train["comment_text"] = clean(train["comment_text"] )<define_variables>
trLast = 1913
maxLags = 57
def create_ds() :
startDay = trLast - maxLags
numCols = [f"d_{day}" for day in range(startDay, trLast + 1)]
catCols = ['id', 'item_id', 'dept_id','store_id', 'cat_id', 'state_id']
dtype = {numCol:"float32" for numCol in numCols}
dtype.update({catCol: "category" for catCol in catCols if catCol != "id"})
 ds = pd.read_csv(".. /input/m5-forecasting-accuracy/sales_train_validation.csv",
usecols = catCols + numCols, dtype = dtype)
 for col in catCols:
if col != "id":
ds[col] = ds[col].cat.codes.astype("int16")
 ds[col] -= ds[col].min()
for day in range(trLast + 1, trLast+ 28 +1):
ds[f"d_{day}"] = np.nan
ds = pd.melt(ds,
id_vars = catCols,
value_vars = [col for col in ds.columns if col.startswith("d_")],
var_name = "d",
value_name = "sales")
 ds = ds.merge(calendar, on = "d", copy = False)
 ds = ds.merge(prices, on = ["store_id", "item_id", "wm_yr_wk"], copy = False)
 return ds
def create_features(ds):
dayLags = [7, 28]
lagSalesCols = [f"lag_{dayLag}" for dayLag in dayLags]
for dayLag, lagSalesCol in zip(dayLags, lagSalesCols):
ds[lagSalesCol] = ds[["id","sales"]].groupby("id")["sales"].shift(dayLag)
 windows = [7, 28]
for window in windows:
for dayLag, lagSalesCol in zip(dayLags, lagSalesCols):
ds[f"rmean_{dayLag}_{window}"] = ds[["id", lagSalesCol]].groupby("id")[lagSalesCol].transform(lambda x: x.rolling(window ).mean())
 dateFeatures = {"wday": "weekday",
"week": "weekofyear",
"month": "month",
"quarter": "quarter",
"year": "year",
"mday": "day"}
for featName, featFunc in dateFeatures.items() :
if featName in ds.columns:
ds[featName] = ds[featName].astype("int16")
 else:
ds[featName] = getattr(ds["date"].dt, featFunc ).astype("int16" )
<string_transform><EOS> 
fday = datetime(2016,4, 25)
 alphas = [1.028, 1.023, 1.018]
weights = [1/len(alphas)] * len(alphas)
 sub = 0.
 for icount,(alpha, weight)in enumerate(zip(alphas, weights)) :
te = create_ds()
cols = [f"F{i}" for i in range(1,29)]
for tdelta in range(0, 28):
day = fday + timedelta(days=tdelta)
 print(tdelta, day)
 tst = te[(te['date'] >= day - timedelta(days=maxLags)) &(te['date'] <= day)].copy()
create_features(tst)
 tst = tst.loc[tst['date'] == day , trainCols]
te.loc[te['date'] == day, "sales"] = alpha * m_lgb.predict(tst)
 te_sub = te.loc[te['date'] >= fday, ["id", "sales"]].copy()
te_sub["F"] = [f"F{rank}" for rank in te_sub.groupby("id")["id"].cumcount() +1]
te_sub = te_sub.set_index(["id", "F" ] ).unstack() ["sales"][cols].reset_index()
te_sub.fillna(0., inplace = True)
 te_sub.sort_values("id", inplace = True)
 te_sub.reset_index(drop=True, inplace = True)
 te_sub.to_csv(f"submission_{icount}.csv",index=False)
 if icount == 0 :
sub = te_sub
sub[cols] *= weight
else:
sub[cols] += te_sub[cols]*weight
print(icount, alpha, weight)
 sub2 = sub.copy()
sub2["id"] = sub2["id"].str.replace("validation$", "evaluation")
 sub = pd.concat([sub, sub2], axis=0, sort=False)
 sub.to_csv("submission.csv",index=False )