{"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":"This code build embeddings for customer and article from text description embeddings. Customer embedding are averaged across the dimension of the embeddings.\n","metadata":{"papermill":{"duration":0.016757,"end_time":"2022-05-18T15:37:44.153706","exception":false,"start_time":"2022-05-18T15:37:44.136949","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import tensorflow_hub as hub\nimport numpy as np\nimport pandas as pd\nimport pickle\nimport warnings\nimport matplotlib.pyplot as plt\nwarnings.filterwarnings('ignore')\n\npath = '../input/h-and-m-personalized-fashion-recommendations/articles.csv'\n \ndf = pd.read_csv(path, dtype={'article_id':str}).astype(str)","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":9.189557,"end_time":"2022-05-18T15:37:53.360495","exception":false,"start_time":"2022-05-18T15:37:44.170938","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-05-06T09:06:24.045173Z","iopub.execute_input":"2023-05-06T09:06:24.045717Z","iopub.status.idle":"2023-05-06T09:06:25.604146Z","shell.execute_reply.started":"2023-05-06T09:06:24.045666Z","shell.execute_reply":"2023-05-06T09:06:25.602731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#@title Load the Universal Sentence Encoder's TF Hub module\n\nmodule_url = \"https://tfhub.dev/google/universal-sentence-encoder/4\" #@param [\"https://tfhub.dev/google/universal-sentence-encoder/4\", \"https://tfhub.dev/google/universal-sentence-encoder-large/5\"]\nmodel = hub.load(module_url)","metadata":{"papermill":{"duration":21.17412,"end_time":"2022-05-18T15:38:14.549706","exception":false,"start_time":"2022-05-18T15:37:53.375586","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-05-06T09:06:25.606616Z","iopub.execute_input":"2023-05-06T09:06:25.607074Z","iopub.status.idle":"2023-05-06T09:07:04.242662Z","shell.execute_reply.started":"2023-05-06T09:06:25.607033Z","shell.execute_reply":"2023-05-06T09:07:04.241785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"desc = df['detail_desc'].unique()","metadata":{"papermill":{"duration":0.076229,"end_time":"2022-05-18T15:38:14.643491","exception":false,"start_time":"2022-05-18T15:38:14.567262","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-05-06T09:07:04.243684Z","iopub.execute_input":"2023-05-06T09:07:04.244059Z","iopub.status.idle":"2023-05-06T09:07:04.305038Z","shell.execute_reply.started":"2023-05-06T09:07:04.244027Z","shell.execute_reply":"2023-05-06T09:07:04.303814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nembeds = model(desc)","metadata":{"papermill":{"duration":8.078876,"end_time":"2022-05-18T15:38:22.738915","exception":false,"start_time":"2022-05-18T15:38:14.660039","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-05-06T09:07:04.308871Z","iopub.execute_input":"2023-05-06T09:07:04.309547Z","iopub.status.idle":"2023-05-06T09:07:14.355838Z","shell.execute_reply.started":"2023-05-06T09:07:04.309495Z","shell.execute_reply":"2023-05-06T09:07:14.354500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"desc_embedding_pairs = {i:embed.numpy() for i, embed in zip(desc, embeds)}","metadata":{"papermill":{"duration":14.402476,"end_time":"2022-05-18T15:38:37.157489","exception":false,"start_time":"2022-05-18T15:38:22.755013","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-05-06T09:07:14.357804Z","iopub.execute_input":"2023-05-06T09:07:14.358570Z","iopub.status.idle":"2023-05-06T09:07:20.800110Z","shell.execute_reply.started":"2023-05-06T09:07:14.358523Z","shell.execute_reply":"2023-05-06T09:07:20.798730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df[['article_id', 'detail_desc']]","metadata":{"papermill":{"duration":0.165783,"end_time":"2022-05-18T15:38:37.339737","exception":false,"start_time":"2022-05-18T15:38:37.173954","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-05-06T09:07:20.801366Z","iopub.execute_input":"2023-05-06T09:07:20.801696Z","iopub.status.idle":"2023-05-06T09:07:20.984439Z","shell.execute_reply.started":"2023-05-06T09:07:20.801667Z","shell.execute_reply":"2023-05-06T09:07:20.981893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['embeddings'] = df.detail_desc.map(desc_embedding_pairs)","metadata":{"papermill":{"duration":0.067272,"end_time":"2022-05-18T15:38:37.423419","exception":false,"start_time":"2022-05-18T15:38:37.356147","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-05-06T09:07:20.986660Z","iopub.execute_input":"2023-05-06T09:07:20.987543Z","iopub.status.idle":"2023-05-06T09:07:21.044468Z","shell.execute_reply.started":"2023-05-06T09:07:20.987483Z","shell.execute_reply":"2023-05-06T09:07:21.043327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df[['article_id', 'embeddings']]","metadata":{"papermill":{"duration":0.03913,"end_time":"2022-05-18T15:38:37.479324","exception":false,"start_time":"2022-05-18T15:38:37.440194","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-05-06T09:07:21.045943Z","iopub.execute_input":"2023-05-06T09:07:21.046373Z","iopub.status.idle":"2023-05-06T09:07:21.254126Z","shell.execute_reply.started":"2023-05-06T09:07:21.046335Z","shell.execute_reply":"2023-05-06T09:07:21.252553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f = open('article_embeddings_from_text.pickle', 'wb')\npickle.dump(df.values, f)","metadata":{"papermill":{"duration":0.580586,"end_time":"2022-05-18T15:38:38.07752","exception":false,"start_time":"2022-05-18T15:38:37.496934","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-05-06T09:07:21.256494Z","iopub.execute_input":"2023-05-06T09:07:21.257814Z","iopub.status.idle":"2023-05-06T09:07:21.967615Z","shell.execute_reply.started":"2023-05-06T09:07:21.257746Z","shell.execute_reply":"2023-05-06T09:07:21.966266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv',\n                    usecols=['customer_id', 'article_id'],\n                   dtype=str)","metadata":{"papermill":{"duration":64.834268,"end_time":"2022-05-18T15:39:42.928141","exception":false,"start_time":"2022-05-18T15:38:38.093873","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-05-06T09:07:21.972094Z","iopub.execute_input":"2023-05-06T09:07:21.972665Z","iopub.status.idle":"2023-05-06T09:08:46.154180Z","shell.execute_reply.started":"2023-05-06T09:07:21.972617Z","shell.execute_reply":"2023-05-06T09:08:46.152773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans = trans.merge(df, on='article_id').drop('article_id', axis=1)","metadata":{"papermill":{"duration":31.222655,"end_time":"2022-05-18T15:40:14.168454","exception":false,"start_time":"2022-05-18T15:39:42.945799","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-05-06T09:08:46.156023Z","iopub.execute_input":"2023-05-06T09:08:46.156450Z","iopub.status.idle":"2023-05-06T09:09:20.422571Z","shell.execute_reply.started":"2023-05-06T09:08:46.156415Z","shell.execute_reply":"2023-05-06T09:09:20.419167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ncustomer_embeddings = trans.groupby('customer_id').mean()","metadata":{"papermill":{"duration":270.61653,"end_time":"2022-05-18T15:44:44.803027","exception":false,"start_time":"2022-05-18T15:40:14.186497","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-05-06T09:09:20.425693Z","iopub.execute_input":"2023-05-06T09:09:20.426356Z","iopub.status.idle":"2023-05-06T09:14:30.936297Z","shell.execute_reply.started":"2023-05-06T09:09:20.426291Z","shell.execute_reply":"2023-05-06T09:14:30.934577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f = open('customer_embeddings_from_text.pickle', 'wb')","metadata":{"papermill":{"duration":0.02269,"end_time":"2022-05-18T15:44:44.840952","exception":false,"start_time":"2022-05-18T15:44:44.818262","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-05-06T09:14:30.938256Z","iopub.execute_input":"2023-05-06T09:14:30.938659Z","iopub.status.idle":"2023-05-06T09:14:30.945997Z","shell.execute_reply.started":"2023-05-06T09:14:30.938623Z","shell.execute_reply":"2023-05-06T09:14:30.944346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pickle.dump(customer_embeddings, f)","metadata":{"papermill":{"duration":26.967751,"end_time":"2022-05-18T15:45:11.824051","exception":false,"start_time":"2022-05-18T15:44:44.8563","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-05-06T09:14:30.947754Z","iopub.execute_input":"2023-05-06T09:14:30.948134Z","iopub.status.idle":"2023-05-06T09:14:52.164345Z","shell.execute_reply.started":"2023-05-06T09:14:30.948102Z","shell.execute_reply":"2023-05-06T09:14:52.162949Z"},"trusted":true},"execution_count":null,"outputs":[]}]}