{"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":"2022-05-20T08:10:40.550693Z","iopub.execute_input":"2022-05-20T08:10:40.551025Z","iopub.status.idle":"2022-05-20T08:10:40.556427Z","shell.execute_reply.started":"2022-05-20T08:10:40.550988Z","shell.execute_reply":"2022-05-20T08:10:40.555507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"everthing = pd.read_csv('../input/hm-embeddings-4-different-approaches/everthing.csv')\ncustomer_all = pd.read_csv('../input/hm-embeddings-4-different-approaches/customer_all.csv')\ncustomers_history = pd.read_csv('../input/hm-data-transformation/customer_sequence.csv').set_index('Unnamed: 0')","metadata":{"execution":{"iopub.status.busy":"2022-05-20T08:10:47.756486Z","iopub.execute_input":"2022-05-20T08:10:47.757058Z","iopub.status.idle":"2022-05-20T08:11:50.328723Z","shell.execute_reply.started":"2022-05-20T08:10:47.757023Z","shell.execute_reply":"2022-05-20T08:11:50.327960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tfrs_knn = pickle.load(open('../input/hm-embeddings-4-different-approaches/tfrs_knn.pickle', 'rb' ))\nimage_knn = pickle.load(open('../input/hm-embeddings-4-different-approaches/image_knn.pickle', 'rb'))\ntext_knn = pickle.load(open('../input/hm-embeddings-4-different-approaches/text_knn.pickle', 'rb'))\nfeature_knn = pickle.load(open('../input/hm-embeddings-4-different-approaches/feature_knn.pickle', 'rb'))\nall_knn = pickle.load(open('../input/hm-embeddings-4-different-approaches/all_knn.pickle', 'rb'))","metadata":{"execution":{"iopub.status.busy":"2022-05-20T08:11:59.290142Z","iopub.execute_input":"2022-05-20T08:11:59.290471Z","iopub.status.idle":"2022-05-20T08:12:13.313308Z","shell.execute_reply.started":"2022-05-20T08:11:59.290435Z","shell.execute_reply":"2022-05-20T08:12:13.312637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\ndef plot_items(items, scores=['None']):\n    path = \"../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":"2022-05-20T08:12:28.157079Z","iopub.execute_input":"2022-05-20T08:12:28.157545Z","iopub.status.idle":"2022-05-20T08:12:28.165193Z","shell.execute_reply.started":"2022-05-20T08:12:28.157493Z","shell.execute_reply":"2022-05-20T08:12:28.163960Z"},"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":"2022-05-20T08:46:22.633995Z","iopub.execute_input":"2022-05-20T08:46:22.634285Z","iopub.status.idle":"2022-05-20T08:46:22.641022Z","shell.execute_reply.started":"2022-05-20T08:46:22.634255Z","shell.execute_reply":"2022-05-20T08:46:22.640300Z"},"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":"2022-05-20T08:56:06.011674Z","iopub.execute_input":"2022-05-20T08:56:06.011969Z","iopub.status.idle":"2022-05-20T08:56:06.019494Z","shell.execute_reply.started":"2022-05-20T08:56:06.011939Z","shell.execute_reply":"2022-05-20T08:56:06.018658Z"},"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":"2022-05-20T09:02:33.200697Z","iopub.execute_input":"2022-05-20T09:02:33.201723Z","iopub.status.idle":"2022-05-20T09:02:33.208424Z","shell.execute_reply.started":"2022-05-20T09:02:33.201674Z","shell.execute_reply":"2022-05-20T09:02:33.207382Z"},"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":"2022-05-20T08:47:29.314992Z","iopub.execute_input":"2022-05-20T08:47:29.315306Z","iopub.status.idle":"2022-05-20T08:47:29.327432Z","shell.execute_reply.started":"2022-05-20T08:47:29.315276Z","shell.execute_reply":"2022-05-20T08:47:29.326306Z"},"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":"2022-05-20T09:19:05.075802Z","iopub.execute_input":"2022-05-20T09:19:05.076094Z","iopub.status.idle":"2022-05-20T09:19:08.953677Z","shell.execute_reply.started":"2022-05-20T09:19:05.076063Z","shell.execute_reply":"2022-05-20T09:19:08.952302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items([article_id], ['Item'])","metadata":{"execution":{"iopub.status.busy":"2022-05-20T09:19:09.603217Z","iopub.execute_input":"2022-05-20T09:19:09.603504Z","iopub.status.idle":"2022-05-20T09:19:09.915627Z","shell.execute_reply.started":"2022-05-20T09:19:09.603473Z","shell.execute_reply":"2022-05-20T09:19:09.914790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(combined_rcmnds, combined_scores)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T09:19:11.791922Z","iopub.execute_input":"2022-05-20T09:19:11.792215Z","iopub.status.idle":"2022-05-20T09:19:14.343866Z","shell.execute_reply.started":"2022-05-20T09:19:11.792181Z","shell.execute_reply":"2022-05-20T09:19:14.342966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(image_rcmnds, image_scores)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T09:19:18.982408Z","iopub.execute_input":"2022-05-20T09:19:18.982729Z","iopub.status.idle":"2022-05-20T09:19:21.300258Z","shell.execute_reply.started":"2022-05-20T09:19:18.982698Z","shell.execute_reply":"2022-05-20T09:19:21.299541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(tfrs_rcmnds, tfrs_scores)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T09:19:23.613233Z","iopub.execute_input":"2022-05-20T09:19:23.614158Z","iopub.status.idle":"2022-05-20T09:19:25.942028Z","shell.execute_reply.started":"2022-05-20T09:19:23.614108Z","shell.execute_reply":"2022-05-20T09:19:25.941130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(text_rcmnds, text_scores)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T09:19:28.264720Z","iopub.execute_input":"2022-05-20T09:19:28.265002Z","iopub.status.idle":"2022-05-20T09:19:30.798968Z","shell.execute_reply.started":"2022-05-20T09:19:28.264974Z","shell.execute_reply":"2022-05-20T09:19:30.798295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(feature_rcmnds, feature_scores)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T09:19:32.538976Z","iopub.execute_input":"2022-05-20T09:19:32.539488Z","iopub.status.idle":"2022-05-20T09:19:34.818333Z","shell.execute_reply.started":"2022-05-20T09:19:32.539453Z","shell.execute_reply":"2022-05-20T09:19:34.814813Z"},"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":"2022-05-20T09:21:26.896714Z","iopub.execute_input":"2022-05-20T09:21:26.897198Z","iopub.status.idle":"2022-05-20T09:21:31.032694Z","shell.execute_reply.started":"2022-05-20T09:21:26.897149Z","shell.execute_reply":"2022-05-20T09:21:31.031459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(customer_history[:8], range(8))","metadata":{"execution":{"iopub.status.busy":"2022-05-20T09:21:31.036524Z","iopub.execute_input":"2022-05-20T09:21:31.038671Z","iopub.status.idle":"2022-05-20T09:21:33.810098Z","shell.execute_reply.started":"2022-05-20T09:21:31.038601Z","shell.execute_reply":"2022-05-20T09:21:33.809320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(combined_rcmnds, combined_scores)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T09:21:36.962599Z","iopub.execute_input":"2022-05-20T09:21:36.963046Z","iopub.status.idle":"2022-05-20T09:21:39.479815Z","shell.execute_reply.started":"2022-05-20T09:21:36.963014Z","shell.execute_reply":"2022-05-20T09:21:39.478798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(image_rcmnds,image_scores)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T09:21:52.117975Z","iopub.execute_input":"2022-05-20T09:21:52.118798Z","iopub.status.idle":"2022-05-20T09:21:54.749156Z","shell.execute_reply.started":"2022-05-20T09:21:52.118753Z","shell.execute_reply":"2022-05-20T09:21:54.748220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(tfrs_rcmnds, text_scores)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T09:21:55.408928Z","iopub.execute_input":"2022-05-20T09:21:55.409228Z","iopub.status.idle":"2022-05-20T09:21:58.063345Z","shell.execute_reply.started":"2022-05-20T09:21:55.409197Z","shell.execute_reply":"2022-05-20T09:21:58.062344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(text_rcmnds, text_scores)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T09:22:00.259240Z","iopub.execute_input":"2022-05-20T09:22:00.259524Z","iopub.status.idle":"2022-05-20T09:22:02.789767Z","shell.execute_reply.started":"2022-05-20T09:22:00.259494Z","shell.execute_reply":"2022-05-20T09:22:02.788828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(feature_rcmnds, feature_scores)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T09:22:05.305354Z","iopub.execute_input":"2022-05-20T09:22:05.306305Z","iopub.status.idle":"2022-05-20T09:22:07.902402Z","shell.execute_reply.started":"2022-05-20T09:22:05.306260Z","shell.execute_reply":"2022-05-20T09:22:07.901447Z"},"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":"2022-05-20T09:22:24.725532Z","iopub.execute_input":"2022-05-20T09:22:24.725849Z","iopub.status.idle":"2022-05-20T09:22:28.684643Z","shell.execute_reply.started":"2022-05-20T09:22:24.725816Z","shell.execute_reply":"2022-05-20T09:22:28.683186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(new_customer_history[:8], range(8))","metadata":{"execution":{"iopub.status.busy":"2022-05-20T09:22:28.686776Z","iopub.execute_input":"2022-05-20T09:22:28.687456Z","iopub.status.idle":"2022-05-20T09:22:30.634698Z","shell.execute_reply.started":"2022-05-20T09:22:28.687402Z","shell.execute_reply":"2022-05-20T09:22:30.633635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(combined_rcmnds, combined_scores)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T09:22:30.636269Z","iopub.execute_input":"2022-05-20T09:22:30.636495Z","iopub.status.idle":"2022-05-20T09:22:33.220727Z","shell.execute_reply.started":"2022-05-20T09:22:30.636468Z","shell.execute_reply":"2022-05-20T09:22:33.219832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(image_rcmnds,image_scores)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T09:22:33.297826Z","iopub.execute_input":"2022-05-20T09:22:33.298157Z","iopub.status.idle":"2022-05-20T09:22:35.830207Z","shell.execute_reply.started":"2022-05-20T09:22:33.298121Z","shell.execute_reply":"2022-05-20T09:22:35.827726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(tfrs_rcmnds, text_scores)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T09:22:36.240523Z","iopub.execute_input":"2022-05-20T09:22:36.241104Z","iopub.status.idle":"2022-05-20T09:22:38.833734Z","shell.execute_reply.started":"2022-05-20T09:22:36.241063Z","shell.execute_reply":"2022-05-20T09:22:38.832696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(text_rcmnds, text_scores)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T09:22:38.835297Z","iopub.execute_input":"2022-05-20T09:22:38.835556Z","iopub.status.idle":"2022-05-20T09:22:41.358617Z","shell.execute_reply.started":"2022-05-20T09:22:38.835523Z","shell.execute_reply":"2022-05-20T09:22:41.357617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(feature_rcmnds, feature_scores)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T09:22:41.360676Z","iopub.execute_input":"2022-05-20T09:22:41.360991Z","iopub.status.idle":"2022-05-20T09:22:43.804986Z","shell.execute_reply.started":"2022-05-20T09:22:41.360947Z","shell.execute_reply":"2022-05-20T09:22:43.804275Z"},"trusted":true},"execution_count":null,"outputs":[]}]}