{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","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"},{"sourceId":9977368,"sourceType":"datasetVersion","datasetId":6139035}],"dockerImageVersionId":30786,"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":"2024-12-22T14:54:38.690004Z","iopub.execute_input":"2024-12-22T14:54:38.690377Z","iopub.status.idle":"2024-12-22T14:54:39.114734Z","shell.execute_reply.started":"2024-12-22T14:54:38.690340Z","shell.execute_reply":"2024-12-22T14:54:39.113141Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install py7zr","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T14:54:41.471177Z","iopub.execute_input":"2024-12-22T14:54:41.471674Z","iopub.status.idle":"2024-12-22T14:54:56.597783Z","shell.execute_reply.started":"2024-12-22T14:54:41.471641Z","shell.execute_reply":"2024-12-22T14:54:56.596308Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n# from scipy.stats import itemfreq\nimport seaborn as sns\nfrom sklearn.model_selection import train_test_split\n%matplotlib inline\n\nimport datetime\nfrom datetime import date, timedelta","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T14:54:56.600392Z","iopub.execute_input":"2024-12-22T14:54:56.600897Z","iopub.status.idle":"2024-12-22T14:54:57.697991Z","shell.execute_reply.started":"2024-12-22T14:54:56.600859Z","shell.execute_reply":"2024-12-22T14:54:57.696874Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import py7zr\nimport pandas as pd\nimport numpy as np\nimport os\n\n# Define the extraction function\ndef extract_7z(file_path, extract_to='.'):\n    with py7zr.SevenZipFile(file_path, mode='r') as z:\n        z.extractall(path=extract_to)\n\n# Define file paths\nbase_path = '/kaggle/input/favorita-grocery-sales-forecasting/'\nfiles = {\n    'stores': 'stores.csv.7z',\n    'train': 'train.csv.7z',\n    'transactions': 'transactions.csv.7z',\n    'items': 'items.csv.7z',\n    'test': 'test.csv.7z',\n    'oil': 'oil.csv.7z',\n    'holidays': 'holidays_events.csv.7z',\n    'sample_submission': 'sample_submission.csv.7z'\n}\n\n# Extract and load each file\ndataframes = {}\nfor key, file in files.items():\n    extract_7z(os.path.join(base_path, file))  # Extract each .7z file\n    csv_file = file.replace('.7z', '')  # Get the .csv filename\n    dataframes[key] = pd.read_csv(csv_file)  # Load other files normally\n\n# Now each DataFrame is loaded and accessible via dataframes dictionary\ntrain = dataframes['train']\nstores = dataframes['stores']\ntransactions = dataframes['transactions']\ntest = dataframes['test']\nitems = dataframes['items']\noil = dataframes['oil']\nholidays_events = dataframes['holidays']\nsample_submission = dataframes['sample_submission']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T14:55:02.362005Z","iopub.execute_input":"2024-12-22T14:55:02.362595Z","iopub.status.idle":"2024-12-22T14:58:35.365545Z","shell.execute_reply.started":"2024-12-22T14:55:02.362559Z","shell.execute_reply":"2024-12-22T14:58:35.363589Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train[\"date\"] = pd.to_datetime(train[\"date\"])\n\n# Group the sales by date and calculate the total sales\ndaily_transactions = train.groupby(\"date\")[\"unit_sales\"].sum()\n\n# Create a line chart of the total sales by date\nplt.plot(daily_transactions.index, daily_transactions.values)\nplt.title(\"Total Sales by Date\")\nplt.xlabel(\"Date\")\nplt.ylabel(\"Total Sales\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T15:04:47.554691Z","iopub.execute_input":"2024-12-22T15:04:47.555343Z","iopub.status.idle":"2024-12-22T15:05:05.813259Z","shell.execute_reply.started":"2024-12-22T15:04:47.555305Z","shell.execute_reply":"2024-12-22T15:05:05.811833Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"As can be seen from above plot that total sales is showing seasonal behaviour, so SARIMA might be useful in this case.","metadata":{}},{"cell_type":"code","source":"# Count the number of stores in each city\ncity_counts = stores[\"city\"].value_counts()\n\n# Create a pie chart of the counts\nplt.pie(city_counts.values, labels=city_counts.index, autopct=\"%1.1f%%\")\nplt.title(\"Distribution of Stores by City\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T15:05:05.816075Z","iopub.execute_input":"2024-12-22T15:05:05.816650Z","iopub.status.idle":"2024-12-22T15:05:06.139296Z","shell.execute_reply.started":"2024-12-22T15:05:05.816597Z","shell.execute_reply":"2024-12-22T15:05:06.137952Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(oil['date'], oil['dcoilwtico'])\nplt.title('Oil Prices Over Time')\nplt.xlabel('Date')\nplt.ylabel('Oil Price (USD/bbl)')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T15:05:06.141824Z","iopub.execute_input":"2024-12-22T15:05:06.142767Z","iopub.status.idle":"2024-12-22T15:05:13.060233Z","shell.execute_reply.started":"2024-12-22T15:05:06.142679Z","shell.execute_reply":"2024-12-22T15:05:13.059064Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#train_store = pd.read_csv('/kaggle/input/individual-store/store_1_data.csv')\n\ntrain_store = train[train['store_nbr']==7]\n\n# 1. Filter Data for a Single Store (Example store_id = 1)\ntrain_store['date'] = pd.to_datetime(train_store['date'])\n\n# 2. Merge Store Location Data\ntrain_store = pd.merge(train_store, stores, on='store_nbr', how='left')\n\n# 5. Merge Holidays and Events Data\nholidays_events['date'] = pd.to_datetime(holidays_events['date'])\nholidays_events = holidays_events.rename(columns={'locale_name': 'city'})\ntrain_store = pd.merge(train_store, holidays_events[['date', 'city', 'type', 'locale']], on=['date', 'city'], how='left')\n\n# 3. Merge Item Categories (use item_nbr to map to item category)\ntrain_store = pd.merge(train_store, items[['item_nbr', 'family']], on='item_nbr', how='left')\n\n# 4. Merge Oil Prices Data\noil['date'] = pd.to_datetime(oil['date'])  # Convert to datetime\ntrain_store['date'] = pd.to_datetime(train_store['date'])  # Ensure date format consistency\ntrain_store = pd.merge(train_store, oil[['date', 'dcoilwtico']], on='date', how='left')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T15:06:24.141492Z","iopub.execute_input":"2024-12-22T15:06:24.141976Z","iopub.status.idle":"2024-12-22T15:06:28.896890Z","shell.execute_reply.started":"2024-12-22T15:06:24.141939Z","shell.execute_reply":"2024-12-22T15:06:28.895571Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Group by item_nbr and calculate total revenue\nitem_sales = train_store.groupby('item_nbr')['unit_sales'].sum().reset_index()\n\n# Sort by revenue in descending order\nitem_sales = item_sales.sort_values(by='unit_sales', ascending=False)\n\n# Find the item_nbr with the highest sales\ntop_item = item_sales.iloc[0]\nprint(f\"Item with the highest sales: {top_item['item_nbr']} with sales: {top_item['unit_sales']}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T15:06:28.898768Z","iopub.execute_input":"2024-12-22T15:06:28.899157Z","iopub.status.idle":"2024-12-22T15:06:28.990351Z","shell.execute_reply.started":"2024-12-22T15:06:28.899125Z","shell.execute_reply":"2024-12-22T15:06:28.988729Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Filter data for a single store and item\ntrain_store = train_store[train_store['item_nbr'] == 1503844]\n\n# Handle missing values\ntrain_store['dcoilwtico'].fillna(method='ffill', inplace=True)\n\nmost_frequent_value = train_store['onpromotion'].mode()[0]  # [0] to extract the single most frequent value\ntrain_store['onpromotion'].fillna(most_frequent_value, inplace=True)\nmost_frequent_value = train_store['type_y'].mode()[0]  # [0] to extract the single most frequent value\ntrain_store['type_y'].fillna('No', inplace=True)\n\n# Encode holiday type as a numeric variable\ntrain_store['holiday_type'] = train_store['type_y'].astype('category').cat.codes\ntrain_store['onpromotion'] = train_store['onpromotion'].astype('category').cat.codes","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T15:06:29.273420Z","iopub.execute_input":"2024-12-22T15:06:29.273888Z","iopub.status.idle":"2024-12-22T15:06:29.365297Z","shell.execute_reply.started":"2024-12-22T15:06:29.273854Z","shell.execute_reply":"2024-12-22T15:06:29.363848Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Feature engineering\ntrain_store['day_of_week'] = train_store['date'].dt.dayofweek\ntrain_store['month'] = train_store['date'].dt.month\ntrain_store['year'] = train_store['date'].dt.year\n\n# Lag features\nfor lag in [7, 14, 28]:\n    train_store[f'lag_{lag}'] = train_store['unit_sales'].shift(lag)\n\n# Rolling statistics\ntrain_store['rolling_mean_7'] = train_store['unit_sales'].rolling(window=7).mean()\ntrain_store['rolling_std_7'] = train_store['unit_sales'].rolling(window=7).std()\n\n# Fill remaining NaN values\ntrain_store.fillna(0, inplace=True)\n\ntrain_store = train_store.select_dtypes(exclude=['object'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T15:06:33.180703Z","iopub.execute_input":"2024-12-22T15:06:33.181184Z","iopub.status.idle":"2024-12-22T15:06:33.202203Z","shell.execute_reply.started":"2024-12-22T15:06:33.181149Z","shell.execute_reply":"2024-12-22T15:06:33.200965Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# sales_by_day = train_store.groupby('type_y')['unit_sales'].sum()\n# print(sales_by_day)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-21T13:42:18.135905Z","iopub.execute_input":"2024-12-21T13:42:18.137350Z","iopub.status.idle":"2024-12-21T13:42:18.153414Z","shell.execute_reply.started":"2024-12-21T13:42:18.137298Z","shell.execute_reply":"2024-12-21T13:42:18.151771Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# sales_by_day = train_store.groupby('type_y')['unit_sales'].mean()\n# print(sales_by_day)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-21T13:44:30.891268Z","iopub.execute_input":"2024-12-21T13:44:30.892103Z","iopub.status.idle":"2024-12-21T13:44:30.910008Z","shell.execute_reply.started":"2024-12-21T13:44:30.892032Z","shell.execute_reply":"2024-12-21T13:44:30.907250Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# plt.figure(figsize=(10, 6))\n\n# # Plot sales by day of the week\n# sales_by_day.plot(kind='bar', color='skyblue', edgecolor='black')\n\n# # Add titles and labels\n# plt.title('Sales by Day of the Week', fontsize=16)\n# plt.xlabel('Day of the Week', fontsize=12)\n# plt.ylabel('Total Sales', fontsize=12)\n\n# # Display the plot\n# plt.xticks(rotation=45)\n# plt.tight_layout()\n# plt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-21T11:48:38.089092Z","iopub.execute_input":"2024-12-21T11:48:38.089596Z","iopub.status.idle":"2024-12-21T11:48:38.339563Z","shell.execute_reply.started":"2024-12-21T11:48:38.089560Z","shell.execute_reply":"2024-12-21T11:48:38.338218Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"correlation_matrix = train_store[['unit_sales', 'onpromotion','dcoilwtico', 'holiday_type', 'day_of_week', 'month', 'year',\n       'lag_7', 'lag_14', 'lag_28', 'rolling_mean_7', 'rolling_std_7']].corr()\n\n# Plot heatmap\nplt.figure(figsize=(8, 6))\nsns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', fmt='.2f', linewidths=0.5)\n\n# Add a title\nplt.title('Heatmap of Correlation Matrix', fontsize=16)\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T15:06:37.639690Z","iopub.execute_input":"2024-12-22T15:06:37.640134Z","iopub.status.idle":"2024-12-22T15:06:38.538687Z","shell.execute_reply.started":"2024-12-22T15:06:37.640101Z","shell.execute_reply":"2024-12-22T15:06:38.534645Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import lightgbm as lgb\nfrom sklearn.metrics import mean_squared_error, r2_score\n\n#train_store = train_store[train_store['item_nbr'] == 582864]\ntest_size = 30\n#train_store = train_store[['unit_sales','date', 'lag_7', 'lag_14', 'lag_28', 'rolling_mean_7', 'rolling_std_7']]\ntrain_data = train_store[:-test_size]  # All rows except the last 30\ntest_data = train_store[-test_size:]   # Last 30 rows\n\n# Separate features (X) and target (y)\nX_train = train_data.drop(['date', 'unit_sales'], axis=1)\ny_train = train_data['unit_sales']\nX_test = test_data.drop(['date', 'unit_sales'], axis=1)\ny_test = test_data['unit_sales']\n\n# LightGBM model\ntrain_dataset = lgb.Dataset(X_train, label=y_train)\ntest_dataset = lgb.Dataset(X_test, label=y_test, reference=train_data)\n\n# Model parameters\nparams = {\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}\n\n# Train the model with early stopping\nmodel = lgb.train(params,train_dataset,num_boost_round=1000,)\n\n# Predictions\ny_pred = model.predict(X_test)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T15:06:59.377046Z","iopub.execute_input":"2024-12-22T15:06:59.377471Z","iopub.status.idle":"2024-12-22T15:07:09.615869Z","shell.execute_reply.started":"2024-12-22T15:06:59.377437Z","shell.execute_reply":"2024-12-22T15:07:09.614446Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom sklearn.metrics import mean_squared_error, r2_score\nfrom datetime import timedelta\n\n# Evaluate the model\nrmse = mean_squared_error(y_test, y_pred, squared=False)\nr2 = r2_score(y_test, y_pred)\n\nprint(f\"RMSE: {rmse}\")\nprint(f\"R² Score: {r2}\")\n\n# Plot actual vs predicted for the last 30 days (test dataset)\n#test_data['Predicted'] = y_pred  # Add predicted values to test_data\n\n# Actual vs Predicted Line Plot\n# plt.subplot(1, 2, 1)  # Create subplot for line plot\nplt.figure(figsize=(12, 4))\nplt.plot(test_data['date'], y_test, label='Actual Sales', marker='o', linestyle='-', color='blue')\nplt.plot(test_data['date'], y_pred, label='Predicted Sales', marker='o', linestyle='--', color='orange')\nplt.legend()\nplt.title(\"Actual vs Predicted Sales \")\nplt.xlabel(\"Date\")\nplt.ylabel(\"Sales\")\nplt.grid()\nplt.show()\n\n\n# Scatter Plot of Actual vs Predicted Sales\n# plt.subplot(1, 2, 2)  # Create subplot for scatter plot\nplt.figure(figsize=(5, 5))\nplt.scatter(y_test, y_pred, alpha=0.5, color='blue')\nplt.plot([min(y_test), max(y_test)], [min(y_test), max(y_test)], color='red', linestyle='--')  # Line for perfect prediction\nplt.title(f\"Scatter Plot of Actual vs Predicted Sales\")\nplt.xlabel(\"Actual Sales\")\nplt.ylabel(\"Predicted Sales\")\nplt.grid()\n\n# Show the plot\n# plt.tight_layout()\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T15:07:09.618778Z","iopub.execute_input":"2024-12-22T15:07:09.619792Z","iopub.status.idle":"2024-12-22T15:07:10.194204Z","shell.execute_reply.started":"2024-12-22T15:07:09.619735Z","shell.execute_reply":"2024-12-22T15:07:10.192703Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Get feature importances\nimportances = model.feature_importance(importance_type='split')  # or 'gain'\n\n# Get feature names\nfeature_names = X_train.columns\n\n# Create a DataFrame for better visualization\nimportance_df = pd.DataFrame({\n    'Feature': feature_names,\n    'Importance': importances\n})\n\n# Sort by importance\nimportance_df = importance_df.sort_values(by='Importance', ascending=False)\n\n# Display top 10 most important features\nprint(importance_df.head(15))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T15:07:15.226286Z","iopub.execute_input":"2024-12-22T15:07:15.226825Z","iopub.status.idle":"2024-12-22T15:07:15.244020Z","shell.execute_reply.started":"2024-12-22T15:07:15.226783Z","shell.execute_reply":"2024-12-22T15:07:15.242568Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# # Import libraries\n# import pandas as pd\n# import numpy as np\n# from statsmodels.tsa.statespace.sarimax import SARIMAX\n# from statsmodels.tsa.seasonal import seasonal_decompose\n# import matplotlib.pyplot as plt\n# from sklearn.metrics import mean_squared_error\n\n# # Load your data\n# # Assuming 'store_data' contains filtered data for a single store and item\n# store_data = train_store\n# store_data['date'] = pd.to_datetime(store_data['date'])\n# store_data = store_data.set_index('date')\n# store_data = store_data.sort_index()\n\n# # Check for missing dates and fill them\n# all_dates = pd.date_range(start=store_data.index.min(), end=store_data.index.max(), freq='D')\n# store_data = store_data.reindex(all_dates, fill_value=0)\n\n# # Decompose the time series (optional for visualization)\n# result = seasonal_decompose(store_data['unit_sales'], model='additive', period=7)\n# # result.plot()\n# # plt.show()\n\n# # Split into training and test sets\n# train_data = store_data.iloc[:-30]  # Use all but the last 30 days as training\n# test_data = store_data.iloc[-30:]   # Use the last 30 days as test\n\n# # Fit SARIMA model\n# # p, d, q are ARIMA parameters; P, D, Q, s are seasonal parameters\n# model = SARIMAX(train_data['unit_sales'], \n#                 order=(1, 1, 1),  # ARIMA parameters\n#                 seasonal_order=(1, 1, 1, 7),  # Seasonal parameters (7 days for weekly seasonality)\n#                 enforce_stationarity=False, \n#                 enforce_invertibility=False)\n# sarima_model = model.fit()\n\n# # Print model summary\n# # print(sarima_model.summary())\n\n# # Forecast\n# forecast = sarima_model.get_forecast(steps=30)\n# forecast_index = test_data.index\n# forecast_values = forecast.predicted_mean\n\n# # # Plot actual vs predicted\n# # plt.figure(figsize=(12, 6))\n# # plt.plot(train_data['unit_sales'], label='Train')\n# # plt.plot(test_data['unit_sales'], label='Actual')\n# # plt.plot(forecast_index, forecast_values, label='Forecast', color='orange')\n# # plt.legend()\n# # plt.title(\"SARIMA Forecast vs Actual Sales\")\n# # plt.show()\n\n# # # Evaluate the model\n# # rmse = np.sqrt(mean_squared_error(test_data['unit_sales'], forecast_values))\n# # # print(f\"Root Mean Squared Error (RMSE): {rmse}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T11:27:52.450491Z","iopub.execute_input":"2024-12-19T11:27:52.451254Z","iopub.status.idle":"2024-12-19T11:27:52.459449Z","shell.execute_reply.started":"2024-12-19T11:27:52.451204Z","shell.execute_reply":"2024-12-19T11:27:52.458110Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}