{"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-02-04T16:37:35.23367Z","iopub.execute_input":"2023-02-04T16:37:35.234171Z","iopub.status.idle":"2023-02-04T16:37:35.26663Z","shell.execute_reply.started":"2023-02-04T16:37:35.234074Z","shell.execute_reply":"2023-02-04T16:37:35.265337Z"},"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-02-04T16:37:35.268838Z","iopub.execute_input":"2023-02-04T16:37:35.269641Z","iopub.status.idle":"2023-02-04T16:38:55.004486Z","shell.execute_reply.started":"2023-02-04T16:37:35.269606Z","shell.execute_reply":"2023-02-04T16:38:55.002863Z"},"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-02-04T16:38:55.00713Z","iopub.execute_input":"2023-02-04T16:38:55.007545Z","iopub.status.idle":"2023-02-04T16:38:56.437464Z","shell.execute_reply.started":"2023-02-04T16:38:55.007505Z","shell.execute_reply":"2023-02-04T16:38:56.436086Z"},"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-02-04T16:38:56.439112Z","iopub.execute_input":"2023-02-04T16:38:56.439463Z","iopub.status.idle":"2023-02-04T16:38:56.487971Z","shell.execute_reply.started":"2023-02-04T16:38:56.439431Z","shell.execute_reply":"2023-02-04T16:38:56.486608Z"},"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-02-04T16:38:56.491602Z","iopub.execute_input":"2023-02-04T16:38:56.492118Z","iopub.status.idle":"2023-02-04T16:38:57.006959Z","shell.execute_reply.started":"2023-02-04T16:38:56.492078Z","shell.execute_reply":"2023-02-04T16:38:57.005923Z"},"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-02-04T16:38:57.008532Z","iopub.execute_input":"2023-02-04T16:38:57.008859Z","iopub.status.idle":"2023-02-04T16:38:57.014001Z","shell.execute_reply.started":"2023-02-04T16:38:57.008831Z","shell.execute_reply":"2023-02-04T16:38:57.012672Z"},"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-02-04T16:38:57.0157Z","iopub.execute_input":"2023-02-04T16:38:57.01611Z","iopub.status.idle":"2023-02-04T16:38:57.026429Z","shell.execute_reply.started":"2023-02-04T16:38:57.016077Z","shell.execute_reply":"2023-02-04T16:38:57.025573Z"},"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-02-04T16:38:57.02751Z","iopub.execute_input":"2023-02-04T16:38:57.02797Z"},"trusted":true},"execution_count":null,"outputs":[]}]}