{"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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-18T11:31:23.526925Z","iopub.execute_input":"2022-05-18T11:31:23.527251Z","iopub.status.idle":"2022-05-18T11:31:23.531226Z","shell.execute_reply.started":"2022-05-18T11:31:23.527218Z","shell.execute_reply":"2022-05-18T11:31:23.530638Z"},"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":{"execution":{"iopub.status.busy":"2022-05-18T11:24:22.350597Z","iopub.execute_input":"2022-05-18T11:24:22.350966Z","iopub.status.idle":"2022-05-18T11:25:28.966333Z","shell.execute_reply.started":"2022-05-18T11:24:22.350928Z","shell.execute_reply":"2022-05-18T11:25:28.965363Z"},"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":{"execution":{"iopub.status.busy":"2022-05-18T11:29:47.784257Z","iopub.execute_input":"2022-05-18T11:29:47.785131Z","iopub.status.idle":"2022-05-18T11:29:48.573807Z","shell.execute_reply.started":"2022-05-18T11:29:47.785089Z","shell.execute_reply":"2022-05-18T11:29:48.5728Z"},"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":{"execution":{"iopub.status.busy":"2022-05-18T11:29:48.575691Z","iopub.execute_input":"2022-05-18T11:29:48.575958Z","iopub.status.idle":"2022-05-18T11:29:48.603139Z","shell.execute_reply.started":"2022-05-18T11:29:48.575925Z","shell.execute_reply":"2022-05-18T11:29:48.602079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles = pd.get_dummies(articles, columns=feature_subset)","metadata":{"execution":{"iopub.status.busy":"2022-05-18T11:29:48.604332Z","iopub.execute_input":"2022-05-18T11:29:48.604589Z","iopub.status.idle":"2022-05-18T11:29:49.113417Z","shell.execute_reply.started":"2022-05-18T11:29:48.604559Z","shell.execute_reply":"2022-05-18T11:29:49.112735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# articles.to_csv('articles_embeddings_from_features.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-05-18T11:29:55.801358Z","iopub.execute_input":"2022-05-18T11:29:55.801683Z","iopub.status.idle":"2022-05-18T11:29:55.805536Z","shell.execute_reply.started":"2022-05-18T11:29:55.801651Z","shell.execute_reply":"2022-05-18T11:29:55.804597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers = df.groupby('customer_id')","metadata":{"execution":{"iopub.status.busy":"2022-05-18T11:29:56.257733Z","iopub.execute_input":"2022-05-18T11:29:56.258019Z","iopub.status.idle":"2022-05-18T11:29:56.263044Z","shell.execute_reply.started":"2022-05-18T11:29:56.257991Z","shell.execute_reply":"2022-05-18T11:29:56.261815Z"},"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":{"execution":{"iopub.status.busy":"2022-05-18T11:39:03.482724Z","iopub.execute_input":"2022-05-18T11:39:03.483458Z","iopub.status.idle":"2022-05-18T11:39:03.894194Z","shell.execute_reply.started":"2022-05-18T11:39:03.483408Z","shell.execute_reply":"2022-05-18T11:39:03.892791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}