{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.10","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":31254,"databundleVersionId":3103714,"sourceType":"competition"}],"dockerImageVersionId":30474,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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-08-15T19:54:55.898432Z","iopub.execute_input":"2023-08-15T19:54:55.899309Z","iopub.status.idle":"2023-08-15T19:54:57.590336Z","shell.execute_reply.started":"2023-08-15T19:54:55.899252Z","shell.execute_reply":"2023-08-15T19:54:57.589064Z"},"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":"2023-08-15T19:54:57.592372Z","iopub.execute_input":"2023-08-15T19:54:57.59273Z","iopub.status.idle":"2023-08-15T19:54:58.636045Z","shell.execute_reply.started":"2023-08-15T19:54:57.592699Z","shell.execute_reply":"2023-08-15T19:54:58.631878Z"},"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":"2023-08-15T19:54:58.636974Z","iopub.status.idle":"2023-08-15T19:54:58.637383Z","shell.execute_reply.started":"2023-08-15T19:54:58.637189Z","shell.execute_reply":"2023-08-15T19:54:58.637207Z"},"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":"2023-08-15T19:54:58.639216Z","iopub.status.idle":"2023-08-15T19:54:58.639669Z","shell.execute_reply.started":"2023-08-15T19:54:58.639462Z","shell.execute_reply":"2023-08-15T19:54:58.639482Z"},"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-08-15T19:54:58.641123Z","iopub.status.idle":"2023-08-15T19:54:58.641531Z","shell.execute_reply.started":"2023-08-15T19:54:58.641335Z","shell.execute_reply":"2023-08-15T19:54:58.641355Z"},"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-08-15T19:54:58.643495Z","iopub.status.idle":"2023-08-15T19:54:58.644153Z","shell.execute_reply.started":"2023-08-15T19:54:58.643929Z","shell.execute_reply":"2023-08-15T19:54:58.643958Z"},"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-08-15T19:54:58.645282Z","iopub.status.idle":"2023-08-15T19:54:58.645968Z","shell.execute_reply.started":"2023-08-15T19:54:58.645707Z","shell.execute_reply":"2023-08-15T19:54:58.645738Z"},"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-08-15T19:54:58.647171Z","iopub.status.idle":"2023-08-15T19:54:58.647834Z","shell.execute_reply.started":"2023-08-15T19:54:58.64759Z","shell.execute_reply":"2023-08-15T19:54:58.647631Z"},"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-08-15T19:54:58.649044Z","iopub.status.idle":"2023-08-15T19:54:58.649701Z","shell.execute_reply.started":"2023-08-15T19:54:58.649471Z","shell.execute_reply":"2023-08-15T19:54:58.649501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items([article_id], ['Item'])","metadata":{"execution":{"iopub.status.busy":"2023-08-15T19:54:58.650944Z","iopub.status.idle":"2023-08-15T19:54:58.651567Z","shell.execute_reply.started":"2023-08-15T19:54:58.651359Z","shell.execute_reply":"2023-08-15T19:54:58.651384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(combined_rcmnds, combined_scores)","metadata":{"execution":{"iopub.status.busy":"2023-08-15T19:54:58.652928Z","iopub.status.idle":"2023-08-15T19:54:58.653576Z","shell.execute_reply.started":"2023-08-15T19:54:58.653363Z","shell.execute_reply":"2023-08-15T19:54:58.653389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(image_rcmnds, image_scores)","metadata":{"execution":{"iopub.status.busy":"2023-08-15T19:54:58.65472Z","iopub.status.idle":"2023-08-15T19:54:58.655157Z","shell.execute_reply.started":"2023-08-15T19:54:58.65495Z","shell.execute_reply":"2023-08-15T19:54:58.65497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(tfrs_rcmnds, tfrs_scores)","metadata":{"execution":{"iopub.status.busy":"2023-08-15T19:54:58.656382Z","iopub.status.idle":"2023-08-15T19:54:58.657179Z","shell.execute_reply.started":"2023-08-15T19:54:58.656929Z","shell.execute_reply":"2023-08-15T19:54:58.65696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(text_rcmnds, text_scores)","metadata":{"execution":{"iopub.status.busy":"2023-08-15T19:54:58.659039Z","iopub.status.idle":"2023-08-15T19:54:58.659491Z","shell.execute_reply.started":"2023-08-15T19:54:58.659261Z","shell.execute_reply":"2023-08-15T19:54:58.659282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(feature_rcmnds, feature_scores)","metadata":{"execution":{"iopub.status.busy":"2023-08-15T19:54:58.660896Z","iopub.status.idle":"2023-08-15T19:54:58.661296Z","shell.execute_reply.started":"2023-08-15T19:54:58.661091Z","shell.execute_reply":"2023-08-15T19:54:58.661109Z"},"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-08-15T19:54:58.662323Z","iopub.status.idle":"2023-08-15T19:54:58.662682Z","shell.execute_reply.started":"2023-08-15T19:54:58.662502Z","shell.execute_reply":"2023-08-15T19:54:58.662519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(customer_history[:8], range(8))","metadata":{"execution":{"iopub.status.busy":"2023-08-15T19:54:58.663969Z","iopub.status.idle":"2023-08-15T19:54:58.664354Z","shell.execute_reply.started":"2023-08-15T19:54:58.664166Z","shell.execute_reply":"2023-08-15T19:54:58.664184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(combined_rcmnds, combined_scores)","metadata":{"execution":{"iopub.status.busy":"2023-08-15T19:54:58.666147Z","iopub.status.idle":"2023-08-15T19:54:58.666795Z","shell.execute_reply.started":"2023-08-15T19:54:58.666592Z","shell.execute_reply":"2023-08-15T19:54:58.666614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(image_rcmnds,image_scores)","metadata":{"execution":{"iopub.status.busy":"2023-08-15T19:54:58.668296Z","iopub.status.idle":"2023-08-15T19:54:58.668669Z","shell.execute_reply.started":"2023-08-15T19:54:58.668476Z","shell.execute_reply":"2023-08-15T19:54:58.668493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(tfrs_rcmnds, text_scores)","metadata":{"execution":{"iopub.status.busy":"2023-08-15T19:54:58.670122Z","iopub.status.idle":"2023-08-15T19:54:58.672648Z","shell.execute_reply.started":"2023-08-15T19:54:58.672414Z","shell.execute_reply":"2023-08-15T19:54:58.672437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(text_rcmnds, text_scores)","metadata":{"execution":{"iopub.status.busy":"2023-08-15T19:54:58.673675Z","iopub.status.idle":"2023-08-15T19:54:58.674083Z","shell.execute_reply.started":"2023-08-15T19:54:58.673889Z","shell.execute_reply":"2023-08-15T19:54:58.673908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(feature_rcmnds, feature_scores)","metadata":{"execution":{"iopub.status.busy":"2023-08-15T19:54:58.675049Z","iopub.status.idle":"2023-08-15T19:54:58.675429Z","shell.execute_reply.started":"2023-08-15T19:54:58.675224Z","shell.execute_reply":"2023-08-15T19:54:58.67525Z"},"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-08-15T19:54:58.677006Z","iopub.status.idle":"2023-08-15T19:54:58.677377Z","shell.execute_reply.started":"2023-08-15T19:54:58.677189Z","shell.execute_reply":"2023-08-15T19:54:58.677206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(new_customer_history[:8], range(8))","metadata":{"execution":{"iopub.status.busy":"2023-08-15T19:54:58.678414Z","iopub.status.idle":"2023-08-15T19:54:58.678789Z","shell.execute_reply.started":"2023-08-15T19:54:58.67859Z","shell.execute_reply":"2023-08-15T19:54:58.678615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(combined_rcmnds, combined_scores)","metadata":{"execution":{"iopub.status.busy":"2023-08-15T19:54:58.679726Z","iopub.status.idle":"2023-08-15T19:54:58.680546Z","shell.execute_reply.started":"2023-08-15T19:54:58.680345Z","shell.execute_reply":"2023-08-15T19:54:58.680365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(image_rcmnds,image_scores)","metadata":{"execution":{"iopub.status.busy":"2023-08-15T19:54:58.681686Z","iopub.status.idle":"2023-08-15T19:54:58.682062Z","shell.execute_reply.started":"2023-08-15T19:54:58.681878Z","shell.execute_reply":"2023-08-15T19:54:58.681896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(tfrs_rcmnds, text_scores)","metadata":{"execution":{"iopub.status.busy":"2023-08-15T19:54:58.683013Z","iopub.status.idle":"2023-08-15T19:54:58.683371Z","shell.execute_reply.started":"2023-08-15T19:54:58.683189Z","shell.execute_reply":"2023-08-15T19:54:58.683206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(text_rcmnds, text_scores)","metadata":{"execution":{"iopub.status.busy":"2023-08-15T19:54:58.684445Z","iopub.status.idle":"2023-08-15T19:54:58.684816Z","shell.execute_reply.started":"2023-08-15T19:54:58.684629Z","shell.execute_reply":"2023-08-15T19:54:58.684647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_items(feature_rcmnds, feature_scores)","metadata":{"execution":{"iopub.status.busy":"2023-08-15T19:54:58.685888Z","iopub.status.idle":"2023-08-15T19:54:58.686249Z","shell.execute_reply.started":"2023-08-15T19:54:58.686064Z","shell.execute_reply":"2023-08-15T19:54:58.686081Z"},"trusted":true},"execution_count":null,"outputs":[]}]}