Kaggle competition: M5 Forecasting - Accuracy
lightgbm = LGBMRegressor(objective='regression',
num_leaves=4,
learning_rate=0.01,
n_estimators=20000,
max_bin=2000,
bagging_fraction=0.75,
bagging_freq=5,
bagging_seed=7,
feature_fraction=0.2,
feature_fraction_seed=7,
verbose=-1, 
)
 gbm = lightgbm.fit(X_training, y_training )<compute_test_metric>
pd.options.display.max_columns = 50
gbm_pred = gbm.predict(X_valid)
 r2_gbm = r2_score(y_valid, gbm_pred)
 rmse_gbm = np.sqrt(mean_squared_error(y_valid, gbm_pred))
print("R^2 Score: " + str(r2_gbm))
print("RMSE Score: " + str(rmse_gbm))<create_dataframe>
CAL_DTYPES = { "event_name_1": "category", "event_type_1": "category",
"weekday": "category", 'wm_yr_wk': 'int16', "wday": "int8",
"month": "int8", "year": "int16", "snap_CA": "float32", 'snap_TX': 'float32', 'snap_WI': 'float32' }
STE_DTYPES = { "item_id": "category", "dept_id": "category", "cat_id": "category", "store_id": "category",
"state_id": "category" }
PRICE_DTYPES = { "store_id": "category", "item_id": "category", "wm_yr_wk": "int16","sell_price":"float32" }
model_performances = pd.DataFrame({
"Model" : ["Linear Regression", "Ridge", "Decision Tree Regressor", "Random Forest Regressor","Gradient Boosting Regression","XGBoost","LGBM Regressor"],
"R Squared" : [str(r2_lin)[0:5], str(r2_ridge)[0:5], str(r2_dtr)[0:5], str(r2_rf)[0:5] , str(r2_clf)[0:5], str(r2_xgb)[0:5], str(r2_gbm)[0:5]],
"RMSE" : [str(rmse_lin)[0:8], str(rmse_ridge)[0:8], str(rmse_dtr)[0:8], str(rmse_rf)[0:8], str(rmse_clf)[0:8], str(rmse_xgb)[0:8], str(rmse_gbm)[0:8]]
})
 model_performances.round(4)
 print("Sorted by R Squared:")
 model_performances.sort_values(by="R Squared", ascending=False )<sort_values>
cal = pd.read_csv(".. /input/m5-forecasting-accuracy/calendar.csv", dtype = CAL_DTYPES)
 ste = pd.read_csv(".. /input/m5-forecasting-accuracy/sales_train_evaluation.csv", dtype = STE_DTYPES)
 pri = pd.read_csv(".. /input/m5-forecasting-accuracy/sell_prices.csv", dtype = PRICE_DTYPES )
print("Sorted by RMSE:")
 model_performances.sort_values(by="RMSE", ascending=True )<predict_on_test>

 print("canceled" )
def blend_models_predict(X):
return(( 0.05 * lm.predict(X)) + \ 
(0.05 * linreg.predict(X)) + \ 
(0.05 * ridge.predict(X)) + \ 
(0.1 * clf.predict(X)) + \ 
(0.2 * gbm.predict(X)) + \ 
(0.15 * rfr.predict(X)) + \ 
(0.4 * xgb.predict(X)))
<predict_on_test>
trans_cols = ["item_id", "dept_id", "cat_id", "store_id", "state_id"]
for col in trans_cols:
ste[col] = ste[col].cat.codes.astype("int16")
 trans_cols = ["item_id", "store_id"]
for col in trans_cols:
pri[col] = pri[col].cat.codes.astype("int16")
 trans_cols = ["event_name_1", "event_type_1"]
for col in trans_cols:
cal[col] = cal[col].cat.codes.astype("int8")+ 1
d_order = { 'Chanukah End': 0, 'Cinco De Mayo': 2, 'Easter': 4, "Father's day": 7, 'OrthodoxEaster': 20, "Purim End": 23 }
cal["event_name_2"] = cal["event_name_2"].map(d_order ).fillna(-1 ).astype("int8")+ 1
d_order = { 'Cultural': 0, 'Religious': 2 }
cal["event_type_2"] = cal["event_type_2"].map(d_order ).fillna(-1 ).astype("int8")+ 1
cal["date"] = pd.to_datetime(cal["date"] )
submission_predictions = np.exp(blend_models_predict(df_test_add))
print(submission_predictions )<save_to_csv>
cal.drop(['weekday'], axis=1, inplace=True )
res=pd.DataFrame(columns = ['Id', 'SalePrice'])
 res['Id'] = df_test.index + 1461
