{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2022-07-11T15:49:36.073821Z","iopub.execute_input":"2022-07-11T15:49:36.074388Z","iopub.status.idle":"2022-07-11T15:49:36.080554Z","shell.execute_reply.started":"2022-07-11T15:49:36.074349Z","shell.execute_reply":"2022-07-11T15:49:36.078972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/store-sales-time-series-forecasting/train.csv')\ntest = pd.read_csv('/kaggle/input/store-sales-time-series-forecasting/test.csv')\nholidays = pd.read_csv('/kaggle/input/store-sales-time-series-forecasting/holidays_events.csv')\noil = pd.read_csv('/kaggle/input/store-sales-time-series-forecasting/oil.csv')\nstores = pd.read_csv('/kaggle/input/store-sales-time-series-forecasting/stores.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-11T15:49:36.111189Z","iopub.execute_input":"2022-07-11T15:49:36.111998Z","iopub.status.idle":"2022-07-11T15:49:38.051673Z","shell.execute_reply.started":"2022-07-11T15:49:36.111950Z","shell.execute_reply":"2022-07-11T15:49:38.050423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"oil.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T15:49:38.053693Z","iopub.execute_input":"2022-07-11T15:49:38.054046Z","iopub.status.idle":"2022-07-11T15:49:38.064532Z","shell.execute_reply.started":"2022-07-11T15:49:38.054016Z","shell.execute_reply":"2022-07-11T15:49:38.063564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(oil.head(2))\noil.tail(2)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T15:49:38.065814Z","iopub.execute_input":"2022-07-11T15:49:38.067201Z","iopub.status.idle":"2022-07-11T15:49:38.087376Z","shell.execute_reply.started":"2022-07-11T15:49:38.067158Z","shell.execute_reply":"2022-07-11T15:49:38.086197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"only oil dataframe has some null values, lets deal with them with interpolation\n\nbut the first row is also a nan so for this i will use bfill","metadata":{}},{"cell_type":"code","source":"oil = oil.interpolate()\noil = oil.fillna(method='bfill')","metadata":{"execution":{"iopub.status.busy":"2022-07-11T15:49:38.090818Z","iopub.execute_input":"2022-07-11T15:49:38.091259Z","iopub.status.idle":"2022-07-11T15:49:38.100778Z","shell.execute_reply.started":"2022-07-11T15:49:38.091226Z","shell.execute_reply":"2022-07-11T15:49:38.099954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"convert all date columns to datetime","metadata":{}},{"cell_type":"code","source":"train.date = pd.to_datetime(train.date)\ntest.date = pd.to_datetime(test.date)\nholidays.date = pd.to_datetime(holidays.date)\noil.date = pd.to_datetime(oil.date)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T15:49:38.102097Z","iopub.execute_input":"2022-07-11T15:49:38.102818Z","iopub.status.idle":"2022-07-11T15:49:38.582479Z","shell.execute_reply.started":"2022-07-11T15:49:38.102783Z","shell.execute_reply":"2022-07-11T15:49:38.581431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n\ncoming up with features","metadata":{}},{"cell_type":"code","source":"print(train.columns)\nprint(test.columns)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T15:49:38.583901Z","iopub.execute_input":"2022-07-11T15:49:38.584766Z","iopub.status.idle":"2022-07-11T15:49:38.591244Z","shell.execute_reply.started":"2022-07-11T15:49:38.584719Z","shell.execute_reply":"2022-07-11T15:49:38.589962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"besides what is already in our train and test dataframe, i will add some new features\n\n- more specific date information\n- oil price\n- store type\n- is it holiday or event\n- take into consideration that salaries are paid on 15th and last day of the month\n- 16 april 2016 earthquake","metadata":{}},{"cell_type":"code","source":"def CreateFeatures(df):\n    \n    df = df.copy()\n    df['dayofmonth'] = df.date.dt.day\n    df['month'] = df.date.dt.month\n    df['year'] = df.date.dt.year\n    df['quarter'] = df.date.dt.quarter\n    df['salary_recently'] = df['date'].apply(lambda x: 1 if x.day in [1, 2, 15, 16, 17] else 0)\n    df = pd.merge(df, oil, how='left', on='date')\n    df['store_type'] = df['store_nbr'].apply(lambda x: 1 if x in range(44, 53) else (2 if x in [9, 11, 18, 20, 21, 31, 34, 39] else (3 if x in [10, 19, 22, 30, 32, 33, 35, 40, 54] or x in range(12, 18) else (4 if x in range(1,9) or x in range(23, 28) or x in [37,38,41,42,53] else 5))))\n    df['holiday'] = df['date'].apply(lambda x: 1 if x in holidays.date and holidays['type'] in ['Holiday', 'Transfer', 'Additional'] and holidays['transferred'] != 'True' and holidays['locale'] == 'National' else 0)\n    df['event'] = df['date'].apply(lambda x: 1 if x in holidays.date and holidays['type'] in ['Event'] and holidays['locale'] == 'National' else 0)\n    \n    return df","metadata":{"execution":{"iopub.status.busy":"2022-07-11T15:49:38.592983Z","iopub.execute_input":"2022-07-11T15:49:38.593431Z","iopub.status.idle":"2022-07-11T15:49:38.608525Z","shell.execute_reply.started":"2022-07-11T15:49:38.593387Z","shell.execute_reply":"2022-07-11T15:49:38.607633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = CreateFeatures(train)\ntest = CreateFeatures(test)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T15:49:38.609960Z","iopub.execute_input":"2022-07-11T15:49:38.610620Z","iopub.status.idle":"2022-07-11T15:52:17.005004Z","shell.execute_reply.started":"2022-07-11T15:49:38.610587Z","shell.execute_reply":"2022-07-11T15:52:17.003830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n\ndata preprocessing and model building","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split\nimport xgboost as xgb","metadata":{"execution":{"iopub.status.busy":"2022-07-11T15:52:17.006524Z","iopub.execute_input":"2022-07-11T15:52:17.006903Z","iopub.status.idle":"2022-07-11T15:52:17.012645Z","shell.execute_reply.started":"2022-07-11T15:52:17.006859Z","shell.execute_reply":"2022-07-11T15:52:17.011337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"le = LabelEncoder()\ntrain['family'] = le.fit_transform(train['family'])\ntest['family'] = le.fit_transform(test['family'])","metadata":{"execution":{"iopub.status.busy":"2022-07-11T15:52:17.017157Z","iopub.execute_input":"2022-07-11T15:52:17.017933Z","iopub.status.idle":"2022-07-11T15:52:17.970480Z","shell.execute_reply.started":"2022-07-11T15:52:17.017864Z","shell.execute_reply":"2022-07-11T15:52:17.969250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features = ['family', 'onpromotion', 'dayofmonth', 'month', 'year', 'quarter', 'salary_recently', 'dcoilwtico', 'store_type', 'holiday', 'event']\ntarget = 'sales'\n\nX = train[features]\ny = train[target]\n\nX_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.25, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T15:52:17.971684Z","iopub.execute_input":"2022-07-11T15:52:17.972038Z","iopub.status.idle":"2022-07-11T15:52:19.270144Z","shell.execute_reply.started":"2022-07-11T15:52:17.972006Z","shell.execute_reply":"2022-07-11T15:52:19.269164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = xgb.XGBRegressor(base_score=0.5, booster='gbtree',\n                        n_estimators=500,\n                        early_stopping_rounds=50,\n                        objective='reg:linear',\n                        max_depth=3,\n                        learning_rate=0.01)\n\nmodel.fit(X_train,y_train,\n        eval_set=[(X_train, y_train), (X_valid, y_valid)],\n        verbose=100)  ","metadata":{"execution":{"iopub.status.busy":"2022-07-11T15:52:19.271738Z","iopub.execute_input":"2022-07-11T15:52:19.272424Z","iopub.status.idle":"2022-07-11T15:59:27.003657Z","shell.execute_reply.started":"2022-07-11T15:52:19.272378Z","shell.execute_reply":"2022-07-11T15:59:27.002097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = test[features]\ntest['preds'] = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T15:59:27.005294Z","iopub.execute_input":"2022-07-11T15:59:27.005631Z","iopub.status.idle":"2022-07-11T15:59:27.090650Z","shell.execute_reply.started":"2022-07-11T15:59:27.005600Z","shell.execute_reply":"2022-07-11T15:59:27.089471Z"},"trusted":true},"execution_count":null,"outputs":[]}]}