{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":7391,"databundleVersionId":44328,"sourceType":"competition"}],"dockerImageVersionId":30822,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T13:47:30.393337Z","iopub.execute_input":"2025-01-15T13:47:30.393894Z","iopub.status.idle":"2025-01-15T13:47:32.747286Z","shell.execute_reply.started":"2025-01-15T13:47:30.393786Z","shell.execute_reply":"2025-01-15T13:47:32.746084Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\n\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\npaths = []\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n        paths.append(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-01-15T13:47:46.534806Z","iopub.execute_input":"2025-01-15T13:47:46.535135Z","iopub.status.idle":"2025-01-15T13:47:46.545229Z","shell.execute_reply.started":"2025-01-15T13:47:46.535110Z","shell.execute_reply":"2025-01-15T13:47:46.544126Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install py7zr","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T13:47:50.810495Z","iopub.execute_input":"2025-01-15T13:47:50.810835Z","iopub.status.idle":"2025-01-15T13:47:58.794900Z","shell.execute_reply.started":"2025-01-15T13:47:50.810808Z","shell.execute_reply":"2025-01-15T13:47:58.793626Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import py7zr\nfor filenames in paths:\n    with py7zr.SevenZipFile(filenames, mode='r') as z_ref:\n        z_ref.extractall(path='/kaggle/working')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T13:48:08.214239Z","iopub.execute_input":"2025-01-15T13:48:08.214655Z","iopub.status.idle":"2025-01-15T13:49:39.221915Z","shell.execute_reply.started":"2025-01-15T13:48:08.214620Z","shell.execute_reply":"2025-01-15T13:49:39.220929Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Corporación Favorita Grocery Sales Forecasting\n\nPlease read data description: https://www.kaggle.com/c/favorita-grocery-sales-forecasting","metadata":{}},{"cell_type":"markdown","source":"## Splitting Data into Training and Test Sets\nThe data you are working with splited into **training** and **test** sets and stored in the following files:\n\n- **`/kaggle/working/train_data.csv`** – the training dataset used for model training.\n- **`/kaggle/working/test_data.csv`** – the test dataset used to evaluate the model's performance.","metadata":{}},{"cell_type":"code","source":"%%time\ndata = pd.read_csv(\"/kaggle/working/train.csv\")\ndata[\"date\"] =  pd.to_datetime(data[\"date\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T13:49:39.223042Z","iopub.execute_input":"2025-01-15T13:49:39.223295Z","iopub.status.idle":"2025-01-15T13:51:27.460445Z","shell.execute_reply.started":"2025-01-15T13:49:39.223274Z","shell.execute_reply":"2025-01-15T13:51:27.459158Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"monthly_counts_data = data['date'].dt.to_period('M').value_counts().sort_index()\n\nplt.figure(figsize=(12, 6))\nmonthly_counts_data.plot(kind='bar', color='skyblue')\nplt.title('Count of Data for Each Month for All data', fontsize=14)\nplt.xlabel('Month', fontsize=12)\nplt.ylabel('Count', fontsize=12)\nplt.xticks(rotation=45)\nplt.grid(axis='y', linestyle='--', alpha=0.7)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T13:51:27.462553Z","iopub.execute_input":"2025-01-15T13:51:27.462917Z","iopub.status.idle":"2025-01-15T13:51:34.997522Z","shell.execute_reply.started":"2025-01-15T13:51:27.462885Z","shell.execute_reply":"2025-01-15T13:51:34.996500Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%%time\nTHRESHOLD_TRAIN_DATE = pd.to_datetime(\"2017-07-01\")\nTHRESHOLD_TEST_DATE = pd.to_datetime(\"2017-08-01\")\n\ndata[data[\"date\"] < THRESHOLD_TRAIN_DATE].to_csv(\"/kaggle/working/train_data.csv\")\ndata[(data[\"date\"] >= THRESHOLD_TRAIN_DATE)&(data[\"date\"] < THRESHOLD_TEST_DATE)].to_csv(\"/kaggle/working/test_data.csv\")\n\n%xdel data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T14:00:06.098531Z","iopub.execute_input":"2025-01-15T14:00:06.098966Z","iopub.status.idle":"2025-01-15T14:07:37.122220Z","shell.execute_reply.started":"2025-01-15T14:00:06.098938Z","shell.execute_reply":"2025-01-15T14:07:37.120679Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%%time\ndf = pd.read_csv(\"/kaggle/working/train_data.csv\")\ntest_df = pd.read_csv(\"/kaggle/working/test_data.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T14:08:03.506592Z","iopub.execute_input":"2025-01-15T14:08:03.507012Z","iopub.status.idle":"2025-01-15T14:09:52.580423Z","shell.execute_reply.started":"2025-01-15T14:08:03.506963Z","shell.execute_reply":"2025-01-15T14:09:52.579447Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df[\"date\"] = pd.to_datetime(df[\"date\"])\ntest_df[\"date\"] = pd.to_datetime(test_df[\"date\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T14:10:01.926786Z","iopub.execute_input":"2025-01-15T14:10:01.927131Z","iopub.status.idle":"2025-01-15T14:10:13.649995Z","shell.execute_reply.started":"2025-01-15T14:10:01.927106Z","shell.execute_reply":"2025-01-15T14:10:13.648921Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"monthly_counts = df['date'].dt.to_period('M').value_counts().sort_index().reset_index()\nmonthly_counts_test = test_df['date'].dt.to_period('M').value_counts().sort_index().reset_index()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T14:11:57.277669Z","iopub.execute_input":"2025-01-15T14:11:57.278183Z","iopub.status.idle":"2025-01-15T14:12:03.675340Z","shell.execute_reply.started":"2025-01-15T14:11:57.278154Z","shell.execute_reply":"2025-01-15T14:12:03.674172Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"monthly_counts['type'] = 'Train'\nmonthly_counts_test['type'] = 'Test'\ncombined = pd.concat([monthly_counts, monthly_counts_test])\n\nplt.figure(figsize=(12, 6))\nsns.barplot(data=combined, x='date', y='count', hue='type', palette=['blue', 'red'])\nplt.title('Count of Data for Each Month (Train vs Test)', fontsize=14)\nplt.xlabel('Month', fontsize=12)\nplt.ylabel('Count', fontsize=12)\nplt.xticks(rotation=45)\nplt.legend(title='Dataset')\nplt.grid(axis='y', linestyle='--', alpha=0.7)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T14:12:21.654595Z","iopub.execute_input":"2025-01-15T14:12:21.655054Z","iopub.status.idle":"2025-01-15T14:12:22.986543Z","shell.execute_reply.started":"2025-01-15T14:12:21.655018Z","shell.execute_reply":"2025-01-15T14:12:22.985298Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T14:12:57.746462Z","iopub.execute_input":"2025-01-15T14:12:57.746901Z","iopub.status.idle":"2025-01-15T14:12:57.766461Z","shell.execute_reply.started":"2025-01-15T14:12:57.746872Z","shell.execute_reply":"2025-01-15T14:12:57.765277Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"THRESHOLD_TRAIN_DATE_LOWER = pd.to_datetime(\"2017-01-01\")\ndf = df[df[\"date\"] >= THRESHOLD_TRAIN_DATE_LOWER]\ndf = df.reset_index(drop=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T14:14:48.657795Z","iopub.execute_input":"2025-01-15T14:14:48.658216Z","iopub.status.idle":"2025-01-15T14:14:50.213612Z","shell.execute_reply.started":"2025-01-15T14:14:48.658179Z","shell.execute_reply":"2025-01-15T14:14:50.212485Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Additional Data\n\n\nTo make more effective use of the data, you also have access to several auxiliary datasets containing contextual information:\n\n#### 1. **`holidays_events.csv`**\n   - **Description**:\n     Contains information about holidays and events that may influence sales.\n   - **Useful Columns**:\n     - `date`: The date of the holiday or event.\n     - `type`: The type of event (national, regional, local).\n     - `locale`: The level of impact (national, regional, local).\n     - `description`: A description of the event.\n   - **How to Use**:\n     - Add a flag indicating whether a date is a holiday to the training and test sets.\n     - Categorize holidays by their `type` and consider their impact on sales.\n     - Use the `locale` column to analyze regional effects.","metadata":{"execution":{"iopub.status.busy":"2024-12-22T19:12:02.31896Z","iopub.execute_input":"2024-12-22T19:12:02.319553Z","iopub.status.idle":"2024-12-22T19:12:02.32548Z","shell.execute_reply.started":"2024-12-22T19:12:02.319516Z","shell.execute_reply":"2024-12-22T19:12:02.323891Z"}}},{"cell_type":"code","source":"holiday_events = pd.read_csv(\"/kaggle/working/holidays_events.csv\")\nprint(holiday_events.shape)\nholiday_events.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T14:23:09.052660Z","iopub.execute_input":"2025-01-15T14:23:09.053197Z","iopub.status.idle":"2025-01-15T14:23:09.076188Z","shell.execute_reply.started":"2025-01-15T14:23:09.053160Z","shell.execute_reply":"2025-01-15T14:23:09.075252Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"holiday_events['date'] = pd.to_datetime(holiday_events['date'])\nholiday_events = holiday_events.rename(columns={\"type\": \"holiday_type\"})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T14:29:34.983360Z","iopub.execute_input":"2025-01-15T14:29:34.983747Z","iopub.status.idle":"2025-01-15T14:29:34.995346Z","shell.execute_reply.started":"2025-01-15T14:29:34.983718Z","shell.execute_reply":"2025-01-15T14:29:34.994192Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = df.merge(holiday_events[['date', 'holiday_type', 'locale']], on='date', how='left')\ntest_df = test_df.merge(holiday_events[['date', 'holiday_type', 'locale']], on='date', how='left')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T14:29:58.936106Z","iopub.execute_input":"2025-01-15T14:29:58.936473Z","iopub.status.idle":"2025-01-15T14:30:02.380403Z","shell.execute_reply.started":"2025-01-15T14:29:58.936446Z","shell.execute_reply":"2025-01-15T14:30:02.379307Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df['is_holiday'] = test_df['holiday_type'].notna().astype(int)\ndf['is_holiday'] = df['holiday_type'].notna().astype(int)\n\ndf.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T14:30:48.240636Z","iopub.execute_input":"2025-01-15T14:30:48.241007Z","iopub.status.idle":"2025-01-15T14:30:48.931720Z","shell.execute_reply.started":"2025-01-15T14:30:48.240981Z","shell.execute_reply":"2025-01-15T14:30:48.930784Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#### 2. **`items.csv`**\n   - **Description**:\n     Contains information about the items being sold.\n   - **Useful Columns**:\n     - `item_nbr`: Unique identifier for each item.\n     - `family`: The category or family the item belongs to (e.g., \"Beverages\").\n     - `class`: A finer categorization of items.\n     - `perishable`: A binary indicator for whether the item is perishable (1 for perishable, 0 otherwise).\n   - **How to Use**:\n     - Use `family` and `class` to group items and analyze trends across categories.\n     - Include the `perishable` indicator to weight predictions for perishable items.","metadata":{}},{"cell_type":"code","source":"items = pd.read_csv(\"/kaggle/working/items.csv\")\nprint(items.shape)\nitems.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T14:33:38.012643Z","iopub.execute_input":"2025-01-15T14:33:38.013083Z","iopub.status.idle":"2025-01-15T14:33:38.032299Z","shell.execute_reply.started":"2025-01-15T14:33:38.013050Z","shell.execute_reply":"2025-01-15T14:33:38.031475Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"counts = items[\"family\"].value_counts()\n\nplt.figure(figsize=(20, 12))\nplt.bar(x=counts.index, height=counts.values)\nplt.xlabel('Family', fontsize=12)\nplt.ylabel('Quantity', fontsize=12)\nplt.xticks(rotation=90)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T14:42:01.440734Z","iopub.execute_input":"2025-01-15T14:42:01.441116Z","iopub.status.idle":"2025-01-15T14:42:01.996222Z","shell.execute_reply.started":"2025-01-15T14:42:01.441084Z","shell.execute_reply":"2025-01-15T14:42:01.995132Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = df.merge(items[[\"item_nbr\", \"family\", \"class\", \"perishable\"]], how=\"left\", on=\"item_nbr\")\ntest_df = test_df.merge(items[[\"item_nbr\", \"family\", \"class\", \"perishable\"]], how=\"left\", on=\"item_nbr\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T14:54:16.349274Z","iopub.execute_input":"2025-01-15T14:54:16.349763Z","iopub.status.idle":"2025-01-15T14:54:23.606033Z","shell.execute_reply.started":"2025-01-15T14:54:16.349728Z","shell.execute_reply":"2025-01-15T14:54:23.604884Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#### 3. **`oil.csv`**\n   - **Description**:\n     Contains daily oil price data, which can serve as an external economic indicator.\n   - **Useful Columns**:\n     - `date`: The date corresponding to the oil price.\n     - `dcoilwtico`: The daily oil price.\n   - **How to Use**:\n     - Join this dataset with the training and test sets on the `date` column to include oil price as a feature.\n     - Analyze the correlation between oil price fluctuations and sales trends.","metadata":{}},{"cell_type":"code","source":"oil = pd.read_csv(\"/kaggle/working/oil.csv\")\nprint(oil.shape)\noil.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T14:54:34.108503Z","iopub.execute_input":"2025-01-15T14:54:34.109048Z","iopub.status.idle":"2025-01-15T14:54:34.133096Z","shell.execute_reply.started":"2025-01-15T14:54:34.109004Z","shell.execute_reply":"2025-01-15T14:54:34.131713Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"oil['date'] = pd.to_datetime(oil['date'])\ndf = df.merge(oil, how=\"left\", on=\"date\")\ntest_df = test_df.merge(oil, how=\"left\", on=\"date\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T14:55:46.852609Z","iopub.execute_input":"2025-01-15T14:55:46.853027Z","iopub.status.idle":"2025-01-15T14:55:55.699963Z","shell.execute_reply.started":"2025-01-15T14:55:46.853001Z","shell.execute_reply":"2025-01-15T14:55:55.698977Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df[\"dcoilwtico\"].ffill(inplace=True)\ntest_df[\"dcoilwtico\"].ffill(inplace=True)\n\ndf[\"dcoilwtico\"].bfill(inplace=True)\ntest_df[\"dcoilwtico\"].bfill(inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T14:56:31.631357Z","iopub.execute_input":"2025-01-15T14:56:31.631791Z","iopub.status.idle":"2025-01-15T14:56:31.781212Z","shell.execute_reply.started":"2025-01-15T14:56:31.631758Z","shell.execute_reply":"2025-01-15T14:56:31.780180Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df[\"unit_sales\"].corr(df[\"dcoilwtico\"])\n# we observe low correlation between features","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T14:57:07.596272Z","iopub.execute_input":"2025-01-15T14:57:07.596737Z","iopub.status.idle":"2025-01-15T14:57:07.911846Z","shell.execute_reply.started":"2025-01-15T14:57:07.596703Z","shell.execute_reply":"2025-01-15T14:57:07.910740Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#### 4. **`stores.csv`**\n   - **Description**:\n     Contains information about the stores where items are sold.\n   - **Useful Columns**:\n     - `store_nbr`: Unique identifier for each store.\n     - `city`: The city where the store is located.\n     - `state`: The state where the store is located.\n     - `type`: The store type.\n     - `cluster`: A grouping of similar stores based on sales patterns.\n   - **How to Use**:\n     - Use `city` and `state` for regional analysis of sales trends.\n     - Incorporate `type` and `cluster` to identify patterns and group stores with similar behavior.\n","metadata":{}},{"cell_type":"code","source":"stores = pd.read_csv(\"/kaggle/working/stores.csv\")\nprint(stores.shape)\nstores.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T15:07:39.630729Z","iopub.execute_input":"2025-01-15T15:07:39.631201Z","iopub.status.idle":"2025-01-15T15:07:39.650932Z","shell.execute_reply.started":"2025-01-15T15:07:39.631169Z","shell.execute_reply":"2025-01-15T15:07:39.649854Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"stores = stores.rename(columns={\"type\": \"store_type\"})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T15:07:46.798829Z","iopub.execute_input":"2025-01-15T15:07:46.799168Z","iopub.status.idle":"2025-01-15T15:07:46.804765Z","shell.execute_reply.started":"2025-01-15T15:07:46.799144Z","shell.execute_reply":"2025-01-15T15:07:46.803830Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = df.merge(stores, on='store_nbr', how='left')\ntest_df = test_df.merge(stores, on='store_nbr', how='left')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T15:08:11.108330Z","iopub.execute_input":"2025-01-15T15:08:11.108794Z","iopub.status.idle":"2025-01-15T15:08:18.259240Z","shell.execute_reply.started":"2025-01-15T15:08:11.108759Z","shell.execute_reply":"2025-01-15T15:08:18.258147Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**[READ DATA DESCRIPTION CAREFULY](https://www.kaggle.com/competitions/favorita-grocery-sales-forecasting) !!!!** \n\n## Exploratory Data Analysis and Model Training\n\n### 1. Baseline Model\n- Consider which model would be suitable as `DummyModel` for this task, then create, train, and use its predictions as a baseline.\n\n### 2. Feature Engineering\n- Generate new features by incorporating data from additional datasets to enhance the model's predictive capabilities.\n\n### 3. Model Training and Optimization\n- Utilize advanced regression models, such as **LGBM Regression** or **CatBoost Regression**, to achieve the best results. Focus on hyperparameter tuning and evaluation metrics to optimize performance. **Use foremention Normalized Weighted Root Mean Squared Logarithmic Error**.  \n\n### Thats All :-)\n","metadata":{}},{"cell_type":"code","source":"from sklearn.dummy import DummyRegressor\nfrom sklearn.compose import ColumnTransformer\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.preprocessing import StandardScaler, OneHotEncoder\nfrom sklearn.impute import SimpleImputer\nimport lightgbm as lgb\nimport optuna\nfrom sklearn.metrics import mean_squared_error as mse","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T15:22:14.415151Z","iopub.execute_input":"2025-01-15T15:22:14.415555Z","iopub.status.idle":"2025-01-15T15:22:14.420473Z","shell.execute_reply.started":"2025-01-15T15:22:14.415523Z","shell.execute_reply":"2025-01-15T15:22:14.419220Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dummy_regr = DummyRegressor(strategy=\"mean\")\ndummy_regr.fit(df, df['unit_sales'].values)\n\ndummy_prediction = dummy_regr.predict(test_df)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T15:10:44.514019Z","iopub.execute_input":"2025-01-15T15:10:44.514428Z","iopub.status.idle":"2025-01-15T15:10:44.563768Z","shell.execute_reply.started":"2025-01-15T15:10:44.514376Z","shell.execute_reply":"2025-01-15T15:10:44.562373Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"mse(dummy_prediction, test_df['unit_sales'].values)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T15:23:02.194530Z","iopub.execute_input":"2025-01-15T15:23:02.194970Z","iopub.status.idle":"2025-01-15T15:23:02.222218Z","shell.execute_reply.started":"2025-01-15T15:23:02.194940Z","shell.execute_reply":"2025-01-15T15:23:02.221171Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.isna().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T15:23:33.338724Z","iopub.execute_input":"2025-01-15T15:23:33.339082Z","iopub.status.idle":"2025-01-15T15:23:39.164838Z","shell.execute_reply.started":"2025-01-15T15:23:33.339055Z","shell.execute_reply":"2025-01-15T15:23:39.163743Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['locale'] = df.locale.fillna('None')\ntest_df['locale'] = test_df.locale.fillna('None')\n\ndf['holiday_type'] = df.locale.fillna('Not holiday')\ntest_df['holiday_type'] = test_df.locale.fillna('Not holiday')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T15:24:24.327704Z","iopub.execute_input":"2025-01-15T15:24:24.328065Z","iopub.status.idle":"2025-01-15T15:24:27.145452Z","shell.execute_reply.started":"2025-01-15T15:24:24.328040Z","shell.execute_reply":"2025-01-15T15:24:27.144373Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T15:25:18.360798Z","iopub.execute_input":"2025-01-15T15:25:18.361220Z","iopub.status.idle":"2025-01-15T15:25:18.378732Z","shell.execute_reply.started":"2025-01-15T15:25:18.361191Z","shell.execute_reply":"2025-01-15T15:25:18.377747Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"holiday_type_ = {\"Transfer\": 2, \"Holiday\": 2, \"Additional\": 2, \"Event\": 1, \"Not holiday\": 0}\n\ndf['holiday_type_ec'] = df['holiday_type'].map(holiday_type_)\ntest_df['holiday_type_ec'] = test_df['holiday_type'].map(holiday_type_)\n\ndf.head()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['onpromotion'] = df['onpromotion'].map({\"False\": 0, \"True\": 1})\ntest_df['onpromotion'] = test_df['onpromotion'].map({\"False\": 0, \"True\": 1})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T15:32:21.252157Z","iopub.execute_input":"2025-01-15T15:32:21.252580Z","iopub.status.idle":"2025-01-15T15:32:22.623984Z","shell.execute_reply.started":"2025-01-15T15:32:21.252542Z","shell.execute_reply":"2025-01-15T15:32:22.623062Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T15:29:36.808373Z","iopub.execute_input":"2025-01-15T15:29:36.808879Z","iopub.status.idle":"2025-01-15T15:29:36.816317Z","shell.execute_reply.started":"2025-01-15T15:29:36.808846Z","shell.execute_reply":"2025-01-15T15:29:36.815046Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"preprocessor = ColumnTransformer(\n    [\n        ('scaler', StandardScaler(), ['dcoilwtico']),\n        ('one_hot', OneHotEncoder(handle_unknown='ignore'), ['family', 'city', 'state', 'store_type', 'cluster', 'holiday_type', 'locale']),\n        ('imputer', SimpleImputer(strategy=\"median\"), \n         ['onpromotion', 'is_holiday', 'perishable']\n        )\n    ]\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T15:33:37.144546Z","iopub.execute_input":"2025-01-15T15:33:37.145003Z","iopub.status.idle":"2025-01-15T15:33:37.150558Z","shell.execute_reply.started":"2025-01-15T15:33:37.144973Z","shell.execute_reply":"2025-01-15T15:33:37.149313Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train = df[[\n    'onpromotion', 'is_holiday', 'perishable', \n    'dcoilwtico', 'holiday_type', 'locale',\n    'family', 'city', 'state', 'store_type', 'cluster'\n]]\nX_test = test_df[[\n    'onpromotion', 'is_holiday', 'perishable', \n    'dcoilwtico', 'holiday_type', 'locale',\n    'family', 'city', 'state', 'store_type', 'cluster'\n]]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T15:33:48.943784Z","iopub.execute_input":"2025-01-15T15:33:48.944124Z","iopub.status.idle":"2025-01-15T15:33:51.682902Z","shell.execute_reply.started":"2025-01-15T15:33:48.944101Z","shell.execute_reply":"2025-01-15T15:33:51.681981Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_preprocessed = preprocessor.fit_transform(X_train)\nX_test_preprocessed = preprocessor.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T15:34:22.265947Z","iopub.execute_input":"2025-01-15T15:34:22.266285Z","iopub.status.idle":"2025-01-15T15:35:18.413328Z","shell.execute_reply.started":"2025-01-15T15:34:22.266259Z","shell.execute_reply":"2025-01-15T15:35:18.412178Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train = df['unit_sales'].values\ny_test = test_df['unit_sales'].values","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T15:35:30.404080Z","iopub.execute_input":"2025-01-15T15:35:30.404544Z","iopub.status.idle":"2025-01-15T15:35:30.409593Z","shell.execute_reply.started":"2025-01-15T15:35:30.404508Z","shell.execute_reply":"2025-01-15T15:35:30.408504Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data = lgb.Dataset(X_train_preprocessed, label=y_train)\nval_data = lgb.Dataset(X_test_preprocessed, label=y_test, reference=train_data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T15:35:45.979019Z","iopub.execute_input":"2025-01-15T15:35:45.979371Z","iopub.status.idle":"2025-01-15T15:35:45.985412Z","shell.execute_reply.started":"2025-01-15T15:35:45.979347Z","shell.execute_reply":"2025-01-15T15:35:45.984319Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"grid = {\n    'num_leaves': 100,\n    'objective': 'regression',\n    'min_data_in_leaf': 200,\n    'learning_rate': 0.01,\n    'feature_fraction': 0.8,\n    'bagging_fraction': 0.7,\n    'metric': 'f1',\n    'num_threads': 12\n}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T15:42:28.976883Z","iopub.execute_input":"2025-01-15T15:42:28.977237Z","iopub.status.idle":"2025-01-15T15:42:28.981917Z","shell.execute_reply.started":"2025-01-15T15:42:28.977210Z","shell.execute_reply":"2025-01-15T15:42:28.980958Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"result = lgb.train(\n    grid, \n    train_data, \n    num_boost_round=1000,\n    valid_sets=[train_data, val_data]\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T15:42:34.318516Z","iopub.execute_input":"2025-01-15T15:42:34.319017Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lgbm_prediction = result.predict(X_test_preprocessed)\nlgbm_score = mse(y_pred=lgbm_prediction, y_true=y_test)\nlgbm_score","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-15T15:41:04.620960Z","iopub.execute_input":"2025-01-15T15:41:04.621301Z","iopub.status.idle":"2025-01-15T15:41:10.991934Z","shell.execute_reply.started":"2025-01-15T15:41:04.621275Z","shell.execute_reply":"2025-01-15T15:41:10.990982Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# well, it's better than for dummyregressor","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}