res['SalePrice'] = submission_predictions
res.to_csv('submission1.csv',index=False )<load_from_csv>
df = pd.melt(ste,id_vars=ste.columns.values[:6],var_name="d",value_name="sells")
 df["sells"] = df["sells"].astype("float32")
 df = df.merge(cal, on='d', copy = False)
 df = df.merge(pri, on=["store_id", "item_id", "wm_yr_wk"],copy = False)
train = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv')
 test = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv')
 train.head()
print(train.shape)
 print(test.shape )<sort_values>
df.drop(df.index[df["wm_yr_wk"]<=11430], inplace=True)
 days_christmas = ["d_331","d_697","d_1062","d_1427","d_1792"]
for day in days_christmas:
df.drop(df.index[df["d"] == day], inplace=True )
train.isnull().sum().sort_values(ascending=False)[0:20]<data_type_conversions>
for i in list(train.columns):
dtype = train[i].dtype
values = 0
if(dtype == float or dtype == int):
method = 'mean'
else:
method = 'mode'
if(train[i].notnull().sum() / 1460 <=.5):
train.drop(i, axis = 1, inplace=True)
 elif method == 'mean':
train[i]=train[i].fillna(train[i].mean())
 else:
train[i]=train[i].fillna(train[i].mode() [0])
 print(train[i])
 for i in list(test.columns):
dtype = test[i].dtype
values = 0
if(dtype == float or dtype == int):
method = 'mean'
else:
method = 'mode'
if(test[i].notnull().sum() / 1460 <=.5):
test.drop(i, axis = 1, inplace=True)
 elif method == 'mean':
test[i]=test[i].fillna(test[i].mean())
 else:
test[i]=test[i].fillna(test[i].mode() [0])
 train.head()
test.shape<count_missing_values>
def create_features(dt):
dt["lag_7"] = dt[["id","sells"]].groupby("id")["sells"].shift(7)
 dt["lag_28"] = dt[["id","sells"]].groupby("id")["sells"].shift(28)
 dt["win_7"] = dt[["id","sells"]].groupby("id")["sells"].transform(lambda x : x.rolling(7 ).mean() ).shift(1)
 dt["win_28"] = dt[["id","sells"]].groupby("id")["sells"].transform(lambda x : x.rolling(28 ).mean() ).shift(1)
 dt["win_7_lag_7"] = dt[["id","win_7"]].groupby("id")["win_7"].shift(7)
 dt["win_28_lag_28"] = dt[["id","win_28"]].groupby("id")["win_28"].shift(28)
 dt["price_lag_1"] = dt[["id","sell_price"]].groupby("id")["sell_price"].shift(1)
 dt["price_lag_7"] = dt[["id","sell_price"]].groupby("id")["sell_price"].shift(7)
 dt["price_win_7"] = dt[["id","sell_price"]].groupby("id")["sell_price"].transform(lambda x : x.rolling(7 ).mean() ).shift(1)
 dt["price_win_28"] = dt[["id","sell_price"]].groupby("id")["sell_price"].transform(lambda x : x.rolling(28 ).mean() ).shift(1 )
train.isnull().any().any()
train.head() 
<define_variables>
create_features(df)
 df.dropna(inplace = True )
columns = ['MSZoning', 'Street',
'LotShape', 'LandContour', 'Utilities', 'LotConfig', 'LandSlope',
'Neighborhood', 'Condition1', 'Condition2', 'BldgType', 'HouseStyle', 'RoofStyle',
'RoofMatl', 'Exterior1st', 'Exterior2nd', 'MasVnrType',
'ExterQual', 'ExterCond', 'Foundation', 'BsmtQual', 'BsmtCond',
'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2',
'Heating', 'HeatingQC', 'CentralAir', 'Electrical',
'KitchenQual', 'Functional', 'FireplaceQu', 'GarageType', 'GarageFinish',
'GarageQual', 'GarageCond', 'PavedDrive', 'SaleType', 'SaleCondition']<concatenate>
features =['item_id', 'dept_id', 'store_id', 'cat_id', 'state_id',
'wday', 'month', 'year',
'event_name_1', 'event_name_2', 'event_type_1', 'event_type_2',
'snap_CA', 'snap_TX', 'snap_WI',
'sell_price',
'lag_7', 'lag_28', 'win_7', 'win_28', 'win_7_lag_7','win_28_lag_28',
'price_win_7', 'price_win_28']
X_train = df[features]
y_train = df['sells']
cat_features = ['item_id', 'dept_id','store_id', 'cat_id', 'state_id',
'event_name_1', 'event_name_2', 'event_type_1', 'event_type_2']
train_test_data = pd.concat([train, test], axis = 0)
 print(test.shape)
 print(train.shape)
 train_test_data.head()
