Kaggle competition: M5 Forecasting - Accuracy
<SOS> metric: M5_WRMSSE Kaggle data source: m5-forecasting-accuracy<define_variables>
warnings.filterwarnings('ignore')
features = ['count_dropoff_city', 'count_dropoff_county', 'count_dropoff_street',
'count_pickup_city', 'count_pickup_county', 'count_pickup_street',
'direction', 'displacement', 'dropoff_city',
'dropoff_county', 'dropoff_latitude', 'dropoff_longitude',
'dropoff_pca0', 'dropoff_pca1', 'end_street', 'f_avenue_count',
'f_boulevard_count', 'f_drive_count', 'f_expressway_count',
'f_left_count', 'f_parkway_count', 'f_plaza_count', 'f_road_count',
'f_square_count', 'f_street_count', 'in_congested_expressways',
'number_of_steps', 'passenger_count', 'pickup_city', 'pickup_county',
'pickup_day', 'pickup_dayofweek', 'pickup_dropoff_same_city',
'pickup_dropoff_same_county', 'pickup_latitude', 'pickup_longitude',
'pickup_pca0', 'pickup_pca1', 'pickup_time',
'starting_street', 'store_and_fwd_flag', 'total_distance',
'temperature', 'humidity', 'wind_speed', 'wind_direction',
'total_travel_time', 'vendor_id']<split>
path = ".. /input/m5-forecasting-accuracy"
calendar = pd.read_csv(os.path.join(path, "calendar.csv"))
selling_prices = pd.read_csv(os.path.join(path, "sell_prices.csv"))
sample_submission = pd.read_csv(os.path.join(path, "sample_submission.csv"))
params = {
'objective': 'regression',
'metric' : 'rmse',
'learning_rate': 0.1,
'max_depth': 25,
'num_leaves': 1000,
'feature_fraction': 0.9,
'bagging_fraction': 0.5,
'max_bin': 1000 ,
'num_threads' : -1}
target = np.log(train_df[['trip_duration']] + 1)
 folds = StratifiedKFold(n_splits = 5, shuffle = True, random_state = RANDOM_SEED)
 oof = np.zeros(train_df.shape[0])
 test_pred = np.zeros(test_df.shape[0])
 feature_importance_df = pd.DataFrame()
