{"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 # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport pickle","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":0.020493,"end_time":"2022-05-18T11:41:33.775466","exception":false,"start_time":"2022-05-18T11:41:33.754973","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-05-07T15:25:10.231596Z","iopub.execute_input":"2023-05-07T15:25:10.232476Z","iopub.status.idle":"2023-05-07T15:25:10.267646Z","shell.execute_reply.started":"2023-05-07T15:25:10.232269Z","shell.execute_reply":"2023-05-07T15:25:10.265813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv',\n                 usecols = ['customer_id', 'article_id'], dtype=str)\n# df = next(df)","metadata":{"papermill":{"duration":59.406479,"end_time":"2022-05-18T11:42:33.190109","exception":false,"start_time":"2022-05-18T11:41:33.78363","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-05-07T15:25:10.270259Z","iopub.execute_input":"2023-05-07T15:25:10.270894Z","iopub.status.idle":"2023-05-07T15:26:50.728598Z","shell.execute_reply.started":"2023-05-07T15:25:10.270841Z","shell.execute_reply":"2023-05-07T15:26:50.726621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/articles.csv', dtype=str)","metadata":{"papermill":{"duration":1.133461,"end_time":"2022-05-18T11:42:34.332309","exception":false,"start_time":"2022-05-18T11:42:33.198848","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-05-07T15:26:50.730034Z","iopub.execute_input":"2023-05-07T15:26:50.730464Z","iopub.status.idle":"2023-05-07T15:26:52.332397Z","shell.execute_reply.started":"2023-05-07T15:26:50.730425Z","shell.execute_reply":"2023-05-07T15:26:52.330356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles = articles[['article_id', 'product_type_name',\n       'product_group_name', \n       'graphical_appearance_name', 'colour_group_name',\n       'perceived_colour_value_name',\n       'perceived_colour_master_name',\n       'department_name', 'index_name',\n       'index_group_name', 'section_name',\n       'garment_group_name']]\n\nfeature_subset = ['product_group_name', 'product_type_name',\n       'graphical_appearance_name', 'colour_group_name',\n       'perceived_colour_value_name',\n       'perceived_colour_master_name',\n       'department_name', 'index_name',\n       'index_group_name', 'section_name',\n       'garment_group_name']","metadata":{"papermill":{"duration":0.048145,"end_time":"2022-05-18T11:42:34.388289","exception":false,"start_time":"2022-05-18T11:42:34.340144","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-05-07T15:26:52.335007Z","iopub.execute_input":"2023-05-07T15:26:52.33554Z","iopub.status.idle":"2023-05-07T15:26:52.389358Z","shell.execute_reply.started":"2023-05-07T15:26:52.335447Z","shell.execute_reply":"2023-05-07T15:26:52.388334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles = pd.get_dummies(articles, columns=feature_subset)","metadata":{"papermill":{"duration":0.485188,"end_time":"2022-05-18T11:42:34.881088","exception":false,"start_time":"2022-05-18T11:42:34.3959","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-05-07T15:26:52.390672Z","iopub.execute_input":"2023-05-07T15:26:52.391381Z","iopub.status.idle":"2023-05-07T15:26:52.960964Z","shell.execute_reply.started":"2023-05-07T15:26:52.391328Z","shell.execute_reply":"2023-05-07T15:26:52.959089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# articles.to_csv('articles_embeddings_from_features.csv', index=False)","metadata":{"papermill":{"duration":0.015078,"end_time":"2022-05-18T11:42:34.904772","exception":false,"start_time":"2022-05-18T11:42:34.889694","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-05-07T15:26:52.963625Z","iopub.execute_input":"2023-05-07T15:26:52.964788Z","iopub.status.idle":"2023-05-07T15:26:52.970665Z","shell.execute_reply.started":"2023-05-07T15:26:52.964741Z","shell.execute_reply":"2023-05-07T15:26:52.969524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers = df.groupby('customer_id')","metadata":{"papermill":{"duration":0.014321,"end_time":"2022-05-18T11:42:34.926806","exception":false,"start_time":"2022-05-18T11:42:34.912485","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-05-07T15:26:52.975113Z","iopub.execute_input":"2023-05-07T15:26:52.975517Z","iopub.status.idle":"2023-05-07T15:26:52.988879Z","shell.execute_reply.started":"2023-05-07T15:26:52.975474Z","shell.execute_reply":"2023-05-07T15:26:52.986892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f = open('customer_embeddings_from_features.pickle', 'wb')\n\nfor group in customers.groups:\n    temp = customers.get_group(group).merge(articles, on='article_id').drop('article_id', axis=1)\n    temp = temp[temp.columns[1:]].sum()\n    pickle.dump([group, temp.values], f)","metadata":{"papermill":{"duration":null,"end_time":null,"exception":false,"start_time":"2022-05-18T11:42:34.93447","status":"running"},"tags":[],"execution":{"iopub.status.busy":"2023-05-07T15:26:52.991065Z","iopub.execute_input":"2023-05-07T15:26:52.99152Z"},"trusted":true},"execution_count":null,"outputs":[]}]}