{"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":"code","source":"import numpy as np \nimport pandas as pd \nimport os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n\nimport datatable as dt\n\nfrom scipy import stats\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport plotly.io as pio\nimport plotly.express as px\nimport plotly.figure_factory as ff\nimport plotly.graph_objects as go\n\nfrom plotly.subplots import make_subplots\nfrom plotly.offline import init_notebook_mode, iplot\n\ninit_notebook_mode(connected=True)\npio.templates.default = \"none\"\nimport cv2\n\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-08T12:33:14.882585Z","iopub.execute_input":"2022-02-08T12:33:14.882825Z","iopub.status.idle":"2022-02-08T12:33:14.890327Z","shell.execute_reply.started":"2022-02-08T12:33:14.882798Z","shell.execute_reply":"2022-02-08T12:33:14.889767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* Data loading is slow - could speed up via loading preconverted parquet/feather files","metadata":{}},{"cell_type":"code","source":"%%time\npath = '/kaggle/input/h-and-m-personalized-fashion-recommendations'\n\nsample_sumbission = dt.fread(path+'/sample_submission.csv').to_pandas()\nart = dt.fread(path+'/articles.csv').to_pandas()\ncust = dt.fread(path+ '/customers.csv').to_pandas()","metadata":{"execution":{"iopub.status.busy":"2022-02-08T12:32:15.388638Z","iopub.execute_input":"2022-02-08T12:32:15.388891Z","iopub.status.idle":"2022-02-08T12:33:14.881412Z","shell.execute_reply.started":"2022-02-08T12:32:15.388863Z","shell.execute_reply":"2022-02-08T12:33:14.880601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ntrans_train = dt.fread(path+'/transactions_train.csv').to_pandas()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(trans_train.shape)\nprint(trans_train.nunique())","metadata":{"execution":{"iopub.status.busy":"2022-02-08T12:33:14.891687Z","iopub.execute_input":"2022-02-08T12:33:14.892371Z","iopub.status.idle":"2022-02-08T12:33:24.591365Z","shell.execute_reply.started":"2022-02-08T12:33:14.892341Z","shell.execute_reply":"2022-02-08T12:33:24.590594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(trans_train)","metadata":{"execution":{"iopub.status.busy":"2022-02-08T12:33:47.734777Z","iopub.execute_input":"2022-02-08T12:33:47.735100Z","iopub.status.idle":"2022-02-08T12:33:47.752151Z","shell.execute_reply.started":"2022-02-08T12:33:47.735069Z","shell.execute_reply":"2022-02-08T12:33:47.751325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(art.shape)\nprint(art.nunique())","metadata":{"execution":{"iopub.status.busy":"2022-02-08T12:33:41.749872Z","iopub.execute_input":"2022-02-08T12:33:41.750438Z","iopub.status.idle":"2022-02-08T12:33:41.945029Z","shell.execute_reply.started":"2022-02-08T12:33:41.750397Z","shell.execute_reply":"2022-02-08T12:33:41.944203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"art.drop_duplicates().shape[0]  ## note - would work better by dorp dupes by all cols except for article_id                      ","metadata":{"execution":{"iopub.status.busy":"2022-02-08T12:39:28.897615Z","iopub.execute_input":"2022-02-08T12:39:28.898252Z","iopub.status.idle":"2022-02-08T12:39:29.263608Z","shell.execute_reply.started":"2022-02-08T12:39:28.898194Z","shell.execute_reply":"2022-02-08T12:39:29.262720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(art)","metadata":{"execution":{"iopub.status.busy":"2022-02-08T12:33:38.934404Z","iopub.execute_input":"2022-02-08T12:33:38.934736Z","iopub.status.idle":"2022-02-08T12:33:39.081301Z","shell.execute_reply.started":"2022-02-08T12:33:38.934700Z","shell.execute_reply":"2022-02-08T12:33:39.080722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(cust.shape)\nprint(cust.nunique())\ndisplay(cust)","metadata":{"execution":{"iopub.status.busy":"2022-02-08T12:33:24.930901Z","iopub.execute_input":"2022-02-08T12:33:24.931178Z","iopub.status.idle":"2022-02-08T12:33:26.438847Z","shell.execute_reply.started":"2022-02-08T12:33:24.931137Z","shell.execute_reply":"2022-02-08T12:33:26.438081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Frequency analysis:\n* Do we have customer history?\n* Distribution freq of items\n\n - We see a very long tail of products, and many customers with a small amount of history\n     * BUT: Most customers do have some previous purchases! i.e this isn't purely session absed and we CAN use recommenders/CF!","metadata":{}},{"cell_type":"code","source":"trans_train[\"customer_id\"].value_counts().describe().round(1)","metadata":{"execution":{"iopub.status.busy":"2022-02-08T12:34:20.422590Z","iopub.execute_input":"2022-02-08T12:34:20.423372Z","iopub.status.idle":"2022-02-08T12:34:34.502369Z","shell.execute_reply.started":"2022-02-08T12:34:20.423315Z","shell.execute_reply":"2022-02-08T12:34:34.501576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans_train[\"customer_id\"].value_counts().hist();","metadata":{"execution":{"iopub.status.busy":"2022-02-08T12:34:34.503722Z","iopub.execute_input":"2022-02-08T12:34:34.503902Z","iopub.status.idle":"2022-02-08T12:34:44.348929Z","shell.execute_reply.started":"2022-02-08T12:34:34.503880Z","shell.execute_reply":"2022-02-08T12:34:44.348138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans_train[\"article_id\"].value_counts().describe().round(1)","metadata":{"execution":{"iopub.status.busy":"2022-02-08T12:36:29.338964Z","iopub.execute_input":"2022-02-08T12:36:29.339293Z","iopub.status.idle":"2022-02-08T12:36:30.644109Z","shell.execute_reply.started":"2022-02-08T12:36:29.339257Z","shell.execute_reply":"2022-02-08T12:36:30.643275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans_train[\"article_id\"].value_counts().hist(bins=30);","metadata":{"execution":{"iopub.status.busy":"2022-02-08T12:36:45.262333Z","iopub.execute_input":"2022-02-08T12:36:45.262981Z","iopub.status.idle":"2022-02-08T12:36:46.795337Z","shell.execute_reply.started":"2022-02-08T12:36:45.262934Z","shell.execute_reply":"2022-02-08T12:36:46.793722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans_train[\"article_id\"].nunique()","metadata":{"execution":{"iopub.status.busy":"2022-02-08T12:35:15.913008Z","iopub.execute_input":"2022-02-08T12:35:15.913285Z","iopub.status.idle":"2022-02-08T12:35:16.169681Z","shell.execute_reply.started":"2022-02-08T12:35:15.913257Z","shell.execute_reply":"2022-02-08T12:35:16.168968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* 20% of articles are very rare (under 9 cases)","metadata":{}},{"cell_type":"code","source":"(trans_train[\"article_id\"].value_counts()>9).sum()","metadata":{"execution":{"iopub.status.busy":"2022-02-08T12:35:44.643489Z","iopub.execute_input":"2022-02-08T12:35:44.644037Z","iopub.status.idle":"2022-02-08T12:35:45.929680Z","shell.execute_reply.started":"2022-02-08T12:35:44.643997Z","shell.execute_reply":"2022-02-08T12:35:45.929046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## 10 most popular\nart.loc[art[\"article_id\"].isin(trans_train[\"article_id\"].value_counts().sort_values(ascending=False).head(10).index)]","metadata":{"execution":{"iopub.status.busy":"2022-02-08T12:38:01.838535Z","iopub.execute_input":"2022-02-08T12:38:01.838886Z","iopub.status.idle":"2022-02-08T12:38:03.257727Z","shell.execute_reply.started":"2022-02-08T12:38:01.838852Z","shell.execute_reply":"2022-02-08T12:38:03.257160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}