{"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":"# Building a recommender script using Turicreate library","metadata":{"execution":{"iopub.status.busy":"2022-04-27T12:32:30.535672Z","iopub.execute_input":"2022-04-27T12:32:30.536348Z","iopub.status.idle":"2022-04-27T12:33:20.830304Z","shell.execute_reply.started":"2022-04-27T12:32:30.536310Z","shell.execute_reply":"2022-04-27T12:33:20.829486Z"},"jupyter":{"outputs_hidden":true}}},{"cell_type":"markdown","source":"Turi Create is an open-source python library developed by Apple, used to create core machine learning models for supervised and unsupervised learning. The models are used for classification, object detection, style transfers, regression, and recommender system by simplifying the development of custom machine learning models.","metadata":{}},{"cell_type":"markdown","source":"In this notebook, for each user we find their frequency of purchase for each article id they had purchased in the past.\n\nThen, the top 10 articles are found for each user based on their frequency of purchase\n\nThis dataset is used to build the recommender system","metadata":{}},{"cell_type":"code","source":"!pip install turicreate --user","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport gc\n\nimport time\nimport turicreate as tc\nfrom sklearn.model_selection import train_test_split\n\nimport sys, warnings, time, os, copy, gc, re, random, pickle, cudf\nwarnings.filterwarnings('ignore')\nfrom IPython.display import display\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\n# pd.set_option('display.max_rows', 50)\n# pd.set_option('display.max_columns', None)\n# pd.set_option(\"display.max_colwidth\", 10000)\nimport seaborn as sns\nsns.set()\nfrom pandas.io.json import json_normalize\nfrom pprint import pprint\nfrom pathlib import Path\nfrom tqdm import tqdm\ntqdm.pandas()\nfrom collections import Counter\nfrom datetime import datetime, timedelta\n","metadata":{"execution":{"iopub.status.busy":"2022-04-27T12:33:20.833101Z","iopub.execute_input":"2022-04-27T12:33:20.833579Z","iopub.status.idle":"2022-04-27T12:33:24.399673Z","shell.execute_reply.started":"2022-04-27T12:33:20.833541Z","shell.execute_reply":"2022-04-27T12:33:24.398874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#articles = cudf.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/articles.csv\")\n#customers = cudf.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/customers.csv\")\ntransactions = cudf.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv\")\n#sample_sub = cudf.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-04-27T12:33:24.401607Z","iopub.execute_input":"2022-04-27T12:33:24.402017Z","iopub.status.idle":"2022-04-27T12:34:03.193344Z","shell.execute_reply.started":"2022-04-27T12:33:24.401982Z","shell.execute_reply":"2022-04-27T12:34:03.192494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Take recent 2 transactions only\n#transactions = transactions.sort_values(['customer_id','article_id','t_dat'],ascending=[1,1,0])\n#transactions = transactions[['customer_id','article_id']].to_pandas().groupby(['customer_id']).head(2)","metadata":{"execution":{"iopub.status.busy":"2022-04-27T12:34:03.195700Z","iopub.execute_input":"2022-04-27T12:34:03.195953Z","iopub.status.idle":"2022-04-27T12:34:03.200398Z","shell.execute_reply.started":"2022-04-27T12:34:03.195918Z","shell.execute_reply":"2022-04-27T12:34:03.199723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-27T12:34:03.201673Z","iopub.execute_input":"2022-04-27T12:34:03.202065Z","iopub.status.idle":"2022-04-27T12:34:03.278925Z","shell.execute_reply.started":"2022-04-27T12:34:03.202029Z","shell.execute_reply":"2022-04-27T12:34:03.278138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions = transactions[['customer_id','article_id']].groupby(['customer_id','article_id']).agg({'article_id':'count'})\n#transactions_og = transactions.copy()","metadata":{"execution":{"iopub.status.busy":"2022-04-27T12:34:03.280305Z","iopub.execute_input":"2022-04-27T12:34:03.280579Z","iopub.status.idle":"2022-04-27T12:34:03.457350Z","shell.execute_reply.started":"2022-04-27T12:34:03.280546Z","shell.execute_reply":"2022-04-27T12:34:03.456653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#transactions_og.columns=['purchase_count']\n#transactions_og.reset_index(inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-04-27T12:34:03.458526Z","iopub.execute_input":"2022-04-27T12:34:03.458842Z","iopub.status.idle":"2022-04-27T12:34:03.462159Z","shell.execute_reply.started":"2022-04-27T12:34:03.458805Z","shell.execute_reply":"2022-04-27T12:34:03.461534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions.columns=['purchase_count']\ntransactions.reset_index(inplace=True)\ntransactions.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-27T12:34:03.463454Z","iopub.execute_input":"2022-04-27T12:34:03.463904Z","iopub.status.idle":"2022-04-27T12:34:03.487372Z","shell.execute_reply.started":"2022-04-27T12:34:03.463870Z","shell.execute_reply":"2022-04-27T12:34:03.486747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#def create_data_dummy(data):\n#    data_dummy = data.copy()\n#    data_dummy['purchase_dummy'] = 1\n#    return data_dummy\n#data_dummy = create_data_dummy(transactions)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#transactions['tot_item_purcahsed'] = \ntot_transactions = transactions[['customer_id','article_id']].groupby(['customer_id']).agg({'article_id':'count'})","metadata":{"execution":{"iopub.status.busy":"2022-04-27T12:34:03.489752Z","iopub.execute_input":"2022-04-27T12:34:03.490108Z","iopub.status.idle":"2022-04-27T12:34:03.953995Z","shell.execute_reply.started":"2022-04-27T12:34:03.490081Z","shell.execute_reply":"2022-04-27T12:34:03.953267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tot_transactions.reset_index(inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-04-27T12:34:03.956965Z","iopub.execute_input":"2022-04-27T12:34:03.957393Z","iopub.status.idle":"2022-04-27T12:34:03.962130Z","shell.execute_reply.started":"2022-04-27T12:34:03.957348Z","shell.execute_reply":"2022-04-27T12:34:03.961342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tot_transactions.columns = ['customer_id','tot_purchase']","metadata":{"execution":{"iopub.status.busy":"2022-04-27T12:34:03.963494Z","iopub.execute_input":"2022-04-27T12:34:03.963949Z","iopub.status.idle":"2022-04-27T12:34:03.971677Z","shell.execute_reply.started":"2022-04-27T12:34:03.963912Z","shell.execute_reply":"2022-04-27T12:34:03.970843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions = cudf.merge(transactions,tot_transactions,on='customer_id')\ntransactions.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-27T12:34:03.974496Z","iopub.execute_input":"2022-04-27T12:34:03.974683Z","iopub.status.idle":"2022-04-27T12:34:05.069224Z","shell.execute_reply.started":"2022-04-27T12:34:03.974656Z","shell.execute_reply":"2022-04-27T12:34:05.068380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions.columns = ['customer_id','article_id','purchase_count','tot_purchase']","metadata":{"execution":{"iopub.status.busy":"2022-04-27T12:34:05.070556Z","iopub.execute_input":"2022-04-27T12:34:05.070806Z","iopub.status.idle":"2022-04-27T12:34:05.075040Z","shell.execute_reply.started":"2022-04-27T12:34:05.070771Z","shell.execute_reply":"2022-04-27T12:34:05.074260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions['scaled_purchase_freq'] = transactions['purchase_count']/transactions['tot_purchase']\n","metadata":{"execution":{"iopub.status.busy":"2022-04-27T12:34:05.076571Z","iopub.execute_input":"2022-04-27T12:34:05.076809Z","iopub.status.idle":"2022-04-27T12:34:05.103969Z","shell.execute_reply.started":"2022-04-27T12:34:05.076777Z","shell.execute_reply":"2022-04-27T12:34:05.102737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions.drop(['purchase_count','tot_purchase'],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-04-27T12:34:05.105481Z","iopub.execute_input":"2022-04-27T12:34:05.105757Z","iopub.status.idle":"2022-04-27T12:34:05.112390Z","shell.execute_reply.started":"2022-04-27T12:34:05.105721Z","shell.execute_reply":"2022-04-27T12:34:05.110210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions = transactions.sort_values(['customer_id','article_id','scaled_purchase_freq'],ascending=[1,1,0])","metadata":{"execution":{"iopub.status.busy":"2022-04-27T12:34:05.114441Z","iopub.execute_input":"2022-04-27T12:34:05.114741Z","iopub.status.idle":"2022-04-27T12:34:12.056461Z","shell.execute_reply.started":"2022-04-27T12:34:05.114706Z","shell.execute_reply":"2022-04-27T12:34:12.055611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions = transactions.to_pandas().groupby(['customer_id']).head(10)","metadata":{"execution":{"iopub.status.busy":"2022-04-27T12:34:12.057784Z","iopub.execute_input":"2022-04-27T12:34:12.058036Z","iopub.status.idle":"2022-04-27T12:34:24.173720Z","shell.execute_reply.started":"2022-04-27T12:34:12.057991Z","shell.execute_reply":"2022-04-27T12:34:24.172903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-27T12:34:24.175074Z","iopub.execute_input":"2022-04-27T12:34:24.175310Z","iopub.status.idle":"2022-04-27T12:34:24.181237Z","shell.execute_reply.started":"2022-04-27T12:34:24.175278Z","shell.execute_reply":"2022-04-27T12:34:24.180607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del tot_transactions","metadata":{"execution":{"iopub.status.busy":"2022-04-27T12:34:24.182394Z","iopub.execute_input":"2022-04-27T12:34:24.182800Z","iopub.status.idle":"2022-04-27T12:34:24.199677Z","shell.execute_reply.started":"2022-04-27T12:34:24.182764Z","shell.execute_reply":"2022-04-27T12:34:24.198955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Building a recommender model using turicreate","metadata":{}},{"cell_type":"code","source":"model = tc.item_similarity_recommender.create(tc.SFrame(transactions), 'customer_id', 'article_id', target='scaled_purchase_freq')","metadata":{"execution":{"iopub.status.busy":"2022-04-27T12:34:24.200718Z","iopub.execute_input":"2022-04-27T12:34:24.201812Z","iopub.status.idle":"2022-04-27T12:37:12.333918Z","shell.execute_reply.started":"2022-04-27T12:34:24.201750Z","shell.execute_reply":"2022-04-27T12:37:12.332936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# constant variables to define field names include:\nuser_id = 'customer_id'\nitem_id = 'article_id'\nusers_to_recommend = transactions[['customer_id']]['customer_id'].drop_duplicates().tolist()\nn_rec = 12 # number of items to recommend\nn_display = 12 # to display the first few rows in an output dataset","metadata":{"execution":{"iopub.status.busy":"2022-04-27T12:43:23.779086Z","iopub.execute_input":"2022-04-27T12:43:23.779811Z","iopub.status.idle":"2022-04-27T12:43:24.638536Z","shell.execute_reply.started":"2022-04-27T12:43:23.779768Z","shell.execute_reply":"2022-04-27T12:43:24.637779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"recom = model.recommend(users=users_to_recommend, k=n_rec)","metadata":{"execution":{"iopub.status.busy":"2022-04-27T12:43:29.312986Z","iopub.execute_input":"2022-04-27T12:43:29.313234Z","iopub.status.idle":"2022-04-27T13:06:23.297893Z","shell.execute_reply.started":"2022-04-27T12:43:29.313204Z","shell.execute_reply":"2022-04-27T13:06:23.297074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_rec = recom.to_dataframe()","metadata":{"execution":{"iopub.status.busy":"2022-04-27T13:08:27.838326Z","iopub.execute_input":"2022-04-27T13:08:27.839011Z","iopub.status.idle":"2022-04-27T13:09:00.151434Z","shell.execute_reply.started":"2022-04-27T13:08:27.838975Z","shell.execute_reply":"2022-04-27T13:09:00.150683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_rec.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-27T13:09:00.152891Z","iopub.execute_input":"2022-04-27T13:09:00.153124Z","iopub.status.idle":"2022-04-27T13:09:00.157879Z","shell.execute_reply.started":"2022-04-27T13:09:00.153092Z","shell.execute_reply":"2022-04-27T13:09:00.157142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(df_rec['customer_id'].drop_duplicates())","metadata":{"execution":{"iopub.status.busy":"2022-04-27T13:09:00.159109Z","iopub.execute_input":"2022-04-27T13:09:00.159571Z","iopub.status.idle":"2022-04-27T13:09:02.825056Z","shell.execute_reply.started":"2022-04-27T13:09:00.159493Z","shell.execute_reply":"2022-04-27T13:09:02.824300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_rec = df_rec[['customer_id','article_id']]","metadata":{"execution":{"iopub.status.busy":"2022-04-27T13:09:02.827318Z","iopub.execute_input":"2022-04-27T13:09:02.827801Z","iopub.status.idle":"2022-04-27T13:09:03.669102Z","shell.execute_reply.started":"2022-04-27T13:09:02.827761Z","shell.execute_reply":"2022-04-27T13:09:03.668359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_rec['prediction'] = df_rec.groupby(['customer_id'])['article_id'].transform(lambda x: ' '.join('0'+x.astype(str)))","metadata":{"execution":{"iopub.status.busy":"2022-04-27T13:09:03.670539Z","iopub.execute_input":"2022-04-27T13:09:03.670814Z","iopub.status.idle":"2022-04-27T13:15:32.604998Z","shell.execute_reply.started":"2022-04-27T13:09:03.670778Z","shell.execute_reply":"2022-04-27T13:15:32.604209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_rec.drop('article_id',inplace=True,axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-04-27T13:15:32.606412Z","iopub.execute_input":"2022-04-27T13:15:32.606683Z","iopub.status.idle":"2022-04-27T13:15:34.847554Z","shell.execute_reply.started":"2022-04-27T13:15:32.606651Z","shell.execute_reply":"2022-04-27T13:15:34.846730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_rec['prediction'][0]","metadata":{"execution":{"iopub.status.busy":"2022-04-27T13:15:34.848867Z","iopub.execute_input":"2022-04-27T13:15:34.849110Z","iopub.status.idle":"2022-04-27T13:15:34.854022Z","shell.execute_reply.started":"2022-04-27T13:15:34.849076Z","shell.execute_reply":"2022-04-27T13:15:34.853359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_rec.drop_duplicates(inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-04-27T13:15:34.855181Z","iopub.execute_input":"2022-04-27T13:15:34.855573Z","iopub.status.idle":"2022-04-27T13:15:43.271403Z","shell.execute_reply.started":"2022-04-27T13:15:34.855534Z","shell.execute_reply":"2022-04-27T13:15:43.270666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_rec.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-27T13:15:43.272754Z","iopub.execute_input":"2022-04-27T13:15:43.272985Z","iopub.status.idle":"2022-04-27T13:15:43.278234Z","shell.execute_reply.started":"2022-04-27T13:15:43.272954Z","shell.execute_reply":"2022-04-27T13:15:43.277270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# for remaining ids","metadata":{}},{"cell_type":"code","source":"sub7 = pd.read_csv('../input/h-m-framework-for-partitioned-validation/submission.csv').sort_values('customer_id').reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-04-27T13:15:43.281092Z","iopub.execute_input":"2022-04-27T13:15:43.281567Z","iopub.status.idle":"2022-04-27T13:15:49.415505Z","shell.execute_reply.started":"2022-04-27T13:15:43.281533Z","shell.execute_reply":"2022-04-27T13:15:49.414756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub7 = sub7[~sub7['customer_id'].isin(df_rec['customer_id'])]","metadata":{"execution":{"iopub.status.busy":"2022-04-27T13:15:49.416717Z","iopub.execute_input":"2022-04-27T13:15:49.416971Z","iopub.status.idle":"2022-04-27T13:15:50.326197Z","shell.execute_reply.started":"2022-04-27T13:15:49.416938Z","shell.execute_reply":"2022-04-27T13:15:50.325470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"frames = [df_rec, sub7]\nresult = pd.concat(frames)","metadata":{"execution":{"iopub.status.busy":"2022-04-27T13:15:50.327355Z","iopub.execute_input":"2022-04-27T13:15:50.327807Z","iopub.status.idle":"2022-04-27T13:15:50.399229Z","shell.execute_reply.started":"2022-04-27T13:15:50.327767Z","shell.execute_reply":"2022-04-27T13:15:50.398540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-27T13:15:50.400553Z","iopub.execute_input":"2022-04-27T13:15:50.400827Z","iopub.status.idle":"2022-04-27T13:15:50.407326Z","shell.execute_reply.started":"2022-04-27T13:15:50.400793Z","shell.execute_reply":"2022-04-27T13:15:50.406549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result.to_csv('turicreate_top10_0.0232.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-04-27T13:15:50.409006Z","iopub.execute_input":"2022-04-27T13:15:50.409526Z","iopub.status.idle":"2022-04-27T13:16:01.654634Z","shell.execute_reply.started":"2022-04-27T13:15:50.409460Z","shell.execute_reply":"2022-04-27T13:16:01.653884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Done","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}