Kaggle competition: Predict Future Sales
<SOS> metric: rmse Kaggle data source: predict-future-sales<merge>
!pip install -i https://test.pypi.org/simple/ litemort==0.1.7
X_train = X_train.join(y_train.set_index('series_id'), on='series_id')
 X_train = X_train.drop(['row_id', 'measurement_number', 'group_id'], axis=1)
 X_test = X_test.drop(['row_id', 'measurement_number'], axis=1 )<normalization>
isMORT = True
alg='MORT' if isMORT else 'XGB'
print(f"gradient boosting lib={alg}")
def quaternion_to_euler(x, y, z, w):
t0 = +2.0 *(w * x + y * z)
 t1 = +1.0 - 2.0 *(x * x + y * y)
 X = math.atan2(t0, t1)
 t2 = +2.0 *(w * y - z * x)
 t2 = +1.0 if t2 > +1.0 else t2
t2 = -1.0 if t2 < -1.0 else t2
Y = math.asin(t2)
 t3 = +2.0 *(w * z + x * y)
 t4 = +1.0 - 2.0 *(y * y + z * z)
 Z = math.atan2(t3, t4)
 return X, Y, Z<feature_engineering>
pd.set_option('display.max_rows', 500)
 pd.set_option('display.max_columns', 100)
 some_rows = None
data_root = '.. /input/'
test = pd.read_csv(f'{data_root}/competitive-data-science-predict-future-sales/test.csv' ).set_index('ID')
 data = pd.read_pickle(f'{data_root}/predict-future-sales/data.pkl')
 if some_rows is not None:
nMost=data.shape[0]
random.seed(42)
 subset = random.sample(range(nMost), some_rows)
 data = data.iloc[subset, :].reset_index(drop=True)
 print('====== Some Samples...data={}'.format(data.shape))
data = data[[
'date_block_num',
'shop_id',
'item_id',
'item_cnt_month',
'city_code',
'item_category_id',
'type_code',
'subtype_code',
'item_cnt_month_lag_1',
'item_cnt_month_lag_2',
'item_cnt_month_lag_3',
'item_cnt_month_lag_6',
'item_cnt_month_lag_12',
'date_avg_item_cnt_lag_1',
'date_item_avg_item_cnt_lag_1',
'date_item_avg_item_cnt_lag_2',
'date_item_avg_item_cnt_lag_3',
'date_item_avg_item_cnt_lag_6',
'date_item_avg_item_cnt_lag_12',
'date_shop_avg_item_cnt_lag_1',
'date_shop_avg_item_cnt_lag_2',
'date_shop_avg_item_cnt_lag_3',
'date_shop_avg_item_cnt_lag_6',
'date_shop_avg_item_cnt_lag_12',
'date_cat_avg_item_cnt_lag_1',
'date_shop_cat_avg_item_cnt_lag_1',
'date_city_avg_item_cnt_lag_1',
'date_item_city_avg_item_cnt_lag_1',
'delta_price_lag',
'month',
'days',
'item_shop_last_sale',
'item_last_sale',
'item_shop_first_sale',
'item_first_sale',
]]
X_train = data[data.date_block_num < 33].drop(['item_cnt_month'], axis=1)
 Y_train = data[data.date_block_num < 33]['item_cnt_month']
X_valid = data[data.date_block_num == 33].drop(['item_cnt_month'], axis=1)
 Y_valid = data[data.date_block_num == 33]['item_cnt_month']
X_test = data[data.date_block_num == 34].drop(['item_cnt_month'], axis=1)
 print(f"X_train={X_train.shape} Y_train={Y_train.shape}")
 print(f"X_valid={X_valid.shape} Y_valid={Y_valid.shape}")
 print(f"X_test={X_test.shape} ")
 del data
gc.collect() ;
def fe_step0(actual):
actual['norm_quat'] =(actual['orientation_X']**2 + actual['orientation_Y']**2 + actual['orientation_Z']**2 + actual['orientation_W']**2)
 actual['mod_quat'] =(actual['norm_quat'])**0.5
actual['norm_X'] = actual['orientation_X'] / actual['mod_quat']
actual['norm_Y'] = actual['orientation_Y'] / actual['mod_quat']
actual['norm_Z'] = actual['orientation_Z'] / actual['mod_quat']
actual['norm_W'] = actual['orientation_W'] / actual['mod_quat']
return actual<concatenate>
params={'num_leaves': 550, 'n_estimators':1000,'early_stopping_rounds':20,
'feature_fraction': 1, 'bagging_fraction': 1,
'max_bin': 512,
'max_depth': 10,
'min_child_weight': 300,
'learning_rate': 0.1,
'objective': 'regression',
'boosting_type': 'gbdt',
'verbose': 1,
'metric': {'rmse'}
}
if isMORT:
print(f"Call LiteMORT...")
 t0=time.time()
model = LiteMORT(params ).fit(X_train,Y_train,eval_set=[(X_valid, Y_valid)])
 print(f"LiteMORT...... OK time={time.time() -t0:.4g} model={model}")
 else:
model = XGBRegressor(
 max_depth=8,
n_estimators=1000,
min_child_weight=300,
colsample_bytree=0.8,
subsample=0.8,
eta=0.3,
seed=42)
 model.fit(
 X_train,
Y_train,
eval_metric="rmse",
eval_set=[(X_train, Y_train),(X_valid, Y_valid)],
verbose=True,
early_stopping_rounds = 10)
 alg = 'xgboost'
Y_pred = model.predict(X_valid ).clip(0, 20)
 score = np.sqrt(mean_squared_error(Y_pred, Y_valid))
Y_test = model.predict(X_test ).clip(0, 20)
 print(f"score={score}")
 def plot_features(booster, figsize):
fig, ax = plt.subplots(1,1,figsize=figsize)
 return plot_importance(booster=booster, ax=ax)
 if not isMORT:
plot_features(model,(10, 14))
<drop_column><EOS> 
submission = pd.DataFrame({
"ID": test.index,
"item_cnt_month": Y_test
})
 path = f'submission.csv'
submission.to_csv(path, index=False)
 print(f"...... Save submit @{path}...... ")