{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":53998,"databundleVersionId":5880353,"sourceType":"competition"}],"dockerImageVersionId":31153,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"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\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\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\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(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-10-27T18:32:00.112776Z","iopub.execute_input":"2025-10-27T18:32:00.113290Z","iopub.status.idle":"2025-10-27T18:32:00.442624Z","shell.execute_reply.started":"2025-10-27T18:32:00.113252Z","shell.execute_reply":"2025-10-27T18:32:00.441779Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport lightgbm as lgb\nfrom datetime import datetime \nimport warnings\n\nwarnings.filterwarnings('ignore')\n\nplt.style.use('seaborn-v0_8-darkgrid')\nsns.set_palette(\"husl\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:32:00.444028Z","iopub.execute_input":"2025-10-27T18:32:00.444426Z","iopub.status.idle":"2025-10-27T18:32:06.765418Z","shell.execute_reply.started":"2025-10-27T18:32:00.444405Z","shell.execute_reply":"2025-10-27T18:32:06.764715Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/Store-Sales-Forecasting/train.csv', parse_dates=['date'])\ntest = pd.read_csv('/kaggle/input/Store-Sales-Forecasting/test.csv', parse_dates=['date'])\nstores = pd.read_csv('/kaggle/input/Store-Sales-Forecasting/stores.csv')\noil = pd.read_csv('/kaggle/input/Store-Sales-Forecasting/oil.csv', parse_dates=['date'])\nholidays = pd.read_csv('/kaggle/input/Store-Sales-Forecasting/holidays_events.csv', parse_dates=['date'])\ntransactions = pd.read_csv('/kaggle/input/Store-Sales-Forecasting/transactions.csv', parse_dates=['date'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:32:06.766252Z","iopub.execute_input":"2025-10-27T18:32:06.766768Z","iopub.status.idle":"2025-10-27T18:32:10.079548Z","shell.execute_reply.started":"2025-10-27T18:32:06.766747Z","shell.execute_reply":"2025-10-27T18:32:10.078479Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:32:10.080494Z","iopub.execute_input":"2025-10-27T18:32:10.080779Z","iopub.status.idle":"2025-10-27T18:32:10.105488Z","shell.execute_reply.started":"2025-10-27T18:32:10.080752Z","shell.execute_reply":"2025-10-27T18:32:10.104652Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:32:10.107796Z","iopub.execute_input":"2025-10-27T18:32:10.108099Z","iopub.status.idle":"2025-10-27T18:32:10.113356Z","shell.execute_reply.started":"2025-10-27T18:32:10.108079Z","shell.execute_reply":"2025-10-27T18:32:10.112609Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:32:10.114119Z","iopub.execute_input":"2025-10-27T18:32:10.114357Z","iopub.status.idle":"2025-10-27T18:32:10.140394Z","shell.execute_reply.started":"2025-10-27T18:32:10.114338Z","shell.execute_reply":"2025-10-27T18:32:10.139430Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:32:10.141463Z","iopub.execute_input":"2025-10-27T18:32:10.142185Z","iopub.status.idle":"2025-10-27T18:32:10.334842Z","shell.execute_reply.started":"2025-10-27T18:32:10.142162Z","shell.execute_reply":"2025-10-27T18:32:10.333961Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['family'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:32:10.335771Z","iopub.execute_input":"2025-10-27T18:32:10.336026Z","iopub.status.idle":"2025-10-27T18:32:10.568286Z","shell.execute_reply.started":"2025-10-27T18:32:10.335998Z","shell.execute_reply":"2025-10-27T18:32:10.567369Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"stores.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:32:10.569337Z","iopub.execute_input":"2025-10-27T18:32:10.569625Z","iopub.status.idle":"2025-10-27T18:32:10.589677Z","shell.execute_reply.started":"2025-10-27T18:32:10.569586Z","shell.execute_reply":"2025-10-27T18:32:10.588584Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"stores.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:32:10.590684Z","iopub.execute_input":"2025-10-27T18:32:10.591398Z","iopub.status.idle":"2025-10-27T18:32:10.609512Z","shell.execute_reply.started":"2025-10-27T18:32:10.591376Z","shell.execute_reply":"2025-10-27T18:32:10.608688Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"stores.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:32:10.610375Z","iopub.execute_input":"2025-10-27T18:32:10.610657Z","iopub.status.idle":"2025-10-27T18:32:10.627031Z","shell.execute_reply.started":"2025-10-27T18:32:10.610637Z","shell.execute_reply":"2025-10-27T18:32:10.626157Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"stores.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:32:10.627987Z","iopub.execute_input":"2025-10-27T18:32:10.628305Z","iopub.status.idle":"2025-10-27T18:32:10.641791Z","shell.execute_reply.started":"2025-10-27T18:32:10.628278Z","shell.execute_reply":"2025-10-27T18:32:10.640837Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"stores['city'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:32:10.642627Z","iopub.execute_input":"2025-10-27T18:32:10.642911Z","iopub.status.idle":"2025-10-27T18:32:10.661413Z","shell.execute_reply.started":"2025-10-27T18:32:10.642866Z","shell.execute_reply":"2025-10-27T18:32:10.660470Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"stores['state'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:32:10.663721Z","iopub.execute_input":"2025-10-27T18:32:10.664007Z","iopub.status.idle":"2025-10-27T18:32:10.679321Z","shell.execute_reply.started":"2025-10-27T18:32:10.663987Z","shell.execute_reply":"2025-10-27T18:32:10.678498Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"stores['type'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:32:10.680328Z","iopub.execute_input":"2025-10-27T18:32:10.680621Z","iopub.status.idle":"2025-10-27T18:32:10.697068Z","shell.execute_reply.started":"2025-10-27T18:32:10.680602Z","shell.execute_reply":"2025-10-27T18:32:10.696295Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"daily_sales = train.groupby('date')['sales'].sum().reset_index()\n\nplt.figure(figsize=(15, 6))\nplt.plot(daily_sales['date'], daily_sales['sales'], linewidth=1)\nplt.title('Günlük Toplam Satışlar', fontsize=16, fontweight='bold')\nplt.xlabel('Tarih')\nplt.ylabel('Satış')\nplt.grid(True, alpha=0.3)\nplt.tight_layout()\nplt.savefig('daily_sales.png', dpi=300, bbox_inches='tight')\nplt.show()  # Grafiği görüntüle","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:36:40.547605Z","iopub.execute_input":"2025-10-27T18:36:40.548192Z","iopub.status.idle":"2025-10-27T18:36:41.833343Z","shell.execute_reply.started":"2025-10-27T18:36:40.548170Z","shell.execute_reply":"2025-10-27T18:36:41.832396Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(12, 8))\ntop_families = train.groupby('family')['sales'].sum().sort_values(ascending=True).tail(15)\ntop_families.plot(kind='barh')\nplt.title('En Çok Satan 15 Ürün Ailesi', fontsize=16, fontweight='bold')\nplt.xlabel('Toplam Satış')\nplt.ylabel('Ürün Ailesi')\nplt.tight_layout()\nplt.savefig('top_families.png', dpi=300, bbox_inches='tight') ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:33:25.489426Z","iopub.execute_input":"2025-10-27T18:33:25.489708Z","iopub.status.idle":"2025-10-27T18:33:26.844801Z","shell.execute_reply.started":"2025-10-27T18:33:25.489690Z","shell.execute_reply":"2025-10-27T18:33:26.843805Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(12, 6))\ntop_stores = train.groupby('store_nbr')['sales'].sum().sort_values(ascending=False).head(15)\nplt.bar(range(len(top_stores)), top_stores.values)\nplt.title('En Çok Satan 15 Mağaza', fontsize=16, fontweight='bold')\nplt.xlabel('Mağaza Numarası')\nplt.ylabel('Toplam Satış')\nplt.xticks(range(len(top_stores)), top_stores.index)\nplt.tight_layout()\nplt.savefig('top_stores.png', dpi=300, bbox_inches='tight')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:34:00.966020Z","iopub.execute_input":"2025-10-27T18:34:00.966342Z","iopub.status.idle":"2025-10-27T18:34:01.755657Z","shell.execute_reply.started":"2025-10-27T18:34:00.966322Z","shell.execute_reply":"2025-10-27T18:34:01.754740Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(15, 6))\nplt.plot(oil['date'], oil['dcoilwtico'], linewidth=1)\nplt.title('Petrol Fiyatları (WTI)', fontsize=16, fontweight='bold')\nplt.xlabel('Tarih')\nplt.ylabel('Fiyat ($)')\nplt.grid(True, alpha=0.3)\nplt.tight_layout()\nplt.savefig('oil_prices.png', dpi=300, bbox_inches='tight')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:34:33.632483Z","iopub.execute_input":"2025-10-27T18:34:33.633364Z","iopub.status.idle":"2025-10-27T18:34:34.590526Z","shell.execute_reply.started":"2025-10-27T18:34:33.633331Z","shell.execute_reply":"2025-10-27T18:34:34.589607Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['day_of_week'] = train['date'].dt.dayofweek\nweekly_sales = train.groupby('day_of_week')['sales'].mean()\ndays = ['Pazartesi', 'Salı', 'Çarşamba', 'Perşembe', 'Cuma', 'Cumartesi', 'Pazar']\n\nplt.figure(figsize=(10, 6))\nplt.bar(range(7), weekly_sales.values)\nplt.title('Hafta İçi Ortalama Satışlar', fontsize=16, fontweight='bold')\nplt.xlabel('Gün')\nplt.ylabel('Ortalama Satış')\nplt.xticks(range(7), days, rotation=45)\nplt.tight_layout()\nplt.savefig('weekly_sales.png', dpi=300, bbox_inches='tight') ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:35:14.660246Z","iopub.execute_input":"2025-10-27T18:35:14.660566Z","iopub.status.idle":"2025-10-27T18:35:15.423628Z","shell.execute_reply.started":"2025-10-27T18:35:14.660543Z","shell.execute_reply":"2025-10-27T18:35:15.422697Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"oil.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:38:16.509288Z","iopub.execute_input":"2025-10-27T18:38:16.509712Z","iopub.status.idle":"2025-10-27T18:38:16.522009Z","shell.execute_reply.started":"2025-10-27T18:38:16.509690Z","shell.execute_reply":"2025-10-27T18:38:16.521030Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"oil.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:40:20.325007Z","iopub.execute_input":"2025-10-27T18:40:20.325362Z","iopub.status.idle":"2025-10-27T18:40:20.332115Z","shell.execute_reply.started":"2025-10-27T18:40:20.325340Z","shell.execute_reply":"2025-10-27T18:40:20.331391Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"oil['dcoilwtico'] = oil['dcoilwtico'].fillna(method='ffill').fillna(method='bfill')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:40:57.984050Z","iopub.execute_input":"2025-10-27T18:40:57.984373Z","iopub.status.idle":"2025-10-27T18:40:57.994900Z","shell.execute_reply.started":"2025-10-27T18:40:57.984351Z","shell.execute_reply":"2025-10-27T18:40:57.993934Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"transactions.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:38:44.339255Z","iopub.execute_input":"2025-10-27T18:38:44.339570Z","iopub.status.idle":"2025-10-27T18:38:44.348764Z","shell.execute_reply.started":"2025-10-27T18:38:44.339551Z","shell.execute_reply":"2025-10-27T18:38:44.347858Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"transactions.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:45:14.761641Z","iopub.execute_input":"2025-10-27T18:45:14.762029Z","iopub.status.idle":"2025-10-27T18:45:14.770683Z","shell.execute_reply.started":"2025-10-27T18:45:14.762004Z","shell.execute_reply":"2025-10-27T18:45:14.769726Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"holidays.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:39:37.402157Z","iopub.execute_input":"2025-10-27T18:39:37.402801Z","iopub.status.idle":"2025-10-27T18:39:37.414807Z","shell.execute_reply.started":"2025-10-27T18:39:37.402775Z","shell.execute_reply":"2025-10-27T18:39:37.414045Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"holidays.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:45:32.829899Z","iopub.execute_input":"2025-10-27T18:45:32.830663Z","iopub.status.idle":"2025-10-27T18:45:32.839086Z","shell.execute_reply.started":"2025-10-27T18:45:32.830628Z","shell.execute_reply":"2025-10-27T18:45:32.838141Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"holidays['type'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:47:08.313551Z","iopub.execute_input":"2025-10-27T18:47:08.314164Z","iopub.status.idle":"2025-10-27T18:47:08.321101Z","shell.execute_reply.started":"2025-10-27T18:47:08.314137Z","shell.execute_reply":"2025-10-27T18:47:08.320331Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"holidays['description'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:48:00.132617Z","iopub.execute_input":"2025-10-27T18:48:00.132972Z","iopub.status.idle":"2025-10-27T18:48:00.141259Z","shell.execute_reply.started":"2025-10-27T18:48:00.132950Z","shell.execute_reply":"2025-10-27T18:48:00.140295Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Ulusal tatiller\nnational_holidays = holidays[holidays['locale'] == 'National'].copy()\nnational_holidays['is_national_holiday'] = 1\nnational_holidays = national_holidays[['date', 'is_national_holiday']].drop_duplicates()\n\n# Bölgesel tatiller\nregional_holidays = holidays[holidays['locale'] == 'Regional'].copy()\nregional_holidays['is_regional_holiday'] = 1\nregional_holidays = regional_holidays[['date', 'is_regional_holiday']].drop_duplicates()\n\n# Yerel tatiller\nlocal_holidays = holidays[holidays['locale'] == 'Local'].copy()\nlocal_holidays['is_local_holiday'] = 1\nlocal_holidays = local_holidays[['date', 'is_local_holiday']].drop_duplicates()\n\n# Çalışma günü mi tatil mi\nwork_holidays = holidays[holidays['type'] == 'Work Day'].copy()\nwork_holidays['is_work_day'] = 1\nwork_holidays = work_holidays[['date', 'is_work_day']].drop_duplicates()\n\n# Transfer edilmiş tatil\ntransferred_holidays = holidays[holidays['transferred'] == True].copy()\ntransferred_holidays['is_transferred'] = 1\ntransferred_holidays = transferred_holidays[['date', 'is_transferred']].drop_duplicates()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:48:34.602549Z","iopub.execute_input":"2025-10-27T18:48:34.603358Z","iopub.status.idle":"2025-10-27T18:48:34.627463Z","shell.execute_reply.started":"2025-10-27T18:48:34.603331Z","shell.execute_reply":"2025-10-27T18:48:34.626646Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = train.merge(stores, on='store_nbr', how='left')\ntrain = train.merge(oil, on='date', how='left')\ntrain = train.merge(transactions, on=['date', 'store_nbr'], how='left')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:48:58.972486Z","iopub.execute_input":"2025-10-27T18:48:58.972754Z","iopub.status.idle":"2025-10-27T18:49:00.759606Z","shell.execute_reply.started":"2025-10-27T18:48:58.972737Z","shell.execute_reply":"2025-10-27T18:49:00.758770Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Tatil bilgilerini ekle\ntrain = train.merge(national_holidays, on='date', how='left')\ntrain = train.merge(regional_holidays, on='date', how='left')\ntrain = train.merge(local_holidays, on='date', how='left')\ntrain = train.merge(work_holidays, on='date', how='left')\ntrain = train.merge(transferred_holidays, on='date', how='left')\n\n# Tatil sütunlarındaki NaN'ları 0 ile doldur\ntrain['is_national_holiday'] = train['is_national_holiday'].fillna(0)\ntrain['is_regional_holiday'] = train['is_regional_holiday'].fillna(0)\ntrain['is_local_holiday'] = train['is_local_holiday'].fillna(0)\ntrain['is_work_day'] = train['is_work_day'].fillna(0)\ntrain['is_transferred'] = train['is_transferred'].fillna(0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:49:24.062958Z","iopub.execute_input":"2025-10-27T18:49:24.063267Z","iopub.status.idle":"2025-10-27T18:49:26.107319Z","shell.execute_reply.started":"2025-10-27T18:49:24.063245Z","shell.execute_reply":"2025-10-27T18:49:26.106570Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test = test.merge(stores, on='store_nbr', how='left') \ntest = test.merge(oil, on='date', how='left') \ntest = test.merge(transactions, on=['date', 'store_nbr'], how='left')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:50:06.585704Z","iopub.execute_input":"2025-10-27T18:50:06.586045Z","iopub.status.idle":"2025-10-27T18:50:06.614124Z","shell.execute_reply.started":"2025-10-27T18:50:06.586023Z","shell.execute_reply":"2025-10-27T18:50:06.613232Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Tatil bilgileri\ntest = test.merge(national_holidays, on='date', how='left')\ntest = test.merge(regional_holidays, on='date', how='left')\ntest = test.merge(local_holidays, on='date', how='left')\ntest = test.merge(work_holidays, on='date', how='left')\ntest = test.merge(transferred_holidays, on='date', how='left')\n\ntest['is_national_holiday'] = test['is_national_holiday'].fillna(0)\ntest['is_regional_holiday'] = test['is_regional_holiday'].fillna(0)\ntest['is_local_holiday'] = test['is_local_holiday'].fillna(0)\ntest['is_work_day'] = test['is_work_day'].fillna(0)\ntest['is_transferred'] = test['is_transferred'].fillna(0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:50:27.453307Z","iopub.execute_input":"2025-10-27T18:50:27.453921Z","iopub.status.idle":"2025-10-27T18:50:27.484576Z","shell.execute_reply.started":"2025-10-27T18:50:27.453866Z","shell.execute_reply":"2025-10-27T18:50:27.483599Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# TRAIN için tarih özellikleri\ntrain['year'] = train['date'].dt.year\ntrain['month'] = train['date'].dt.month\ntrain['day'] = train['date'].dt.day\ntrain['day_of_week'] = train['date'].dt.dayofweek\ntrain['day_of_year'] = train['date'].dt.dayofyear\ntrain['week_of_year'] = train['date'].dt.isocalendar().week\ntrain['quarter'] = train['date'].dt.quarter\ntrain['is_weekend'] = (train['day_of_week'] >= 5).astype(int)\ntrain['is_month_start'] = train['date'].dt.is_month_start.astype(int)\ntrain['is_month_end'] = train['date'].dt.is_month_end.astype(int)\ntrain['is_quarter_start'] = train['date'].dt.is_quarter_start.astype(int)\ntrain['is_quarter_end'] = train['date'].dt.is_quarter_end.astype(int)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:51:20.391247Z","iopub.execute_input":"2025-10-27T18:51:20.392050Z","iopub.status.idle":"2025-10-27T18:51:21.427754Z","shell.execute_reply.started":"2025-10-27T18:51:20.392023Z","shell.execute_reply":"2025-10-27T18:51:21.426852Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# TEST için tarih özellikleri\ntest['year'] = test['date'].dt.year\ntest['month'] = test['date'].dt.month\ntest['day'] = test['date'].dt.day\ntest['day_of_week'] = test['date'].dt.dayofweek\ntest['day_of_year'] = test['date'].dt.dayofyear\ntest['week_of_year'] = test['date'].dt.isocalendar().week\ntest['quarter'] = test['date'].dt.quarter\ntest['is_weekend'] = (test['day_of_week'] >= 5).astype(int)\ntest['is_month_start'] = test['date'].dt.is_month_start.astype(int)\ntest['is_month_end'] = test['date'].dt.is_month_end.astype(int)\ntest['is_quarter_start'] = test['date'].dt.is_quarter_start.astype(int)\ntest['is_quarter_end'] = test['date'].dt.is_quarter_end.astype(int)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:51:33.136577Z","iopub.execute_input":"2025-10-27T18:51:33.136914Z","iopub.status.idle":"2025-10-27T18:51:33.163958Z","shell.execute_reply.started":"2025-10-27T18:51:33.136857Z","shell.execute_reply":"2025-10-27T18:51:33.162953Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = train.sort_values(['store_nbr', 'family', 'date'])\n\n# 7 gün önceki satışlar\ntrain['sales_lag_7'] = train.groupby(['store_nbr', 'family'])['sales'].shift(7)\n\n# 14 gün önceki satışlar\ntrain['sales_lag_14'] = train.groupby(['store_nbr', 'family'])['sales'].shift(14)\n\n# 28 gün önceki satışlar\ntrain['sales_lag_28'] = train.groupby(['store_nbr', 'family'])['sales'].shift(28)\n\n# 7 günlük hareketli ortalama\ntrain['sales_rolling_mean_7'] = train.groupby(['store_nbr', 'family'])['sales'].transform(\n    lambda x: x.shift(1).rolling(window=7, min_periods=1).mean()\n)\n\n# 14 günlük hareketli ortalama\ntrain['sales_rolling_mean_14'] = train.groupby(['store_nbr', 'family'])['sales'].transform(\n    lambda x: x.shift(1).rolling(window=14, min_periods=1).mean()\n)\n\n# 28 günlük hareketli ortalama\ntrain['sales_rolling_mean_28'] = train.groupby(['store_nbr', 'family'])['sales'].transform(\n    lambda x: x.shift(1).rolling(window=28, min_periods=1).mean()\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:52:20.739761Z","iopub.execute_input":"2025-10-27T18:52:20.740611Z","iopub.status.idle":"2025-10-27T18:52:26.070701Z","shell.execute_reply.started":"2025-10-27T18:52:20.740585Z","shell.execute_reply":"2025-10-27T18:52:26.069932Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"categorical_cols = ['family', 'city', 'state', 'type', 'cluster']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:52:58.017216Z","iopub.execute_input":"2025-10-27T18:52:58.017519Z","iopub.status.idle":"2025-10-27T18:52:58.021985Z","shell.execute_reply.started":"2025-10-27T18:52:58.017498Z","shell.execute_reply":"2025-10-27T18:52:58.020951Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"label_encodings = {}\nfor col in categorical_cols: \n    all_values = pd.concat([train[col], test[col]]).unique()\n    label_encodings[col] = {val: idx for idx, val in enumerate(all_values)}\n     \n    train[f'{col}_encoded'] = train[col].map(label_encodings[col])\n    test[f'{col}_encoded'] = test[col].map(label_encodings[col])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:53:09.922485Z","iopub.execute_input":"2025-10-27T18:53:09.923602Z","iopub.status.idle":"2025-10-27T18:53:11.192246Z","shell.execute_reply.started":"2025-10-27T18:53:09.923560Z","shell.execute_reply":"2025-10-27T18:53:11.191344Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['transactions'] = train['transactions'].fillna(train['transactions'].median())\ntest['transactions'] = test['transactions'].fillna(train['transactions'].median())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:53:27.189848Z","iopub.execute_input":"2025-10-27T18:53:27.190733Z","iopub.status.idle":"2025-10-27T18:53:27.266743Z","shell.execute_reply.started":"2025-10-27T18:53:27.190707Z","shell.execute_reply":"2025-10-27T18:53:27.265912Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lag_cols = [col for col in train.columns if 'lag' in col or 'rolling' in col]\nfor col in lag_cols:\n    train[col] = train[col].fillna(0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:53:47.727758Z","iopub.execute_input":"2025-10-27T18:53:47.728406Z","iopub.status.idle":"2025-10-27T18:53:47.867369Z","shell.execute_reply.started":"2025-10-27T18:53:47.728380Z","shell.execute_reply":"2025-10-27T18:53:47.866212Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.loc[train['sales'] < 0, 'sales'] = 0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:53:59.652218Z","iopub.execute_input":"2025-10-27T18:53:59.652535Z","iopub.status.idle":"2025-10-27T18:53:59.667135Z","shell.execute_reply.started":"2025-10-27T18:53:59.652511Z","shell.execute_reply":"2025-10-27T18:53:59.666015Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.to_csv('train_processed.csv', index=False)\ntest.to_csv('test_processed.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:54:20.191147Z","iopub.execute_input":"2025-10-27T18:54:20.191440Z","iopub.status.idle":"2025-10-27T18:55:21.619498Z","shell.execute_reply.started":"2025-10-27T18:54:20.191420Z","shell.execute_reply":"2025-10-27T18:55:21.618759Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pickle\nwith open('label_encodings.pkl', 'wb') as f:\n    pickle.dump(label_encodings, f)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:55:21.620818Z","iopub.execute_input":"2025-10-27T18:55:21.621176Z","iopub.status.idle":"2025-10-27T18:55:21.625801Z","shell.execute_reply.started":"2025-10-27T18:55:21.621156Z","shell.execute_reply":"2025-10-27T18:55:21.624767Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"feature_cols = [col for col in train.columns if col not in \n                ['id', 'date', 'sales', 'family', 'city', 'state', 'type']]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:55:21.626699Z","iopub.execute_input":"2025-10-27T18:55:21.626960Z","iopub.status.idle":"2025-10-27T18:55:21.643424Z","shell.execute_reply.started":"2025-10-27T18:55:21.626943Z","shell.execute_reply":"2025-10-27T18:55:21.642437Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Kullanılmayacak sütunlar\ndrop_cols = ['id', 'date', 'sales', 'family', 'city', 'state', 'type']\n\n# Mevcut sütunlardan olmayanları çıkar\ndrop_cols = [col for col in drop_cols if col in train.columns]\n\n# Özellik sütunları\nfeature_cols = [col for col in train.columns if col not in drop_cols]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:55:21.645040Z","iopub.execute_input":"2025-10-27T18:55:21.645345Z","iopub.status.idle":"2025-10-27T18:55:21.660637Z","shell.execute_reply.started":"2025-10-27T18:55:21.645319Z","shell.execute_reply":"2025-10-27T18:55:21.659831Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = train[feature_cols]\ny = train['sales']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:55:22.352668Z","iopub.execute_input":"2025-10-27T18:55:22.353019Z","iopub.status.idle":"2025-10-27T18:55:22.872297Z","shell.execute_reply.started":"2025-10-27T18:55:22.352995Z","shell.execute_reply":"2025-10-27T18:55:22.871450Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:55:40.367926Z","iopub.execute_input":"2025-10-27T18:55:40.368679Z","iopub.status.idle":"2025-10-27T18:55:40.373982Z","shell.execute_reply.started":"2025-10-27T18:55:40.368658Z","shell.execute_reply":"2025-10-27T18:55:40.372942Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:55:48.446474Z","iopub.execute_input":"2025-10-27T18:55:48.446765Z","iopub.status.idle":"2025-10-27T18:55:48.452936Z","shell.execute_reply.started":"2025-10-27T18:55:48.446746Z","shell.execute_reply":"2025-10-27T18:55:48.451929Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Zaman serisinde rastgele split yapamayız, son günleri validation olarak ayırıyoruz\n# Son 15 günü validation için ayır\ntrain_dates = train['date'].unique()\ntrain_dates = sorted(train_dates)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:56:08.416403Z","iopub.execute_input":"2025-10-27T18:56:08.416692Z","iopub.status.idle":"2025-10-27T18:56:08.453150Z","shell.execute_reply.started":"2025-10-27T18:56:08.416672Z","shell.execute_reply":"2025-10-27T18:56:08.452141Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"split_date = train_dates[-16]  # Son 15 gün validation\n\ntrain_mask = train['date'] < split_date\nval_mask = train['date'] >= split_date","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:56:20.890716Z","iopub.execute_input":"2025-10-27T18:56:20.891546Z","iopub.status.idle":"2025-10-27T18:56:20.919410Z","shell.execute_reply.started":"2025-10-27T18:56:20.891515Z","shell.execute_reply":"2025-10-27T18:56:20.918671Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train = X[train_mask]\ny_train = y[train_mask]\nX_val = X[val_mask]\ny_val = y[val_mask]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:56:31.161073Z","iopub.execute_input":"2025-10-27T18:56:31.161365Z","iopub.status.idle":"2025-10-27T18:56:32.004649Z","shell.execute_reply.started":"2025-10-27T18:56:31.161346Z","shell.execute_reply":"2025-10-27T18:56:32.003669Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"params = {\n    'objective': 'regression',\n    'metric': 'rmse',\n    'boosting_type': 'gbdt',\n    'num_leaves': 31,\n    'learning_rate': 0.05,\n    'feature_fraction': 0.9,\n    'bagging_fraction': 0.8,\n    'bagging_freq': 5,\n    'max_depth': -1,\n    'min_child_samples': 20,\n    'reg_alpha': 0.1,\n    'reg_lambda': 0.1,\n    'verbose': -1,\n    'n_jobs': -1\n}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:59:03.000915Z","iopub.execute_input":"2025-10-27T18:59:03.001238Z","iopub.status.idle":"2025-10-27T18:59:03.006501Z","shell.execute_reply.started":"2025-10-27T18:59:03.001214Z","shell.execute_reply":"2025-10-27T18:59:03.005586Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#  LIGHTGBM DATASET OLUŞTURMA\nlgb_train = lgb.Dataset(X_train, y_train)\nlgb_val = lgb.Dataset(X_val, y_val, reference=lgb_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:59:21.562804Z","iopub.execute_input":"2025-10-27T18:59:21.563179Z","iopub.status.idle":"2025-10-27T18:59:21.567735Z","shell.execute_reply.started":"2025-10-27T18:59:21.563157Z","shell.execute_reply":"2025-10-27T18:59:21.566597Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = lgb.train(\n    params,\n    lgb_train,\n    num_boost_round=2000,\n    valid_sets=[lgb_train, lgb_val],\n    valid_names=['train', 'valid'],\n    callbacks=[\n        lgb.early_stopping(stopping_rounds=50, verbose=True),\n        lgb.log_evaluation(period=100)\n    ]\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T18:59:33.238429Z","iopub.execute_input":"2025-10-27T18:59:33.238701Z","iopub.status.idle":"2025-10-27T19:01:41.902519Z","shell.execute_reply.started":"2025-10-27T18:59:33.238683Z","shell.execute_reply":"2025-10-27T19:01:41.901269Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Validation tahminleri\ny_val_pred = model.predict(X_val, num_iteration=model.best_iteration)\n\n# Negatif tahminleri 0 yap\ny_val_pred = np.maximum(y_val_pred, 0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T19:02:51.671220Z","iopub.execute_input":"2025-10-27T19:02:51.671530Z","iopub.status.idle":"2025-10-27T19:02:52.376165Z","shell.execute_reply.started":"2025-10-27T19:02:51.671510Z","shell.execute_reply":"2025-10-27T19:02:52.375433Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import mean_squared_error, mean_absolute_error","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T19:02:53.720443Z","iopub.execute_input":"2025-10-27T19:02:53.720712Z","iopub.status.idle":"2025-10-27T19:02:53.725561Z","shell.execute_reply.started":"2025-10-27T19:02:53.720695Z","shell.execute_reply":"2025-10-27T19:02:53.724451Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Metrikler\nrmse = np.sqrt(mean_squared_error(y_val, y_val_pred))\nmae = mean_absolute_error(y_val, y_val_pred)\nmape = np.mean(np.abs((y_val - y_val_pred) / (y_val + 1))) * 100","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T19:02:55.435058Z","iopub.execute_input":"2025-10-27T19:02:55.435337Z","iopub.status.idle":"2025-10-27T19:02:55.456721Z","shell.execute_reply.started":"2025-10-27T19:02:55.435320Z","shell.execute_reply":"2025-10-27T19:02:55.455734Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Feature importance\nfeature_importance = pd.DataFrame({\n    'feature': feature_cols,\n    'importance': model.feature_importance(importance_type='gain')\n})\nfeature_importance = feature_importance.sort_values('importance', ascending=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T19:02:59.332375Z","iopub.execute_input":"2025-10-27T19:02:59.332662Z","iopub.status.idle":"2025-10-27T19:02:59.338855Z","shell.execute_reply.started":"2025-10-27T19:02:59.332642Z","shell.execute_reply":"2025-10-27T19:02:59.337972Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Feature importance grafiği\nplt.figure(figsize=(10, 12))\ntop_features = feature_importance.head(30)\nplt.barh(range(len(top_features)), top_features['importance'])\nplt.yticks(range(len(top_features)), top_features['feature'])\nplt.xlabel('Importance (Gain)')\nplt.title('Top 30 Özellik Önemliliği', fontsize=14, fontweight='bold')\nplt.gca().invert_yaxis()\nplt.tight_layout()\nplt.savefig('feature_importance.png', dpi=300, bbox_inches='tight')\nprint(\"\\n✓ feature_importance.png kaydedildi\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T19:03:00.223236Z","iopub.execute_input":"2025-10-27T19:03:00.223778Z","iopub.status.idle":"2025-10-27T19:03:01.511643Z","shell.execute_reply.started":"2025-10-27T19:03:00.223753Z","shell.execute_reply":"2025-10-27T19:03:01.510656Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Rastgele bir örnek seç (belirli bir mağaza ve ürün)\nsample_store = train['store_nbr'].mode()[0]\nsample_family_col = [col for col in train.columns if 'family_encoded' in col][0]\nsample_family = train[sample_family_col].mode()[0]\n\nsample_mask = (train['store_nbr'] == sample_store) & (train[sample_family_col] == sample_family)\nsample_data = train[sample_mask].copy()\nsample_data = sample_data.sort_values('date')\n\n# Son 60 günü al\nsample_data = sample_data.tail(60)\n\n# Bu sample için tahmin yap\nsample_X = sample_data[feature_cols]\nsample_y_true = sample_data['sales'].values\nsample_y_pred = model.predict(sample_X, num_iteration=model.best_iteration)\nsample_y_pred = np.maximum(sample_y_pred, 0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T19:03:09.051096Z","iopub.execute_input":"2025-10-27T19:03:09.051405Z","iopub.status.idle":"2025-10-27T19:03:09.111228Z","shell.execute_reply.started":"2025-10-27T19:03:09.051382Z","shell.execute_reply":"2025-10-27T19:03:09.110586Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(15, 6))\nplt.plot(sample_data['date'], sample_y_true, label='Gerçek Satış', marker='o', linewidth=2)\nplt.plot(sample_data['date'], sample_y_pred, label='Tahmin', marker='s', linewidth=2, alpha=0.7)\nplt.xlabel('Tarih')\nplt.ylabel('Satış')\nplt.title(f'Gerçek vs Tahmin (Mağaza {sample_store}, Son 60 Gün)', fontsize=14, fontweight='bold')\nplt.legend()\nplt.grid(True, alpha=0.3)\nplt.xticks(rotation=45)\nplt.tight_layout()\nplt.savefig('prediction_vs_actual.png', dpi=300, bbox_inches='tight')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-27T19:11:23.302082Z","iopub.execute_input":"2025-10-27T19:11:23.302381Z","iopub.status.idle":"2025-10-27T19:11:24.481550Z","shell.execute_reply.started":"2025-10-27T19:11:23.302362Z","shell.execute_reply":"2025-10-27T19:11:24.480706Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}