{"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\n\nimport 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":{"papermill":{"duration":0.005864,"end_time":"2023-01-30T17:18:51.000918","exception":false,"start_time":"2023-01-30T17:18:50.995054","status":"completed"},"tags":[]}},{"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.execute_input":"2023-01-30T17:19:00.978085Z","iopub.status.busy":"2023-01-30T17:19:00.977533Z","iopub.status.idle":"2023-01-30T17:19:28.980182Z","shell.execute_reply":"2023-01-30T17:19:28.977991Z"},"papermill":{"duration":28.012207,"end_time":"2023-01-30T17:19:28.983980","exception":false,"start_time":"2023-01-30T17:19:00.971773","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"desc = df['detail_desc'].unique()","metadata":{"execution":{"iopub.execute_input":"2023-01-30T17:19:28.998324Z","iopub.status.busy":"2023-01-30T17:19:28.997676Z","iopub.status.idle":"2023-01-30T17:19:29.072963Z","shell.execute_reply":"2023-01-30T17:19:29.071272Z"},"papermill":{"duration":0.085929,"end_time":"2023-01-30T17:19:29.075916","exception":false,"start_time":"2023-01-30T17:19:28.989987","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nembeds = model(desc)","metadata":{"execution":{"iopub.execute_input":"2023-01-30T17:19:29.087343Z","iopub.status.busy":"2023-01-30T17:19:29.086856Z","iopub.status.idle":"2023-01-30T17:19:36.441296Z","shell.execute_reply":"2023-01-30T17:19:36.440448Z"},"papermill":{"duration":7.364196,"end_time":"2023-01-30T17:19:36.444743","exception":false,"start_time":"2023-01-30T17:19:29.080547","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"desc_embedding_pairs = {i:embed.numpy() for i, embed in zip(desc, embeds)}","metadata":{"execution":{"iopub.execute_input":"2023-01-30T17:19:36.456361Z","iopub.status.busy":"2023-01-30T17:19:36.455814Z","iopub.status.idle":"2023-01-30T17:19:41.945151Z","shell.execute_reply":"2023-01-30T17:19:41.943501Z"},"papermill":{"duration":5.498446,"end_time":"2023-01-30T17:19:41.948294","exception":false,"start_time":"2023-01-30T17:19:36.449848","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df[['article_id', 'detail_desc']]","metadata":{"execution":{"iopub.execute_input":"2023-01-30T17:19:41.961153Z","iopub.status.busy":"2023-01-30T17:19:41.959870Z","iopub.status.idle":"2023-01-30T17:19:42.075465Z","shell.execute_reply":"2023-01-30T17:19:42.073486Z"},"papermill":{"duration":0.125404,"end_time":"2023-01-30T17:19:42.078834","exception":false,"start_time":"2023-01-30T17:19:41.953430","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['embeddings'] = df.detail_desc.map(desc_embedding_pairs)","metadata":{"execution":{"iopub.execute_input":"2023-01-30T17:19:42.089878Z","iopub.status.busy":"2023-01-30T17:19:42.089432Z","iopub.status.idle":"2023-01-30T17:19:42.158420Z","shell.execute_reply":"2023-01-30T17:19:42.156613Z"},"papermill":{"duration":0.077721,"end_time":"2023-01-30T17:19:42.161187","exception":false,"start_time":"2023-01-30T17:19:42.083466","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df[['article_id', 'embeddings']]","metadata":{"execution":{"iopub.execute_input":"2023-01-30T17:19:42.171721Z","iopub.status.busy":"2023-01-30T17:19:42.171332Z","iopub.status.idle":"2023-01-30T17:19:42.192879Z","shell.execute_reply":"2023-01-30T17:19:42.191904Z"},"papermill":{"duration":0.029407,"end_time":"2023-01-30T17:19:42.195028","exception":false,"start_time":"2023-01-30T17:19:42.165621","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f = open('article_embeddings_from_text.pickle', 'wb')\npickle.dump(df.values, f)","metadata":{"execution":{"iopub.execute_input":"2023-01-30T17:19:42.205654Z","iopub.status.busy":"2023-01-30T17:19:42.205178Z","iopub.status.idle":"2023-01-30T17:19:42.687938Z","shell.execute_reply":"2023-01-30T17:19:42.686382Z"},"papermill":{"duration":0.491031,"end_time":"2023-01-30T17:19:42.690560","exception":false,"start_time":"2023-01-30T17:19:42.199529","status":"completed"},"tags":[]},"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.execute_input":"2023-01-30T17:19:42.701223Z","iopub.status.busy":"2023-01-30T17:19:42.700758Z","iopub.status.idle":"2023-01-30T17:21:20.224512Z","shell.execute_reply":"2023-01-30T17:21:20.222602Z"},"papermill":{"duration":97.533158,"end_time":"2023-01-30T17:21:20.228029","exception":false,"start_time":"2023-01-30T17:19:42.694871","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans = trans.merge(df, on='article_id').drop('article_id', axis=1)","metadata":{"execution":{"iopub.execute_input":"2023-01-30T17:21:20.239627Z","iopub.status.busy":"2023-01-30T17:21:20.239038Z","iopub.status.idle":"2023-01-30T17:21:50.293027Z","shell.execute_reply":"2023-01-30T17:21:50.291563Z"},"papermill":{"duration":30.06332,"end_time":"2023-01-30T17:21:50.296139","exception":false,"start_time":"2023-01-30T17:21:20.232819","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ncustomer_embeddings = trans.groupby('customer_id').mean()","metadata":{"execution":{"iopub.execute_input":"2023-01-30T17:21:50.307976Z","iopub.status.busy":"2023-01-30T17:21:50.307262Z","iopub.status.idle":"2023-01-30T17:26:36.253466Z","shell.execute_reply":"2023-01-30T17:26:36.249074Z"},"papermill":{"duration":285.969741,"end_time":"2023-01-30T17:26:36.270633","exception":false,"start_time":"2023-01-30T17:21:50.300892","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f = open('customer_embeddings_from_text.pickle', 'wb')","metadata":{"execution":{"iopub.execute_input":"2023-01-30T17:26:36.288369Z","iopub.status.busy":"2023-01-30T17:26:36.287350Z","iopub.status.idle":"2023-01-30T17:26:36.303006Z","shell.execute_reply":"2023-01-30T17:26:36.301060Z"},"papermill":{"duration":0.03208,"end_time":"2023-01-30T17:26:36.307283","exception":false,"start_time":"2023-01-30T17:26:36.275203","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pickle.dump(customer_embeddings, f)","metadata":{"execution":{"iopub.execute_input":"2023-01-30T17:26:36.318666Z","iopub.status.busy":"2023-01-30T17:26:36.317969Z","iopub.status.idle":"2023-01-30T17:26:53.853557Z","shell.execute_reply":"2023-01-30T17:26:53.851619Z"},"papermill":{"duration":17.545088,"end_time":"2023-01-30T17:26:53.857037","exception":false,"start_time":"2023-01-30T17:26:36.311949","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}