train_test_data.shape<categorify>
np.random.seed(1000)
 fake_valid_index = np.random.choice(X_train.index.values, 2000000, replace = False)
 train_index = np.setdiff1d(X_train.index.values, fake_valid_index)
 train_data = lgb.Dataset(X_train.loc[train_index] , label = y_train.loc[train_index],
categorical_feature=cat_features, free_raw_data=False)
 fake_valid_data = lgb.Dataset(X_train.loc[fake_valid_index], label = y_train.loc[fake_valid_index],
categorical_feature=cat_features, free_raw_data=False )
def One_hot_encoding(columns):
df_final=train_test_data
i=0
for fields in columns:
df1=pd.get_dummies(train_test_data[fields],drop_first=True)
 train_test_data.drop([fields],axis=1,inplace=True)
 if i==0:
df_final=df1.copy()
else:
df_final=pd.concat([df_final,df1],axis=1)
 i=i+1
df_final=pd.concat([train_test_data,df_final],axis=1)
 return df_final<categorify>
del df, X_train, y_train, fake_valid_index, train_index
gc.collect()
train_test_data = One_hot_encoding(columns)
 print(train_test_data.shape)
 train_test_data.head()<count_duplicates>
params = {
"objective" : "tweedie",
"metric" : ["rmse"],
"force_row_wise" : True,
"learning_rate" : 0.07,
"bagging_freq" : 3,
"bagging_fraction" : 0.5,
"lambda_l2" : 0.1,
"num_iterations" : 1000,
"num_leaves" : 255,
"min_data_in_leaf": 128,
}
train_test_data.columns.duplicated() 
<count_duplicates>
%%time
m_lgb = lgb.train(params, train_data, valid_sets = [fake_valid_data], verbose_eval=50 )
train_test_data =train_test_data.loc[:,~train_test_data.columns.duplicated() ]
train_test_data.shape<import_modules>
m_lgb.save_model("model.lgb" )
from scipy.stats import norm, skew
from scipy import stats<feature_engineering>
days = [f"d_{i}" for i in range(1942,1970)]
for day in days:
ste[day] = 0
train["SalePrice"] = np.log(train["SalePrice"])
<prepare_x_and_y>
df = pd.melt(ste,id_vars=ste.columns.values[:6],var_name="d",value_name="sells")
 df["sells"] = df["sells"].astype("float32")
 df = df.merge(cal, on='d', copy = False)
 df = df.merge(pri, on=["store_id", "item_id", "wm_yr_wk"],copy = False )
df_Test.drop(['SalePrice'],axis=1,inplace=True)
 X_train_final=df_Train.drop(['SalePrice'],axis=1)
 y_train_final=df_Train['SalePrice']
X_train_final.shape<create_dataframe>
df.drop(df.index[df["wm_yr_wk"]<=11607], inplace=True )
X_std = StandardScaler().fit_transform(X_train_final)
 my_columns = X_train_final.columns
new_df = pd.DataFrame(X_std, columns=my_columns )<normalization>
create_features(df )
pca = PCA(n_components = 2)
 df_pca = pca.fit_transform(new_df)
 print(df_pca )<train_model>
%%time
for day in days:
X_pred = df[df["d"] == day][features]
y_pred = m_lgb.predict(X_pred)
 print(day)
 df.loc[df["d"] == day, "sells"] = y_pred
create_features(df )
X_train, X_test, y_train, y_test = train_test_split(X_train_final, y_train_final)
 linreg = LinearRegression()
linreg.fit(X_train, y_train)
 print("R-Squared Value for Training Set: ",linreg.score(X_train, y_train))
print("R-Squared Value for Test Set: ",linreg.score(X_test, y_test))<train_model>
sub = df[["id","d","sells"]].pivot(index="id", columns="d", values="sells")
 sub = sub.reset_index()
sub.columns.name = None
knnreg = KNeighborsRegressor(n_neighbors = 2)
 knnreg.fit(X_train, y_train)
 print('R-squared train score:',knnreg.score(X_train, y_train))
print('R-squared test score: ',knnreg.score(X_test, y_test))<train_model>
sub1 = pd.concat([sub.T[0:1],sub.T[-56:-28]] ).T
sub2 = pd.concat([sub.T[0:1],sub.T[-28:]] ).T
ridge = Ridge()
ridge.fit(X_train, y_train)
 print('R-squared train score:',ridge.score(X_train, y_train))
print('R-squared test score: ',ridge.score(X_test, y_test))<train_model>
sub_columns = ["id"] + [f"F{i}" for i in range(1,29)]
sub1.columns = sub_columns
sub2.columns = sub_columns
sub1["id"] = sub1["id"].str.replace("evaluation", "validation" )
<train_model><EOS> 
sub = pd.concat([sub2,sub1])
 sub.to_csv("submission.csv",index=False)
 sub