{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":31254,"databundleVersionId":3103714,"sourceType":"competition"}],"dockerImageVersionId":30747,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-09-03T04:53:20.103179Z","iopub.execute_input":"2024-09-03T04:53:20.103559Z","iopub.status.idle":"2024-09-03T04:53:20.108137Z","shell.execute_reply.started":"2024-09-03T04:53:20.103529Z","shell.execute_reply":"2024-09-03T04:53:20.107081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles_df = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/articles.csv')\ncustomers_df = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/customers.csv')\ntransactions_df = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/transactions_train.csv')","metadata":{"execution":{"iopub.status.busy":"2024-09-03T04:53:20.110011Z","iopub.execute_input":"2024-09-03T04:53:20.110319Z","iopub.status.idle":"2024-09-03T04:53:22.074930Z","shell.execute_reply.started":"2024-09-03T04:53:20.110285Z","shell.execute_reply":"2024-09-03T04:53:22.071293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_df.t_dat = pd.to_datetime(transactions_df.t_dat)","metadata":{"execution":{"iopub.status.busy":"2024-09-03T04:53:22.075597Z","iopub.status.idle":"2024-09-03T04:53:22.075902Z","shell.execute_reply.started":"2024-09-03T04:53:22.075748Z","shell.execute_reply":"2024-09-03T04:53:22.075761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SalesPerMonth = transactions_df.copy()\nSalesPerMonth","metadata":{"execution":{"iopub.status.busy":"2024-09-03T04:53:22.077907Z","iopub.status.idle":"2024-09-03T04:53:22.078434Z","shell.execute_reply.started":"2024-09-03T04:53:22.078153Z","shell.execute_reply":"2024-09-03T04:53:22.078174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SalesPerMonth['t_dat'] = SalesPerMonth['t_dat'].dt.to_period('M')\n\nSalesPerMonth['t_dat'] = SalesPerMonth['t_dat'].dt.to_timestamp()","metadata":{"execution":{"iopub.status.busy":"2024-09-03T04:53:22.079836Z","iopub.status.idle":"2024-09-03T04:53:22.080279Z","shell.execute_reply.started":"2024-09-03T04:53:22.080038Z","shell.execute_reply":"2024-09-03T04:53:22.080055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nSalesPerMonthGroup = SalesPerMonth.groupby('t_dat')['price'].sum().reset_index()","metadata":{"execution":{"iopub.status.busy":"2024-09-03T04:53:22.082481Z","iopub.status.idle":"2024-09-03T04:53:22.083051Z","shell.execute_reply.started":"2024-09-03T04:53:22.082799Z","shell.execute_reply":"2024-09-03T04:53:22.082834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize= (36,10))\nplt.title(\"SalesPerMonth\")\nplt.xlabel(\"Time\")\nplt.ylabel(\"Sales Price\")\nplt.plot(SalesPerMonthGroup['t_dat'], SalesPerMonthGroup['price'], color = 'blue',marker= 'o', linestyle= '-')\nplt.grid(True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-09-03T04:53:22.084484Z","iopub.status.idle":"2024-09-03T04:53:22.085177Z","shell.execute_reply.started":"2024-09-03T04:53:22.084927Z","shell.execute_reply":"2024-09-03T04:53:22.084947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dailySales = transactions_df.groupby('t_dat')['price'].sum().reset_index()","metadata":{"execution":{"iopub.status.busy":"2024-09-03T04:53:22.086489Z","iopub.status.idle":"2024-09-03T04:53:22.086930Z","shell.execute_reply.started":"2024-09-03T04:53:22.086703Z","shell.execute_reply":"2024-09-03T04:53:22.086721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(36,10))\nplt.title(\"Sales Daily\")\nplt.plot(dailySales['t_dat'], dailySales['price'], color = 'red', marker= 'o', linestyle= '-')\nplt.xlabel(\"Time\")\nplt.ylabel(\"Sale Price\")\nplt.grid(True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-09-03T04:53:22.088432Z","iopub.status.idle":"2024-09-03T04:53:22.088850Z","shell.execute_reply.started":"2024-09-03T04:53:22.088636Z","shell.execute_reply":"2024-09-03T04:53:22.088653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dailySales.loc[dailySales.price == dailySales.price.max(), 't_dat']","metadata":{"execution":{"iopub.status.busy":"2024-09-03T04:53:22.090058Z","iopub.status.idle":"2024-09-03T04:53:22.090531Z","shell.execute_reply.started":"2024-09-03T04:53:22.090298Z","shell.execute_reply":"2024-09-03T04:53:22.090316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mostSalesDaily = transactions_df.loc[transactions_df.t_dat == \"2019-09-28\"]\nmostSalesDaily","metadata":{"execution":{"iopub.status.busy":"2024-09-03T04:53:22.092261Z","iopub.status.idle":"2024-09-03T04:53:22.092611Z","shell.execute_reply.started":"2024-09-03T04:53:22.092454Z","shell.execute_reply":"2024-09-03T04:53:22.092468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(mostSalesDaily.customer_id.unique()).size","metadata":{"execution":{"iopub.status.busy":"2024-09-03T04:53:22.094284Z","iopub.status.idle":"2024-09-03T04:53:22.094617Z","shell.execute_reply.started":"2024-09-03T04:53:22.094458Z","shell.execute_reply":"2024-09-03T04:53:22.094472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mostSalesDaily.article_id.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-09-03T04:53:22.096457Z","iopub.status.idle":"2024-09-03T04:53:22.096813Z","shell.execute_reply.started":"2024-09-03T04:53:22.096643Z","shell.execute_reply":"2024-09-03T04:53:22.096657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"top10 = mostSalesDaily.article_id.value_counts().head(10).index\ntop10","metadata":{"execution":{"iopub.status.busy":"2024-09-03T04:53:22.097802Z","iopub.status.idle":"2024-09-03T04:53:22.098102Z","shell.execute_reply.started":"2024-09-03T04:53:22.097946Z","shell.execute_reply":"2024-09-03T04:53:22.097958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles_df.loc[articles_df.article_id.isin(top10)]","metadata":{"execution":{"iopub.status.busy":"2024-09-03T04:53:22.099264Z","iopub.status.idle":"2024-09-03T04:53:22.099569Z","shell.execute_reply.started":"2024-09-03T04:53:22.099418Z","shell.execute_reply":"2024-09-03T04:53:22.099430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dailySalesItem = transactions_df.groupby('t_dat').size()","metadata":{"execution":{"iopub.status.busy":"2024-09-03T04:53:22.100502Z","iopub.status.idle":"2024-09-03T04:53:22.100796Z","shell.execute_reply.started":"2024-09-03T04:53:22.100650Z","shell.execute_reply":"2024-09-03T04:53:22.100662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dailySalesItem = dailySalesItem.reset_index(name='items')","metadata":{"execution":{"iopub.status.busy":"2024-09-03T04:53:22.102105Z","iopub.status.idle":"2024-09-03T04:53:22.102449Z","shell.execute_reply.started":"2024-09-03T04:53:22.102285Z","shell.execute_reply":"2024-09-03T04:53:22.102298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dailySalesItem","metadata":{"execution":{"iopub.status.busy":"2024-09-03T04:53:22.103848Z","iopub.status.idle":"2024-09-03T04:53:22.104150Z","shell.execute_reply.started":"2024-09-03T04:53:22.104000Z","shell.execute_reply":"2024-09-03T04:53:22.104012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(36, 10))\nplt.title(\"Sales Daily\")\nplt.plot(dailySalesItem['t_dat'], dailySalesItem['items'], color='red', marker='o', linestyle='-')\nplt.xlabel(\"Time\")\nplt.ylabel(\"Sale items\")\nplt.grid(True)\nplt.show()\nplt.figure(figsize=(36,10))\nplt.title(\"Sales Daily\")\nplt.plot(dailySales['t_dat'], dailySales['price'], color = 'blue', marker= 'o', linestyle= '-')\nplt.xlabel(\"Time\")\nplt.ylabel(\"Sale Price\")\nplt.grid(True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-09-03T04:53:22.105060Z","iopub.status.idle":"2024-09-03T04:53:22.105401Z","shell.execute_reply.started":"2024-09-03T04:53:22.105206Z","shell.execute_reply":"2024-09-03T04:53:22.105238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2024-09-03T04:53:22.107382Z","iopub.status.idle":"2024-09-03T04:53:22.107689Z","shell.execute_reply.started":"2024-09-03T04:53:22.107539Z","shell.execute_reply":"2024-09-03T04:53:22.107552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"salesdailyreg = dailySales.merge(dailySalesItem[['t_dat','items']], on='t_dat', how='left')","metadata":{"execution":{"iopub.status.busy":"2024-09-03T04:53:22.108451Z","iopub.status.idle":"2024-09-03T04:53:22.108746Z","shell.execute_reply.started":"2024-09-03T04:53:22.108597Z","shell.execute_reply":"2024-09-03T04:53:22.108609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"salesdailyreg","metadata":{"execution":{"iopub.status.busy":"2024-09-03T04:53:22.110430Z","iopub.status.idle":"2024-09-03T04:53:22.110746Z","shell.execute_reply.started":"2024-09-03T04:53:22.110583Z","shell.execute_reply":"2024-09-03T04:53:22.110595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"salesdailyreg","metadata":{"execution":{"iopub.status.busy":"2024-09-03T04:53:22.112454Z","iopub.status.idle":"2024-09-03T04:53:22.112905Z","shell.execute_reply.started":"2024-09-03T04:53:22.112660Z","shell.execute_reply":"2024-09-03T04:53:22.112678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set(style=\"whitegrid\")\n\n# Scatter plot with regression line\nplt.figure(figsize=(16, 8))\nsns.regplot(x='price', y='items', data=salesdailyreg)\n\nplt.title('Sales Revenue vs. Items Sold')\nplt.xlabel('Number of Items Sold')\nplt.ylabel('Sales Revenue')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-09-03T04:53:22.114036Z","iopub.status.idle":"2024-09-03T04:53:22.114488Z","shell.execute_reply.started":"2024-09-03T04:53:22.114266Z","shell.execute_reply":"2024-09-03T04:53:22.114285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install pmdarima\n","metadata":{"execution":{"iopub.status.busy":"2024-09-03T04:54:05.150832Z","iopub.execute_input":"2024-09-03T04:54:05.151531Z","iopub.status.idle":"2024-09-03T04:54:17.413421Z","shell.execute_reply.started":"2024-09-03T04:54:05.151502Z","shell.execute_reply":"2024-09-03T04:54:17.412268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom statsmodels.tsa.arima.model import ARIMA\nfrom pmdarima import auto_arima\nfrom datetime import timedelta","metadata":{"execution":{"iopub.status.busy":"2024-09-03T04:54:24.217123Z","iopub.execute_input":"2024-09-03T04:54:24.217878Z","iopub.status.idle":"2024-09-03T04:54:24.222431Z","shell.execute_reply.started":"2024-09-03T04:54:24.217849Z","shell.execute_reply":"2024-09-03T04:54:24.221531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load data\ntransactions = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/transactions_train.csv', parse_dates=['t_dat'])\narticles = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/articles.csv')\ncustomers = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/customers.csv')\nsample_submission = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2024-09-03T04:54:26.838473Z","iopub.execute_input":"2024-09-03T04:54:26.839171Z","iopub.status.idle":"2024-09-03T04:55:19.823092Z","shell.execute_reply.started":"2024-09-03T04:54:26.839141Z","shell.execute_reply":"2024-09-03T04:55:19.822281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Preprocessing\nsales = transactions.groupby(['article_id', 't_dat']).size().reset_index(name='sales')\nsales = sales.pivot_table(index='t_dat', columns='article_id', values='sales', fill_value=0)\nsales.index = pd.to_datetime(sales.index)\nsales = sales.asfreq('D')","metadata":{"execution":{"iopub.status.busy":"2024-09-03T04:56:10.293339Z","iopub.execute_input":"2024-09-03T04:56:10.293619Z","iopub.status.idle":"2024-09-03T04:56:21.559209Z","shell.execute_reply.started":"2024-09-03T04:56:10.293595Z","shell.execute_reply":"2024-09-03T04:56:21.558429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ARIMA Model fitting and forecasting\ndef fit_arima_forecast(series, forecast_days=7):\n    model = auto_arima(series, seasonal=False, stepwise=True)\n    model_fit = model.fit(series)\n    forecast = model.predict(n_periods=forecast_days)\n    return forecast\n\n# Prepare forecasts\ndef generate_forecasts(sales_data, forecast_days=7):\n    forecasts = {}\n    for article_id in sales_data.columns:\n        series = sales_data[article_id]\n        forecast = fit_arima_forecast(series, forecast_days=forecast_days)\n        forecasts[article_id] = forecast\n    return forecasts\n\n# Generate forecasts\nforecast_days = 7\nforecasts = generate_forecasts(sales, forecast_days=forecast_days)\n\n# Generate predictions for sample_submission\npredictions = []\nfor customer_id in sample_submission['customer_id'].unique():\n    for article_id in forecasts.keys():\n        forecast = forecasts[article_id]\n        for day in range(forecast_days):\n            prediction_date = sales.index.max() + timedelta(days=day)\n            predictions.append({\n                'customer_id': customer_id,\n                'article_id': article_id,\n                'date': prediction_date,\n                'forecast': forecast[day]\n            })\n\n# Convert to DataFrame\npredictions_df = pd.DataFrame(predictions)\n\n# Save predictions to CSV\npredictions_df.to_csv('final_predictions.csv', index=False)\n","metadata":{"execution":{"iopub.status.busy":"2024-09-03T04:53:22.117684Z","iopub.status.idle":"2024-09-03T04:53:22.118111Z","shell.execute_reply.started":"2024-09-03T04:53:22.117883Z","shell.execute_reply":"2024-09-03T04:53:22.117901Z"},"trusted":true},"execution_count":null,"outputs":[]}]}