{"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":31254,"databundleVersionId":3103714,"sourceType":"competition"}],"dockerImageVersionId":30804,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom wordcloud import WordCloud, STOPWORDS\nfrom datetime import datetime\nfrom PIL import Image","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:02:03.155889Z","iopub.execute_input":"2024-12-04T16:02:03.156826Z","iopub.status.idle":"2024-12-04T16:02:04.214295Z","shell.execute_reply.started":"2024-12-04T16:02:03.156777Z","shell.execute_reply":"2024-12-04T16:02:04.213022Z"}},"outputs":[],"execution_count":null},{"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\")\nsample_submission_df = pd.read_csv(\"/kaggle/input/h-and-m-personalized-fashion-recommendations/sample_submission.csv\")\ntransactions_train_df = pd.read_csv(\"/kaggle/input/h-and-m-personalized-fashion-recommendations/transactions_train.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:02:23.6631Z","iopub.execute_input":"2024-12-04T16:02:23.663632Z","iopub.status.idle":"2024-12-04T16:03:54.619477Z","shell.execute_reply.started":"2024-12-04T16:02:23.663594Z","shell.execute_reply":"2024-12-04T16:03:54.618267Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"articles_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:03:54.621353Z","iopub.execute_input":"2024-12-04T16:03:54.621691Z","iopub.status.idle":"2024-12-04T16:03:54.658587Z","shell.execute_reply.started":"2024-12-04T16:03:54.621657Z","shell.execute_reply":"2024-12-04T16:03:54.657546Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"customers_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:04:03.452876Z","iopub.execute_input":"2024-12-04T16:04:03.453503Z","iopub.status.idle":"2024-12-04T16:04:03.473155Z","shell.execute_reply.started":"2024-12-04T16:04:03.453456Z","shell.execute_reply":"2024-12-04T16:04:03.471768Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_submission_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:04:06.273444Z","iopub.execute_input":"2024-12-04T16:04:06.274673Z","iopub.status.idle":"2024-12-04T16:04:06.286555Z","shell.execute_reply.started":"2024-12-04T16:04:06.274613Z","shell.execute_reply":"2024-12-04T16:04:06.285031Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"transactions_train_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:04:08.403797Z","iopub.execute_input":"2024-12-04T16:04:08.404919Z","iopub.status.idle":"2024-12-04T16:04:08.416736Z","shell.execute_reply.started":"2024-12-04T16:04:08.404878Z","shell.execute_reply":"2024-12-04T16:04:08.415489Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def missing_data(data):\n    total = data.isnull().sum().sort_values(ascending = False)\n    percent = (data.isnull().sum()/data.isnull().count()*100).sort_values(ascending = False)\n    return pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:04:16.78617Z","iopub.execute_input":"2024-12-04T16:04:16.787561Z","iopub.status.idle":"2024-12-04T16:04:16.797378Z","shell.execute_reply.started":"2024-12-04T16:04:16.787492Z","shell.execute_reply":"2024-12-04T16:04:16.795782Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def unique_values(data):\n    total = data.count()\n    tt = pd.DataFrame(total)\n    tt.columns = ['Total']\n    uniques = []\n    for col in data.columns:\n        unique = data[col].nunique()\n        uniques.append(unique)\n    tt['Uniques'] = uniques\n    return tt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:04:21.957114Z","iopub.execute_input":"2024-12-04T16:04:21.957495Z","iopub.status.idle":"2024-12-04T16:04:21.964147Z","shell.execute_reply.started":"2024-12-04T16:04:21.957464Z","shell.execute_reply":"2024-12-04T16:04:21.962586Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"articles_df.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:04:29.20665Z","iopub.execute_input":"2024-12-04T16:04:29.207184Z","iopub.status.idle":"2024-12-04T16:04:29.356967Z","shell.execute_reply.started":"2024-12-04T16:04:29.20714Z","shell.execute_reply":"2024-12-04T16:04:29.354548Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"missing_data(articles_df)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:04:37.978095Z","iopub.execute_input":"2024-12-04T16:04:37.978493Z","iopub.status.idle":"2024-12-04T16:04:38.235398Z","shell.execute_reply.started":"2024-12-04T16:04:37.978456Z","shell.execute_reply":"2024-12-04T16:04:38.234106Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"customers_df.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:04:45.189283Z","iopub.execute_input":"2024-12-04T16:04:45.189707Z","iopub.status.idle":"2024-12-04T16:04:45.578787Z","shell.execute_reply.started":"2024-12-04T16:04:45.189667Z","shell.execute_reply":"2024-12-04T16:04:45.577688Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"missing_data(customers_df)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:04:56.075131Z","iopub.execute_input":"2024-12-04T16:04:56.075503Z","iopub.status.idle":"2024-12-04T16:04:56.99829Z","shell.execute_reply.started":"2024-12-04T16:04:56.07547Z","shell.execute_reply":"2024-12-04T16:04:56.997112Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"missing_data(transactions_train_df)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:05:20.170818Z","iopub.execute_input":"2024-12-04T16:05:20.171214Z","iopub.status.idle":"2024-12-04T16:05:30.260599Z","shell.execute_reply.started":"2024-12-04T16:05:20.171179Z","shell.execute_reply":"2024-12-04T16:05:30.25918Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"unique_values(articles_df)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:05:40.132392Z","iopub.execute_input":"2024-12-04T16:05:40.132789Z","iopub.status.idle":"2024-12-04T16:05:40.372484Z","shell.execute_reply.started":"2024-12-04T16:05:40.132752Z","shell.execute_reply":"2024-12-04T16:05:40.370895Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"unique_values(transactions_train_df)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:05:49.126184Z","iopub.execute_input":"2024-12-04T16:05:49.126583Z","iopub.status.idle":"2024-12-04T16:06:03.545181Z","shell.execute_reply.started":"2024-12-04T16:05:49.126548Z","shell.execute_reply":"2024-12-04T16:06:03.544114Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"temp = articles_df.groupby([\"product_group_name\"])[\"product_type_name\"].nunique()\ndf = pd.DataFrame({'Product Group': temp.index,\n                   'Product Types': temp.values\n                  })\ndf = df.sort_values(['Product Types'], ascending=False)\nplt.figure(figsize = (8,6))\nplt.title('Number of Product Types per each Product Group')\nsns.set_color_codes(\"pastel\")\ns = sns.barplot(x = 'Product Group', y=\"Product Types\", data=df)\ns.set_xticklabels(s.get_xticklabels(),rotation=90)\nlocs, labels = plt.xticks()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:06:07.147174Z","iopub.execute_input":"2024-12-04T16:06:07.147702Z","iopub.status.idle":"2024-12-04T16:06:07.59916Z","shell.execute_reply.started":"2024-12-04T16:06:07.14765Z","shell.execute_reply":"2024-12-04T16:06:07.597683Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"stopwords = set(STOPWORDS)\n\ndef show_wordcloud(data, title = None):\n    wordcloud = WordCloud(\n        background_color='white',\n        stopwords=stopwords,\n        max_words=200,\n        max_font_size=40, \n        scale=5,\n        random_state=1\n    ).generate(str(data))\n\n    fig = plt.figure(1, figsize=(10,10))\n    plt.axis('off')\n    if title: \n        fig.suptitle(title, fontsize=14)\n        fig.subplots_adjust(top=2.3)\n\n    plt.imshow(wordcloud)\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:06:25.752463Z","iopub.execute_input":"2024-12-04T16:06:25.753027Z","iopub.status.idle":"2024-12-04T16:06:25.761034Z","shell.execute_reply.started":"2024-12-04T16:06:25.75295Z","shell.execute_reply":"2024-12-04T16:06:25.759536Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"show_wordcloud(articles_df[\"prod_name\"], \"Wordcloud from product name\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:06:31.245713Z","iopub.execute_input":"2024-12-04T16:06:31.246151Z","iopub.status.idle":"2024-12-04T16:06:31.663756Z","shell.execute_reply.started":"2024-12-04T16:06:31.246113Z","shell.execute_reply":"2024-12-04T16:06:31.662399Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"temp = articles_df.groupby([\"product_group_name\"])[\"article_id\"].nunique()\ndf = pd.DataFrame({'Product Group': temp.index,\n                   'Articles': temp.values\n                  })\ndf = df.sort_values(['Articles'], ascending=False)\nplt.figure(figsize = (8,6))\nplt.title('Number of Articles per each Product Group')\nsns.set_color_codes(\"pastel\")\ns = sns.barplot(x = 'Product Group', y=\"Articles\", data=df)\ns.set_xticklabels(s.get_xticklabels(),rotation=90)\nlocs, labels = plt.xticks()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:06:39.992673Z","iopub.execute_input":"2024-12-04T16:06:39.993068Z","iopub.status.idle":"2024-12-04T16:06:40.451128Z","shell.execute_reply.started":"2024-12-04T16:06:39.993033Z","shell.execute_reply":"2024-12-04T16:06:40.449861Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"temp = articles_df.groupby([\"product_type_name\"])[\"article_id\"].nunique()\ndf = pd.DataFrame({'Product Type': temp.index,\n                   'Articles': temp.values\n                  })\ntotal_types = len(df['Product Type'].unique())\ndf = df.sort_values(['Articles'], ascending=False)[0:50]\nplt.figure(figsize = (16,6))\nplt.title(f'Number of Articles per each Product Type (top 50 from total: {total_types})')\nsns.set_color_codes(\"pastel\")\ns = sns.barplot(x = 'Product Type', y=\"Articles\", data=df)\ns.set_xticklabels(s.get_xticklabels(),rotation=90)\nlocs, labels = plt.xticks()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:06:48.726646Z","iopub.execute_input":"2024-12-04T16:06:48.727047Z","iopub.status.idle":"2024-12-04T16:06:49.572857Z","shell.execute_reply.started":"2024-12-04T16:06:48.727011Z","shell.execute_reply":"2024-12-04T16:06:49.571534Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"temp = articles_df.groupby([\"department_name\"])[\"article_id\"].nunique()\ndf = pd.DataFrame({'Department Name': temp.index,\n                   'Articles': temp.values\n                  })\ntotal_depts = len(df['Department Name'].unique())\ndf = df.sort_values(['Articles'], ascending=False).head(50)\nplt.figure(figsize = (16,6))\nplt.title(f'Number of Articles per each Department (top 50 from total: {total_depts})')\nsns.set_color_codes(\"pastel\")\ns = sns.barplot(x = 'Department Name', y=\"Articles\", data=df)\ns.set_xticklabels(s.get_xticklabels(),rotation=90)\nlocs, labels = plt.xticks()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:07:00.957055Z","iopub.execute_input":"2024-12-04T16:07:00.957449Z","iopub.status.idle":"2024-12-04T16:07:01.659629Z","shell.execute_reply.started":"2024-12-04T16:07:00.957415Z","shell.execute_reply":"2024-12-04T16:07:01.658436Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"temp = articles_df.groupby([\"graphical_appearance_name\"])[\"article_id\"].nunique()\ndf = pd.DataFrame({'Graphical Appearance Name': temp.index,\n                   'Articles': temp.values\n                  })\ndf = df.sort_values(['Articles'], ascending=False).head(50)\nplt.figure(figsize = (16,6))\nplt.title(f'Number of Articles per each Graphical Appearance Name')\nsns.set_color_codes(\"pastel\")\ns = sns.barplot(x = 'Graphical Appearance Name', y=\"Articles\", data=df)\ns.set_xticklabels(s.get_xticklabels(),rotation=90)\nlocs, labels = plt.xticks()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:07:35.496777Z","iopub.execute_input":"2024-12-04T16:07:35.497179Z","iopub.status.idle":"2024-12-04T16:07:36.007342Z","shell.execute_reply.started":"2024-12-04T16:07:35.497145Z","shell.execute_reply":"2024-12-04T16:07:36.005257Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"temp = articles_df.groupby([\"index_group_name\"])[\"article_id\"].nunique()\ndf = pd.DataFrame({'Index Group Name': temp.index,\n                   'Articles': temp.values\n                  })\ndf = df.sort_values(['Articles'], ascending=False)\nplt.figure(figsize = (6,6))\nplt.title(f'Number of Articles per each Index Group Name')\nsns.set_color_codes(\"pastel\")\ns = sns.barplot(x = 'Index Group Name', y=\"Articles\", data=df)\ns.set_xticklabels(s.get_xticklabels(),rotation=90)\nlocs, labels = plt.xticks()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:07:44.057177Z","iopub.execute_input":"2024-12-04T16:07:44.05758Z","iopub.status.idle":"2024-12-04T16:07:44.306091Z","shell.execute_reply.started":"2024-12-04T16:07:44.057544Z","shell.execute_reply":"2024-12-04T16:07:44.304924Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"temp = articles_df.groupby([\"colour_group_name\"])[\"article_id\"].nunique()\ndf = pd.DataFrame({'Colour Group Name': temp.index,\n                   'Articles': temp.values\n                  })\ndf = df.sort_values(['Articles'], ascending=False)\nplt.figure(figsize = (12,6))\nplt.title(f'Number of Articles per each Colour Group Name')\nsns.set_color_codes(\"pastel\")\ns = sns.barplot(x = 'Colour Group Name', y=\"Articles\", data=df)\ns.set_xticklabels(s.get_xticklabels(),rotation=90)\nlocs, labels = plt.xticks()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:07:54.814264Z","iopub.execute_input":"2024-12-04T16:07:54.814652Z","iopub.status.idle":"2024-12-04T16:07:55.467293Z","shell.execute_reply.started":"2024-12-04T16:07:54.814618Z","shell.execute_reply":"2024-12-04T16:07:55.466127Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"temp = articles_df.groupby([\"garment_group_name\"])[\"article_id\"].nunique()\ndf = pd.DataFrame({'Garment Group Name': temp.index,\n                   'Articles': temp.values\n                  })\ndf = df.sort_values(['Articles'], ascending=False)\nplt.figure(figsize = (12,6))\nplt.title(f'Number of Articles per each Garment Group Name')\nsns.set_color_codes(\"pastel\")\ns = sns.barplot(x = 'Garment Group Name', y=\"Articles\", data=df)\ns.set_xticklabels(s.get_xticklabels(),rotation=90)\nlocs, labels = plt.xticks()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:08:10.458278Z","iopub.execute_input":"2024-12-04T16:08:10.45868Z","iopub.status.idle":"2024-12-04T16:08:10.824106Z","shell.execute_reply.started":"2024-12-04T16:08:10.458644Z","shell.execute_reply":"2024-12-04T16:08:10.822847Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"show_wordcloud(articles_df[\"detail_desc\"], \"Wordcloud from detailed description of articles\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:08:20.094845Z","iopub.execute_input":"2024-12-04T16:08:20.095686Z","iopub.status.idle":"2024-12-04T16:08:20.591359Z","shell.execute_reply.started":"2024-12-04T16:08:20.095639Z","shell.execute_reply":"2024-12-04T16:08:20.590061Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"temp = customers_df.groupby([\"age\"])[\"customer_id\"].count()\ndf = pd.DataFrame({'Age': temp.index,\n                   'Customers': temp.values\n                  })\ndf = df.sort_values(['Age'], ascending=False)\nplt.figure(figsize = (16,6))\nplt.title(f'Number of Customers per each Age')\nsns.set_color_codes(\"pastel\")\ns = sns.barplot(x = 'Age', y=\"Customers\", data=df)\ns.set_xticklabels(s.get_xticklabels(),rotation=90)\nlocs, labels = plt.xticks()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:08:29.703579Z","iopub.execute_input":"2024-12-04T16:08:29.70407Z","iopub.status.idle":"2024-12-04T16:08:30.824465Z","shell.execute_reply.started":"2024-12-04T16:08:29.704031Z","shell.execute_reply":"2024-12-04T16:08:30.823247Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = transactions_train_df.sample(100_000)\nfig, ax = plt.subplots(1, 1, figsize=(14, 7))\nsns.kdeplot(np.log(df.loc[df[\"sales_channel_id\"]==1].price.value_counts()))\nsns.kdeplot(np.log(df.loc[df[\"sales_channel_id\"]==2].price.value_counts()))\nax.legend(labels=['Sales channel 1', 'Sales channel 1'])\nplt.title(\"Logaritmic distribution of price frequency in transactions, grouped per sales channel (100k sample)\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:08:48.667832Z","iopub.execute_input":"2024-12-04T16:08:48.668278Z","iopub.status.idle":"2024-12-04T16:08:50.736814Z","shell.execute_reply.started":"2024-12-04T16:08:48.668229Z","shell.execute_reply":"2024-12-04T16:08:50.735485Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = transactions_train_df.sample(100_000).groupby([\"t_dat\"])[\"article_id\"].count().reset_index()\ndf[\"t_dat\"] = df[\"t_dat\"].apply(lambda x: datetime.strptime(x, '%Y-%m-%d'))\ndf.columns = [\"Date\", \"Transactions\"]\nfig, ax = plt.subplots(1, 1, figsize=(16,6))\nplt.plot(df[\"Date\"], df[\"Transactions\"], color=\"Darkgreen\")\nplt.xlabel(\"Date\")\nplt.ylabel(\"Transactions\")\nplt.title(f\"Transactions per day (100k sample; to get the real volume, please consider that real transaction count is {round(transactions_train_df.shape[0]/10.e6,2)}M)\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:08:59.661209Z","iopub.execute_input":"2024-12-04T16:08:59.661639Z","iopub.status.idle":"2024-12-04T16:09:01.768583Z","shell.execute_reply.started":"2024-12-04T16:08:59.6616Z","shell.execute_reply":"2024-12-04T16:09:01.767456Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = transactions_train_df.sample(100_000).groupby([\"t_dat\", \"sales_channel_id\"])[\"article_id\"].count().reset_index()\ndf[\"t_dat\"] = df[\"t_dat\"].apply(lambda x: datetime.strptime(x, '%Y-%m-%d'))\ndf.columns = [\"Date\", \"Sales Channel Id\", \"Transactions\"]\nfig, ax = plt.subplots(1, 1, figsize=(16,6))\ng1 = ax.plot(df.loc[df[\"Sales Channel Id\"]==1, \"Date\"], df.loc[df[\"Sales Channel Id\"]==1, \"Transactions\"], label=\"Sales Channel 1\", color=\"Darkblue\")\ng2 = ax.plot(df.loc[df[\"Sales Channel Id\"]==2, \"Date\"], df.loc[df[\"Sales Channel Id\"]==2, \"Transactions\"], label=\"Sales Channel 2\", color=\"Magenta\")\nplt.xlabel(\"Date\")\nplt.ylabel(\"Transactions\")\nax.legend()\nplt.title(f\"Transactions per day, grouped by Sales Channel (100k sample; to get the real volume, please consider that real transaction count is {round(transactions_train_df.shape[0]/10.e6,2)}M)\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:09:36.828397Z","iopub.execute_input":"2024-12-04T16:09:36.828764Z","iopub.status.idle":"2024-12-04T16:09:39.016551Z","shell.execute_reply.started":"2024-12-04T16:09:36.828733Z","shell.execute_reply":"2024-12-04T16:09:39.014884Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"transactions_train_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:10:05.524379Z","iopub.execute_input":"2024-12-04T16:10:05.524976Z","iopub.status.idle":"2024-12-04T16:10:05.54539Z","shell.execute_reply.started":"2024-12-04T16:10:05.524931Z","shell.execute_reply":"2024-12-04T16:10:05.542998Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"merged_df = pd.merge(transactions_train_df, articles_df, on='article_id', how='inner')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:23:21.784324Z","iopub.execute_input":"2024-12-04T16:23:21.784839Z","iopub.status.idle":"2024-12-04T16:23:44.928636Z","shell.execute_reply.started":"2024-12-04T16:23:21.784798Z","shell.execute_reply":"2024-12-04T16:23:44.927274Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"merged_df.head(10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:23:44.931075Z","iopub.execute_input":"2024-12-04T16:23:44.931689Z","iopub.status.idle":"2024-12-04T16:23:44.962884Z","shell.execute_reply.started":"2024-12-04T16:23:44.931617Z","shell.execute_reply":"2024-12-04T16:23:44.96166Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"trousers_df = merged_df[merged_df['product_type_name'] == 'Trousers']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:26:35.522091Z","iopub.execute_input":"2024-12-04T16:26:35.522423Z","iopub.status.idle":"2024-12-04T16:26:40.229295Z","shell.execute_reply.started":"2024-12-04T16:26:35.52238Z","shell.execute_reply":"2024-12-04T16:26:40.227929Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"trousers_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:27:14.668173Z","iopub.execute_input":"2024-12-04T16:27:14.668831Z","iopub.status.idle":"2024-12-04T16:27:14.727281Z","shell.execute_reply.started":"2024-12-04T16:27:14.668761Z","shell.execute_reply":"2024-12-04T16:27:14.723705Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"daily_sales_trousers = trousers_df.groupby(['t_dat', 'article_id']).size().reset_index(name='sales_count')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:29:24.84879Z","iopub.execute_input":"2024-12-04T16:29:24.849243Z","iopub.status.idle":"2024-12-04T16:29:25.413566Z","shell.execute_reply.started":"2024-12-04T16:29:24.849205Z","shell.execute_reply":"2024-12-04T16:29:25.411563Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"full_date_range = pd.date_range(start=daily_sales_trousers['t_dat'].min(), end=daily_sales_trousers['t_dat'].max(), freq='D')\nfull_dates_df = pd.DataFrame(full_date_range, columns=['t_dat'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:29:35.072291Z","iopub.execute_input":"2024-12-04T16:29:35.07273Z","iopub.status.idle":"2024-12-04T16:29:35.105173Z","shell.execute_reply.started":"2024-12-04T16:29:35.07269Z","shell.execute_reply":"2024-12-04T16:29:35.102371Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"all_article_ids = trousers_df['article_id'].unique()\nfull_sales_df = pd.MultiIndex.from_product([full_dates_df['t_dat'], all_article_ids], names=['t_dat', 'article_id']).to_frame(index=False)\nfinal_sales_df = pd.merge(full_sales_df, daily_sales_trousers, on=['t_dat', 'article_id'], how='left')\nfinal_sales_df['sales_count'] = final_sales_df['sales_count'].fillna(0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:29:50.205854Z","iopub.execute_input":"2024-12-04T16:29:50.207551Z","iopub.status.idle":"2024-12-04T16:29:53.047879Z","shell.execute_reply.started":"2024-12-04T16:29:50.2075Z","shell.execute_reply":"2024-12-04T16:29:53.046558Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"final_sales_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-04T16:29:56.100296Z","iopub.execute_input":"2024-12-04T16:29:56.100876Z","iopub.status.idle":"2024-12-04T16:29:56.11786Z","shell.execute_reply.started":"2024-12-04T16:29:56.100809Z","shell.execute_reply":"2024-12-04T16:29:56.116327Z"}},"outputs":[],"execution_count":null}]}