{"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 norebook compares 4 different approaches for recommendations each uses embedding of customers and article generated in the following ways:**\n\n1. Embeddings from products images\n1. Embeddings from product text discription\n1. Embeddings from prodcut features\n1. Embeddings from collaborative filltering model build with TFRS\n\nCustomer embeddings are generated by averaging the embeddings of articles in customer purchase history\n\nSimilar items are found using KNN classifier from sklearn and models ares saved in pickle files in data directory\n\nSimilarity metric used is dot product (1 - cosine_similarity) and scores are shown over each article image\n\n","metadata":{"execution":{"iopub.status.busy":"2022-05-20T05:10:38.730992Z","iopub.execute_input":"2022-05-20T05:10:38.731527Z","iopub.status.idle":"2022-05-20T05:10:46.446879Z","shell.execute_reply.started":"2022-05-20T05:10:38.731485Z","shell.execute_reply":"2022-05-20T05:10:46.445999Z"}}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport pickle\nimport warnings\nfrom sklearn.neighbors import KNeighborsClassifier as KNN\nfrom sklearn.preprocessing import MinMaxScaler\n\nwarnings.filterwarnings('ignore')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-07T14:19:52.205431Z","iopub.execute_input":"2023-05-07T14:19:52.206016Z","iopub.status.idle":"2023-05-07T14:19:53.265381Z","shell.execute_reply.started":"2023-05-07T14:19:52.205910Z","shell.execute_reply":"2023-05-07T14:19:53.264404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"everthing = pd.read_csv('/kaggle/input/hm-embeddings-4-different-approaches/everthing.csv')\ncustomer_all = pd.read_csv('/kaggle/input/hm-embeddings-4-different-approaches/customer_all.csv')\ncustomers_history = pd.read_csv('/kaggle/input/hm-data-transformation/customer_sequence.csv').set_index('Unnamed: 0')","metadata":{"execution":{"iopub.status.busy":"2023-05-07T14:19:53.267106Z","iopub.execute_input":"2023-05-07T14:19:53.267398Z","iopub.status.idle":"2023-05-07T14:21:31.464122Z","shell.execute_reply.started":"2023-05-07T14:19:53.267369Z","shell.execute_reply":"2023-05-07T14:21:31.462965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tfrs_knn = pickle.load(open ('/kaggle/input/hm-embeddings-4-different-approaches/tfrs_knn.pickle','rb' ))\nimage_knn = pickle.load(open('/kaggle/input/hm-embeddings-4-different-approaches/image_knn.pickle', 'rb'))\ntext_knn = pickle.load(open('/kaggle/input/hm-embeddings-4-different-approaches/text_knn.pickle', 'rb'))\nfeature_knn = pickle.load(open('/kaggle/input/hm-embeddings-4-different-approaches/feature_knn.pickle', 'rb'))\nall_knn = pickle.load(open('/kaggle/input/hm-embeddings-4-different-approaches/all_knn.pickle', 'rb'))","metadata":{"execution":{"iopub.status.busy":"2023-05-07T14:21:31.465372Z","iopub.execute_input":"2023-05-07T14:21:31.465708Z","iopub.status.idle":"2023-05-07T14:22:06.580185Z","shell.execute_reply.started":"2023-05-07T14:21:31.465677Z","shell.execute_reply":"2023-05-07T14:22:06.579042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\ndef plot_items(items, scores=['None']):\n    path = \"/kaggle/input/h-and-m-personalized-fashion-recommendations/images\"\n\n    k = len(items)\n    fig = plt.figure(figsize=(2*k, 10))\n    for item, i, score in zip(items, range(1, k+1), scores):\n        item = '0'+ str(item)\n        sub = item[:3]\n        image = path + \"/\"+ sub + \"/\"+ item +\".jpg\"\n        image = plt.imread(image)\n        fig.add_subplot(1, k, i)\n        plt.axis('off')\n        plt.title(score)\n        plt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2023-05-07T14:22:06.582600Z","iopub.execute_input":"2023-05-07T14:22:06.582916Z","iopub.status.idle":"2023-05-07T14:22:06.590652Z","shell.execute_reply.started":"2023-05-07T14:22:06.582887Z","shell.execute_reply":"2023-05-07T14:22:06.589422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_rand_article(article_id):\n    \n    article_mask = everthing.article_id == article_id\n    \n    combined_article = everthing[article_mask].values[0][1:]\n    article_tfrs = everthing[article_mask].filter(regex='^tfrs',axis=1).values\n    article_image = everthing[article_mask].filter(regex='^image',axis=1).values\n    article_text = everthing[article_mask].filter(regex='^text',axis=1).values\n    article_feature = everthing[article_mask].filter(regex='^feature',axis=1).values\n\n    return combined_article, article_tfrs, article_image, article_text, article_feature","metadata":{"execution":{"iopub.status.busy":"2023-05-07T14:22:06.592188Z","iopub.execute_input":"2023-05-07T14:22:06.592595Z","iopub.status.idle":"2023-05-07T14:22:06.601472Z","shell.execute_reply.started":"2023-05-07T14:22:06.592552Z","shell.execute_reply":"2023-05-07T14:22:06.600545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_rand_customer(customer):\n        \n    customer = customer_all[customer_all.customer_id == customer].drop('customer_id', axis=1)\n    \n    customer_tfrs = customer.filter(regex='^tfrs',axis=1)\n    customer_image = customer.filter(regex='^image',axis=1)\n    customer_text = customer.filter(regex='^text',axis=1)\n    customer_feature = customer.filter(regex='^feature',axis=1)\n    \n    return customer.values[0], customer_tfrs, customer_image, customer_text, customer_feature","metadata":{"execution":{"iopub.status.busy":"2023-05-07T14:22:06.602873Z","iopub.execute_input":"2023-05-07T14:22:06.603451Z","iopub.status.idle":"2023-05-07T14:22:06.616969Z","shell.execute_reply.started":"2023-05-07T14:22:06.603408Z","shell.execute_reply":"2023-05-07T14:22:06.615901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_new_customer(new_customer_history):\n    \n    new_customer_embeddings = pd.DataFrame(everthing[everthing.article_id.isin(new_customer_history)].mean()).T\n    \n    new_tfrs = new_customer_embeddings.filter(regex='^tfrs')\n    new_image = new_customer_embeddings.filter(regex='^image')\n    new_text = new_customer_embeddings.filter(regex='^text')\n    new_feature = new_customer_embeddings.filter(regex='^feature')\n    \n    return new_customer_embeddings.values[0], new_tfrs, new_image, new_text, new_feature","metadata":{"execution":{"iopub.status.busy":"2023-05-07T14:22:06.618697Z","iopub.execute_input":"2023-05-07T14:22:06.619067Z","iopub.status.idle":"2023-05-07T14:22:06.628037Z","shell.execute_reply.started":"2023-05-07T14:22:06.619026Z","shell.execute_reply":"2023-05-07T14:22:06.627233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_rcmnds(combined, tfrs, image, text, feature ,k=8):\n    \n    combined = all_knn.kneighbors([combined], k)\n    combined_rcmnds, combined_scores = everthing.iloc[combined[1][0]].article_id.values, np.round(1- combined[0][0], 2)\n    \n    tfrs = tfrs_knn.kneighbors(tfrs, k)\n    tfrs_rcmnds, tfrs_scores = everthing.iloc[tfrs[1][0]].article_id.values, np.round(1- tfrs[0][0], 2)\n\n    image = image_knn.kneighbors(image, k)\n    image_rcmnds, image_scores = everthing.iloc[image[1][0]].article_id.values, np.round(1- image[0][0], 2)\n\n    text = text_knn.kneighbors(text, k)\n    text_rcmnds, text_scores = everthing.iloc[text[1][0]].article_id.values, np.round(1- text[0][0], 2)\n\n    feature = feature_knn.kneighbors(feature, k)\n    feature_rcmnds, feature_scores = everthing.iloc[feature[1][0]].article_id.values, np.round(1- feature[0][0], 2)\n    \n    \n    return (combined_rcmnds, combined_scores), (tfrs_rcmnds, tfrs_scores), (image_rcmnds, image_scores), (text_rcmnds, text_scores), (feature_rcmnds, feature_scores)","metadata":{"execution":{"iopub.status.busy":"2023-05-07T14:22:06.629145Z","iopub.execute_input":"2023-05-07T14:22:06.629840Z","iopub.status.idle":"2023-05-07T14:22:06.639890Z","shell.execute_reply.started":"2023-05-07T14:22:06.629806Z","shell.execute_reply":"2023-05-07T14:22:06.638964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Finding Similar Items given an article","metadata":{}},{"cell_type":"code","source":"article_id = everthing.sample(1).article_id.values[0]\n\ncombined_article, article_tfrs, article_image, article_text, article_feature = get_rand_article(article_id)\n\n(combined_rcmnds, combined_scores), (image_rcmnds, image_scores), (tfrs_rcmnds, tfrs_scores), (text_rcmnds, text_scores), (feature_rcmnds, feature_scores) = get_rcmnds(combined_article, article_tfrs, article_image, article_text, article_feature)","metadata":{"execution":{"iopub.status.busy":"2023-05-07T14:22:06.641090Z","iopub.execute_input":"2023-05-07T14:22:06.641583Z","iopub.status.idle":"2023-05-07T14:22:10.344863Z","shell.execute_reply.started":"2023-05-07T14:22:06.641552Z","shell.execute_reply":"2023-05-07T14:22:10.343623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items([article_id], ['Item'])","metadata":{"execution":{"iopub.status.busy":"2023-05-07T14:22:10.350568Z","iopub.execute_input":"2023-05-07T14:22:10.351345Z","iopub.status.idle":"2023-05-07T14:22:10.796758Z","shell.execute_reply.started":"2023-05-07T14:22:10.351294Z","shell.execute_reply":"2023-05-07T14:22:10.795691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(combined_rcmnds, combined_scores)","metadata":{"execution":{"iopub.status.busy":"2023-05-07T14:22:10.798184Z","iopub.execute_input":"2023-05-07T14:22:10.798507Z","iopub.status.idle":"2023-05-07T14:22:13.628019Z","shell.execute_reply.started":"2023-05-07T14:22:10.798476Z","shell.execute_reply":"2023-05-07T14:22:13.627187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(image_rcmnds, image_scores)","metadata":{"execution":{"iopub.status.busy":"2023-05-07T14:22:13.628901Z","iopub.execute_input":"2023-05-07T14:22:13.629221Z","iopub.status.idle":"2023-05-07T14:22:16.438021Z","shell.execute_reply.started":"2023-05-07T14:22:13.629191Z","shell.execute_reply":"2023-05-07T14:22:16.437172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(tfrs_rcmnds, tfrs_scores)","metadata":{"execution":{"iopub.status.busy":"2023-05-07T14:22:16.439362Z","iopub.execute_input":"2023-05-07T14:22:16.439880Z","iopub.status.idle":"2023-05-07T14:22:19.740191Z","shell.execute_reply.started":"2023-05-07T14:22:16.439845Z","shell.execute_reply":"2023-05-07T14:22:19.739368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(text_rcmnds, text_scores)","metadata":{"execution":{"iopub.status.busy":"2023-05-07T14:22:19.741573Z","iopub.execute_input":"2023-05-07T14:22:19.742130Z","iopub.status.idle":"2023-05-07T14:22:22.565887Z","shell.execute_reply.started":"2023-05-07T14:22:19.742094Z","shell.execute_reply":"2023-05-07T14:22:22.564758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(feature_rcmnds, feature_scores)","metadata":{"execution":{"iopub.status.busy":"2023-05-07T14:22:22.567484Z","iopub.execute_input":"2023-05-07T14:22:22.568042Z","iopub.status.idle":"2023-05-07T14:22:25.416472Z","shell.execute_reply.started":"2023-05-07T14:22:22.567975Z","shell.execute_reply":"2023-05-07T14:22:25.415369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Recommending items for a customer","metadata":{}},{"cell_type":"code","source":"customer = customer_all.sample(1).customer_id.values[0]\ncustomer_history = eval(customers_history[customers_history.customer == customer].sequence.values[0])\ncustomer_history = [int(i) for i in customer_history]\n\ncombined_customer, customer_tfrs, customer_image, customer_text, customer_feature = get_rand_customer(customer)\n(combined_rcmnds, combined_scores), (image_rcmnds, image_scores), (tfrs_rcmnds, tfrs_scores), (text_rcmnds, text_scores), (feature_rcmnds, feature_scores) = get_rcmnds(combined_customer, customer_tfrs, customer_image, customer_text, customer_feature)","metadata":{"execution":{"iopub.status.busy":"2023-05-07T14:22:25.418103Z","iopub.execute_input":"2023-05-07T14:22:25.418842Z","iopub.status.idle":"2023-05-07T14:22:29.107584Z","shell.execute_reply.started":"2023-05-07T14:22:25.418800Z","shell.execute_reply":"2023-05-07T14:22:29.106188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(customer_history[:8], range(8))","metadata":{"execution":{"iopub.status.busy":"2023-05-07T14:22:29.109401Z","iopub.execute_input":"2023-05-07T14:22:29.110126Z","iopub.status.idle":"2023-05-07T14:22:32.128127Z","shell.execute_reply.started":"2023-05-07T14:22:29.110079Z","shell.execute_reply":"2023-05-07T14:22:32.127081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(combined_rcmnds, combined_scores)","metadata":{"execution":{"iopub.status.busy":"2023-05-07T14:22:32.129487Z","iopub.execute_input":"2023-05-07T14:22:32.129793Z","iopub.status.idle":"2023-05-07T14:22:35.228923Z","shell.execute_reply.started":"2023-05-07T14:22:32.129763Z","shell.execute_reply":"2023-05-07T14:22:35.227796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(image_rcmnds,image_scores)","metadata":{"execution":{"iopub.status.busy":"2023-05-07T14:22:35.230412Z","iopub.execute_input":"2023-05-07T14:22:35.231183Z","iopub.status.idle":"2023-05-07T14:22:38.150018Z","shell.execute_reply.started":"2023-05-07T14:22:35.231148Z","shell.execute_reply":"2023-05-07T14:22:38.148698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(tfrs_rcmnds, text_scores)","metadata":{"execution":{"iopub.status.busy":"2023-05-07T14:22:38.151698Z","iopub.execute_input":"2023-05-07T14:22:38.152087Z","iopub.status.idle":"2023-05-07T14:22:41.241682Z","shell.execute_reply.started":"2023-05-07T14:22:38.152028Z","shell.execute_reply":"2023-05-07T14:22:41.240532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(text_rcmnds, text_scores)","metadata":{"execution":{"iopub.status.busy":"2023-05-07T14:22:41.243302Z","iopub.execute_input":"2023-05-07T14:22:41.244355Z","iopub.status.idle":"2023-05-07T14:22:43.932496Z","shell.execute_reply.started":"2023-05-07T14:22:41.244312Z","shell.execute_reply":"2023-05-07T14:22:43.931284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(feature_rcmnds, feature_scores)","metadata":{"execution":{"iopub.status.busy":"2023-05-07T14:22:43.934144Z","iopub.execute_input":"2023-05-07T14:22:43.934510Z","iopub.status.idle":"2023-05-07T14:22:46.936912Z","shell.execute_reply.started":"2023-05-07T14:22:43.934476Z","shell.execute_reply":"2023-05-07T14:22:46.935796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Recommendations for newly generated customer","metadata":{}},{"cell_type":"code","source":"new_customer_history = np.random.choice(everthing.article_id.values, size=6, replace=False)\n\ncombined_customer, customer_tfrs, customer_image, customer_text, customer = get_new_customer(new_customer_history)\n(combined_rcmnds, combined_scores), (image_rcmnds, image_scores), (tfrs_rcmnds, tfrs_scores), (text_rcmnds, text_scores), (feature_rcmnds, feature_scores) = get_rcmnds(combined_customer[1:], customer_tfrs, customer_image, customer_text, customer_feature)","metadata":{"execution":{"iopub.status.busy":"2023-05-07T14:22:46.938460Z","iopub.execute_input":"2023-05-07T14:22:46.938799Z","iopub.status.idle":"2023-05-07T14:22:50.531902Z","shell.execute_reply.started":"2023-05-07T14:22:46.938765Z","shell.execute_reply":"2023-05-07T14:22:50.530551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(new_customer_history[:8], range(8))","metadata":{"execution":{"iopub.status.busy":"2023-05-07T14:22:50.533914Z","iopub.execute_input":"2023-05-07T14:22:50.534392Z","iopub.status.idle":"2023-05-07T14:22:52.836265Z","shell.execute_reply.started":"2023-05-07T14:22:50.534347Z","shell.execute_reply":"2023-05-07T14:22:52.835216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(combined_rcmnds, combined_scores)","metadata":{"execution":{"iopub.status.busy":"2023-05-07T14:22:52.837802Z","iopub.execute_input":"2023-05-07T14:22:52.838148Z","iopub.status.idle":"2023-05-07T14:22:55.805656Z","shell.execute_reply.started":"2023-05-07T14:22:52.838115Z","shell.execute_reply":"2023-05-07T14:22:55.804466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(image_rcmnds,image_scores)","metadata":{"execution":{"iopub.status.busy":"2023-05-07T14:22:55.807623Z","iopub.execute_input":"2023-05-07T14:22:55.808041Z","iopub.status.idle":"2023-05-07T14:22:58.692247Z","shell.execute_reply.started":"2023-05-07T14:22:55.807985Z","shell.execute_reply":"2023-05-07T14:22:58.691079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(tfrs_rcmnds, text_scores)","metadata":{"execution":{"iopub.status.busy":"2023-05-07T14:22:58.693460Z","iopub.execute_input":"2023-05-07T14:22:58.693798Z","iopub.status.idle":"2023-05-07T14:23:01.498353Z","shell.execute_reply.started":"2023-05-07T14:22:58.693766Z","shell.execute_reply":"2023-05-07T14:23:01.497169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(text_rcmnds, text_scores)","metadata":{"execution":{"iopub.status.busy":"2023-05-07T14:23:01.504196Z","iopub.execute_input":"2023-05-07T14:23:01.505102Z","iopub.status.idle":"2023-05-07T14:23:04.521979Z","shell.execute_reply.started":"2023-05-07T14:23:01.505063Z","shell.execute_reply":"2023-05-07T14:23:04.520836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(feature_rcmnds, feature_scores)","metadata":{"execution":{"iopub.status.busy":"2023-05-07T14:23:04.523409Z","iopub.execute_input":"2023-05-07T14:23:04.523718Z","iopub.status.idle":"2023-05-07T14:23:07.244849Z","shell.execute_reply.started":"2023-05-07T14:23:04.523688Z","shell.execute_reply":"2023-05-07T14:23:07.243757Z"},"trusted":true},"execution_count":null,"outputs":[]}]}