{"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":{}},{"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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-18T15:34:46.456975Z","iopub.execute_input":"2022-05-18T15:34:46.457252Z","iopub.status.idle":"2022-05-18T15:34:54.792307Z","shell.execute_reply.started":"2022-05-18T15:34:46.457222Z","shell.execute_reply":"2022-05-18T15:34:54.791393Z"},"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":{"execution":{"iopub.status.busy":"2022-05-18T15:34:54.794179Z","iopub.execute_input":"2022-05-18T15:34:54.794456Z","iopub.status.idle":"2022-05-18T15:35:15.478211Z","shell.execute_reply.started":"2022-05-18T15:34:54.794418Z","shell.execute_reply":"2022-05-18T15:35:15.477376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"desc = df['detail_desc'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-05-18T15:35:15.479792Z","iopub.execute_input":"2022-05-18T15:35:15.480083Z","iopub.status.idle":"2022-05-18T15:35:15.534598Z","shell.execute_reply.started":"2022-05-18T15:35:15.480047Z","shell.execute_reply":"2022-05-18T15:35:15.533797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nembeds = model(desc)","metadata":{"execution":{"iopub.status.busy":"2022-05-18T15:35:15.536671Z","iopub.execute_input":"2022-05-18T15:35:15.537019Z","iopub.status.idle":"2022-05-18T15:35:23.184684Z","shell.execute_reply.started":"2022-05-18T15:35:15.536979Z","shell.execute_reply":"2022-05-18T15:35:23.183867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"desc_embedding_pairs = {i:embed.numpy() for i, embed in zip(desc, embeds)}","metadata":{"execution":{"iopub.status.busy":"2022-05-18T15:35:23.186135Z","iopub.execute_input":"2022-05-18T15:35:23.186591Z","iopub.status.idle":"2022-05-18T15:35:37.347176Z","shell.execute_reply.started":"2022-05-18T15:35:23.186549Z","shell.execute_reply":"2022-05-18T15:35:37.346104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df[['article_id', 'detail_desc']]","metadata":{"execution":{"iopub.status.busy":"2022-05-18T15:35:37.348703Z","iopub.execute_input":"2022-05-18T15:35:37.348976Z","iopub.status.idle":"2022-05-18T15:35:37.489507Z","shell.execute_reply.started":"2022-05-18T15:35:37.348939Z","shell.execute_reply":"2022-05-18T15:35:37.488653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['embeddings'] = df.detail_desc.map(desc_embedding_pairs)","metadata":{"execution":{"iopub.status.busy":"2022-05-18T15:35:37.490686Z","iopub.execute_input":"2022-05-18T15:35:37.491508Z","iopub.status.idle":"2022-05-18T15:35:37.528360Z","shell.execute_reply.started":"2022-05-18T15:35:37.491460Z","shell.execute_reply":"2022-05-18T15:35:37.527529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df[['article_id', 'embeddings']]","metadata":{"execution":{"iopub.status.busy":"2022-05-18T15:35:37.529888Z","iopub.execute_input":"2022-05-18T15:35:37.530341Z","iopub.status.idle":"2022-05-18T15:35:37.548173Z","shell.execute_reply.started":"2022-05-18T15:35:37.530301Z","shell.execute_reply":"2022-05-18T15:35:37.547409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f = open('article_embeddings_from_text.pickle', 'wb')\npickle.dump(df.values, f)","metadata":{"execution":{"iopub.status.busy":"2022-05-18T15:35:37.549373Z","iopub.execute_input":"2022-05-18T15:35:37.549758Z","iopub.status.idle":"2022-05-18T15:35:38.063095Z","shell.execute_reply.started":"2022-05-18T15:35:37.549715Z","shell.execute_reply":"2022-05-18T15:35:38.062282Z"},"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":{"execution":{"iopub.status.busy":"2022-05-18T15:35:38.065881Z","iopub.execute_input":"2022-05-18T15:35:38.066185Z","iopub.status.idle":"2022-05-18T15:35:38.083080Z","shell.execute_reply.started":"2022-05-18T15:35:38.066132Z","shell.execute_reply":"2022-05-18T15:35:38.082290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans = trans.merge(df, on='article_id').drop('article_id', axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-05-18T15:35:38.084371Z","iopub.execute_input":"2022-05-18T15:35:38.084730Z","iopub.status.idle":"2022-05-18T15:35:38.138485Z","shell.execute_reply.started":"2022-05-18T15:35:38.084688Z","shell.execute_reply":"2022-05-18T15:35:38.137682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ncustomer_embeddings = trans.groupby('customer_id').mean()","metadata":{"execution":{"iopub.status.busy":"2022-05-18T15:35:38.139697Z","iopub.execute_input":"2022-05-18T15:35:38.140461Z","iopub.status.idle":"2022-05-18T15:35:38.194606Z","shell.execute_reply.started":"2022-05-18T15:35:38.140414Z","shell.execute_reply":"2022-05-18T15:35:38.193753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f = open('customer_embeddings_from_text.pickle', 'wb')","metadata":{"execution":{"iopub.status.busy":"2022-05-18T15:35:38.195842Z","iopub.execute_input":"2022-05-18T15:35:38.197451Z","iopub.status.idle":"2022-05-18T15:35:38.202416Z","shell.execute_reply.started":"2022-05-18T15:35:38.197395Z","shell.execute_reply":"2022-05-18T15:35:38.201664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pickle.dump(customer_embeddings, f)","metadata":{"execution":{"iopub.status.busy":"2022-05-18T15:35:38.204101Z","iopub.execute_input":"2022-05-18T15:35:38.204693Z","iopub.status.idle":"2022-05-18T15:35:38.214660Z","shell.execute_reply.started":"2022-05-18T15:35:38.204639Z","shell.execute_reply":"2022-05-18T15:35:38.213782Z"},"trusted":true},"execution_count":null,"outputs":[]}]}