for _fold,(train_index, eval_index)in enumerate(folds.split(train_df, train_df['bin_target'])) :
print(f"
Fold: {_fold}
-------------------------")
 x_train = train_df.iloc[train_index][features].values ; y_train = target.iloc[train_index]
x_eval = train_df.iloc[eval_index][features].values ; y_eval = target.iloc[eval_index]
train_data = lgbm.Dataset(x_train, y_train)
 eval_data = lgbm.Dataset(x_eval, y_eval)
 model = lgbm.train(params, train_data, num_boost_round=1500, valid_sets=(train_data, eval_data),
early_stopping_rounds=100, verbose_eval=100)
 eval_pred = model.predict(train_df.iloc[eval_index][features], num_iteration = model.best_iteration)
 oof[eval_index] = eval_pred
test_pred +=(np.exp(model.predict(test_df[features], num_iteration = model.best_iteration)) - 1)/ folds.n_splits
fold_importance_df = pd.DataFrame()
fold_importance_df['Features'] = features
fold_importance_df['Importance'] = model.feature_importance()
feature_importance_df = pd.concat(( feature_importance_df, fold_importance_df), axis = 0 )<save_to_csv>
sales = pd.read_csv(os.path.join(path, "sales_train_validation.csv"))
submission = pd.read_csv('/kaggle/input/nyc-taxi-trip-duration/sample_submission.zip', compression='zip')
 submission['trip_duration'] = test_pred
submission[['id', 'trip_duration']].to_csv('./submission.csv', index = False )<import_modules>
def prep_calendar(df):
df = df.drop(["date", "weekday"], axis=1)
 df = df.assign(d = df.d.str[2:].astype(int))
df = df.fillna("missing")
 cols = list(set(df.columns)- {"wm_yr_wk", "d"})
 df[cols] = OrdinalEncoder(dtype="int" ).fit_transform(df[cols])
 df = reduce_mem_usage(df)
 return df
calendar = prep_calendar(calendar )
import pandas as pd
import numpy as np
from zipfile import ZipFile
import os
from datetime import datetime
import matplotlib.pyplot as plt
import seaborn as sns
from geopy import distance
import math
from sklearn.preprocessing import LabelEncoder, StandardScaler
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.optimizers import Adam, SGD
from keras.callbacks import ModelCheckpoint<define_variables>
def prep_selling_prices(df):
gr = df.groupby(["store_id", "item_id"])["sell_price"]
df["sell_price_rel_diff"] = gr.pct_change()
df["sell_price_roll_sd7"] = gr.transform(lambda x: x.rolling(7 ).std())
 df["sell_price_cumrel"] =(gr.shift(0)- gr.cummin())/(1 + gr.cummax() - gr.cummin())
 df = reduce_mem_usage(df)
 return df
selling_prices = prep_selling_prices(selling_prices )
base_path = ".. /input/nyc-taxi-trip-duration/"<load_from_csv>
sales.item_id.value_counts()
data_df = pd.read_csv(base_path+"train.csv" )<count_values>
def reshape_sales(df, drop_d = None):
if drop_d is not None:
df = df.drop(["d_" + str(i + 1)for i in range(drop_d)], axis=1)
 df = df.assign(id=df.id.str.replace("_validation", ""))
df = df.reindex(columns=df.columns.tolist() + ["d_" + str(1913 + i + 1)for i in range(2 * 28)])
 df = df.melt(id_vars=["id", "item_id", "dept_id", "cat_id", "store_id", "state_id"],
var_name='d', value_name='demand')
 df = df.assign(d=df.d.str[2:].astype("int16"))
return df
sales = reshape_sales(sales, 1000 )
data_df[(data_df['trip_duration']>500000)].count()<drop_column>
def prep_sales(df):
df['lag_t28'] = df.groupby(['id'])['demand'].transform(lambda x: x.shift(28))
df['rolling_mean_t7'] = df.groupby(['id'])['demand'].transform(lambda x: x.shift(28 ).rolling(7 ).mean())
 df['rolling_mean_t30'] = df.groupby(['id'])['demand'].transform(lambda x: x.shift(28 ).rolling(30 ).mean())
 df['rolling_mean_t60'] = df.groupby(['id'])['demand'].transform(lambda x: x.shift(28 ).rolling(60 ).mean())
 df['rolling_mean_t90'] = df.groupby(['id'])['demand'].transform(lambda x: x.shift(28 ).rolling(90 ).mean())
 df['rolling_mean_t180'] = df.groupby(['id'])['demand'].transform(lambda x: x.shift(28 ).rolling(180 ).mean())
 df['rolling_std_t7'] = df.groupby(['id'])['demand'].transform(lambda x: x.shift(28 ).rolling(7 ).std())
 df['rolling_std_t30'] = df.groupby(['id'])['demand'].transform(lambda x: x.shift(28 ).rolling(30 ).std())
 df = df[(df.d >= 1914)|(pd.notna(df.rolling_mean_t180)) ]
df = reduce_mem_usage(df)
 return df
sales = prep_sales(sales )
data_df.drop(data_df[data_df['trip_duration'] > 500000].index, inplace=True )<feature_engineering>
sales = sales.merge(calendar, how="left", on="d")
 gc.collect()
sales.head()
data_df['trip_duration'] = data_df['trip_duration'].apply(math.log )<filter>
sales = sales.merge(selling_prices, how="left", on=["wm_yr_wk", "store_id", "item_id"])
 sales.drop(["wm_yr_wk"], axis=1, inplace=True)
 gc.collect()
sales.head()
data_df = data_df[(data_df['trip_duration']<100000)]<drop_column>
del selling_prices
data_df.reset_index(drop = True, inplace=True )<data_type_conversions>
cat_id_cols = ["item_id", "dept_id", "store_id", "cat_id", "state_id"]
cat_cols = cat_id_cols + ["wday", "month", "year", "event_name_1",
"event_type_1", "event_name_2", "event_type_2"]
for i, v in tqdm(enumerate(cat_id_cols)) :
sales[v] = OrdinalEncoder(dtype="int" ).fit_transform(sales[[v]])
 sales = reduce_mem_usage(sales)
 sales.head()
gc.collect()
def strtodatetime(x):
return datetime.strptime(x, "%Y-%m-%d %H:%M:%S" )<feature_engineering>
test = sales[sales.d >= 1914]
test = test.assign(id=test.id + "_" + np.where(test.d <= 1941, "validation", "evaluation"),
F="F" +(test.d - 1913 - 28 *(test.d > 1941)).astype("str"))
test.head()
gc.collect()
data_df['pickup_datetime'] = data_df['pickup_datetime'].apply(strtodatetime )<feature_engineering>
def make_X(df):
X = {"dense1": df[dense_cols].to_numpy() }
for i, v in enumerate(cat_cols):
X[v] = df[[v]].to_numpy()
return X
X_test = make_X(test)
 flag =(sales.d < 1914)&(sales.d >= 1914 - 28)
 valid =(make_X(sales[flag]),
sales["demand"][flag])
 flag = sales.d < 1914 - 28
X_train = make_X(sales[flag])
 y_train = sales["demand"][flag]
del sales, flag
gc.collect()
def dayofweek(x):
return x.weekday()<feature_engineering>
import tensorflow as tf
import tensorflow.keras as keras
from tensorflow.keras import regularizers
from tensorflow.keras.layers import Dense, Input, Embedding, Dropout, concatenate, Flatten
from tensorflow.keras.models import Model
data_df['day_of_week'] = data_df['pickup_datetime'].apply(dayofweek )<train_model>
def create_model(lr=0.002):
tf.random.set_seed(173)
 tf.keras.backend.clear_session()
gc.collect()
dense_input = Input(shape=(len(dense_cols),), name='dense1')
 wday_input = Input(shape=(1,), name='wday')
 month_input = Input(shape=(1,), name='month')
 year_input = Input(shape=(1,), name='year')
 event_name_1_input = Input(shape=(1,), name='event_name_1')
 event_type_1_input = Input(shape=(1,), name='event_type_1')
 event_name_2_input = Input(shape=(1,), name='event_name_2')
 event_type_2_input = Input(shape=(1,), name='event_type_2')
 item_id_input = Input(shape=(1,), name='item_id')
 dept_id_input = Input(shape=(1,), name='dept_id')
 store_id_input = Input(shape=(1,), name='store_id')
 cat_id_input = Input(shape=(1,), name='cat_id')
 state_id_input = Input(shape=(1,), name='state_id')
 wday_emb = Flatten()(Embedding(7, 1 )(wday_input))
month_emb = Flatten()(Embedding(12, 1 )(month_input))
year_emb = Flatten()(Embedding(6, 1 )(year_input))
event_name_1_emb = Flatten()(Embedding(31, 1 )(event_name_1_input))
event_type_1_emb = Flatten()(Embedding(5, 1 )(event_type_1_input))
event_name_2_emb = Flatten()(Embedding(5, 1 )(event_name_2_input))
event_type_2_emb = Flatten()(Embedding(5, 1 )(event_type_2_input))
item_id_emb = Flatten()(Embedding(3049, 3 )(item_id_input))
dept_id_emb = Flatten()(Embedding(7, 1 )(dept_id_input))
store_id_emb = Flatten()(Embedding(10, 1 )(store_id_input))
cat_id_emb = Flatten()(Embedding(3, 1 )(cat_id_input))
state_id_emb = Flatten()(Embedding(3, 1 )(state_id_input))
x = concatenate([dense_input, wday_emb, month_emb, year_emb,
event_name_1_emb, event_type_1_emb,
event_name_2_emb, event_type_2_emb,
item_id_emb, dept_id_emb, store_id_emb,
cat_id_emb, state_id_emb])
 x = Dense(150, activation="tanh" )(x)
 x = Dense(75, activation="tanh" )(x)
 x = Dense(10, activation="tanh" )(x)
 outputs = Dense(1, activation="linear", name='output' )(x)
 inputs = {"dense1": dense_input, "wday": wday_input, "month": month_input, "year": year_input,
"event_name_1": event_name_1_input, "event_type_1": event_type_1_input,
"event_name_2": event_name_2_input, "event_type_2": event_type_2_input,
"item_id": item_id_input, "dept_id": dept_id_input, "store_id": store_id_input,
"cat_id": cat_id_input, "state_id": state_id_input}
model = Model(inputs, outputs)
 model.compile(loss=keras.losses.mean_squared_error,
metrics=["mse"],
optimizer=keras.optimizers.RMSprop(learning_rate=lr))
return model
distance.distance(( data_df['pickup_latitude'].iloc[0],
data_df['pickup_longitude'].iloc[0]), 
(data_df['dropoff_latitude'].iloc[0],
data_df['dropoff_longitude'].iloc[0])).m<compute_test_metric>
history = model.fit(X_train,
y_train,
batch_size=10000,
epochs=30,
shuffle=True,
validation_data=valid )
def dist(x):
return distance.distance(( x[0], x[1]), 
(x[2], x[3])).m<feature_engineering>
model.save('model.h5' )
data_df['dist'] = data_df[['pickup_latitude',
'pickup_longitude',
'dropoff_latitude',
'dropoff_longitude']].apply(lambda x:dist(x), axis=1 )<count_values>
pred = model.predict(X_test, batch_size=10000 )
data_df[(data_df['pickup_latitude']==data_df['dropoff_latitude'])& 
(data_df['pickup_longitude']==data_df['dropoff_longitude'])].count()<filter>
test["demand"] = pred.clip(0)
 submission = test.pivot(index="id", columns="F", values="demand" ).reset_index() [sample_submission.columns]
submission = sample_submission[["id"]].merge(submission, how="left", on="id")
 submission.head()
<categorify>
submission.to_csv("submission.csv", index=False )
data_df['dist'].replace(to_replace=0, value=1, inplace=True )<feature_engineering>
submission.to_csv("submission.csv", index=False )