{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# H&M - Baseline: 12 most popular SEASONAL items\n\n### A naive approach system using the 12 items that were \"most popular\" for the specific month!\n\n* Test period: 7 days, From 2020-09-22 to 2020-09-29 \n\n* V1: september only, most popular by total users - 0.004 public LB\n* V2: most popular by total amount - 0.000 public LB\n* V3: september+October only, most popular by total users\n\nNormally this approach would be VERY strong as a baseline - but! COVID messes this sort of consumer behavior up, especially given that out september history was during lockdowns. We lack relevant historical data!","metadata":{}},{"cell_type":"code","source":"import pandas as pd","metadata":{"execution":{"iopub.status.busy":"2022-02-09T09:41:03.553929Z","iopub.execute_input":"2022-02-09T09:41:03.554289Z","iopub.status.idle":"2022-02-09T09:41:03.567185Z","shell.execute_reply.started":"2022-02-09T09:41:03.554247Z","shell.execute_reply":"2022-02-09T09:41:03.566492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATA_PATH = \"../input/h-and-m-personalized-fashion-recommendations/\"","metadata":{"execution":{"iopub.status.busy":"2022-02-09T09:40:20.824156Z","iopub.execute_input":"2022-02-09T09:40:20.824458Z","iopub.status.idle":"2022-02-09T09:40:20.828232Z","shell.execute_reply.started":"2022-02-09T09:40:20.824427Z","shell.execute_reply":"2022-02-09T09:40:20.827567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(DATA_PATH+'transactions_train.csv', dtype={'article_id': str},\n                parse_dates=['t_dat'],infer_datetime_format=True,usecols=['t_dat', 'customer_id', 'article_id'])\ndf_sub = pd.read_csv(DATA_PATH+'sample_submission.csv')\n\narticles = pd.read_csv(DATA_PATH+'articles.csv').set_index('article_id')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-09T09:40:21.468744Z","iopub.execute_input":"2022-02-09T09:40:21.469235Z","iopub.status.idle":"2022-02-09T09:41:03.54483Z","shell.execute_reply.started":"2022-02-09T09:40:21.469202Z","shell.execute_reply":"2022-02-09T09:41:03.543811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Keep only records for September\n* Could try also +- a month - winter/autumn season","metadata":{}},{"cell_type":"code","source":"print(df.shape[0])\ndf = df.loc[df['t_dat'].dt.month==9]\n# df = df.loc[df['t_dat'].dt.month.between(9,10)] ## september through october?\nprint(df.shape[0])\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-09T09:41:03.569155Z","iopub.execute_input":"2022-02-09T09:41:03.570048Z","iopub.status.idle":"2022-02-09T09:41:06.58454Z","shell.execute_reply.started":"2022-02-09T09:41:03.570002Z","shell.execute_reply":"2022-02-09T09:41:06.583414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Get the 12 most popular items\n* By total users OR by total purchases\n","metadata":{}},{"cell_type":"code","source":"## 12 by unique users:\nprint(\"top 12 by unique customers\")\n# top_12_items = df.groupby('article_id')['customer_id'].nunique().sort_values(ascending=False).head(12).index#.tolist() # orig\ntop_12_byUser_items = df.groupby('article_id')['customer_id'].nunique().sort_values(ascending=False).head(12).index.astype(int)\n\ndisplay(articles.loc[top_12_byUser_items])\n\n# top_12_byUser_items = top_12_byUser_items.astype(str).tolist()\n\ntop_12_byUser_items= df.groupby('article_id')['customer_id'].nunique().sort_values(ascending=False).head(12).index.tolist() # recalc\nprint(top_12_byUser_items)","metadata":{"execution":{"iopub.status.busy":"2022-02-09T09:44:36.537859Z","iopub.execute_input":"2022-02-09T09:44:36.538758Z","iopub.status.idle":"2022-02-09T09:44:38.149958Z","shell.execute_reply.started":"2022-02-09T09:44:36.538697Z","shell.execute_reply":"2022-02-09T09:44:38.149292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"top 12 by overall popularity\")\n# top_12_items = df['article_id'].value_counts().sort_values(ascending=False).head(12).index.tolist()\ntop_12_pop_items = df['article_id'].value_counts().sort_values(ascending=False).head(12).index.astype(int)\n\ndisplay(articles.loc[top_12_pop_items])\n\n# top_12_pop_items = top_12_pop_items.astype(str).tolist()\ntop_12_pop_items = df['article_id'].value_counts().sort_values(ascending=False).head(12).index.tolist()\ntop_12_pop_items","metadata":{"execution":{"iopub.status.busy":"2022-02-09T09:45:38.060915Z","iopub.execute_input":"2022-02-09T09:45:38.061233Z","iopub.status.idle":"2022-02-09T09:45:38.468904Z","shell.execute_reply.started":"2022-02-09T09:45:38.061202Z","shell.execute_reply":"2022-02-09T09:45:38.467902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"top_12_items = top_12_byUser_items \n# top_12_items = top_12_pop_items # inferior","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submit","metadata":{}},{"cell_type":"code","source":"# Predict for everyone\ndf_sub['prediction'] =  ' '.join(top_12_items)\ndf_sub.to_csv('submission.csv', index=False)\ndf_sub.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-09T09:28:27.274805Z","iopub.execute_input":"2022-02-09T09:28:27.275281Z","iopub.status.idle":"2022-02-09T09:28:33.606223Z","shell.execute_reply.started":"2022-02-09T09:28:27.275238Z","shell.execute_reply":"2022-02-09T09:28:33.605317Z"},"trusted":true},"execution_count":null,"outputs":[]}]}