{"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":"# Customers Who Bought This Frequently Buy This!\nIn this notebook we will explore which items were frequently purchased together. Using this information, we can predict which items a customer will buy after we observe what they have already bought!","metadata":{}},{"cell_type":"code","source":"import cudf, gc\nimport cv2, matplotlib.pyplot as plt\nfrom os.path import exists\nprint('RAPIDS version',cudf.__version__)","metadata":{"execution":{"iopub.status.busy":"2022-02-17T23:08:49.95389Z","iopub.execute_input":"2022-02-17T23:08:49.954366Z","iopub.status.idle":"2022-02-17T23:08:49.959606Z","shell.execute_reply.started":"2022-02-17T23:08:49.954331Z","shell.execute_reply":"2022-02-17T23:08:49.958772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Tranactions","metadata":{}},{"cell_type":"code","source":"# LOAD TRANSACTIONS DATAFRAME\ndf = cudf.read_csv('../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv')\nprint('Transactions shape',df.shape)\ndisplay( df.head() )\n\n# REDUCE MEMORY OF DATAFRAME\ndf = df[['customer_id','article_id']]\ndf.customer_id = df.customer_id.str[-16:].str.hex_to_int().astype('int64')\ndf.article_id = df.article_id.astype('int32')\n_ = gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-02-17T22:56:51.09492Z","iopub.execute_input":"2022-02-17T22:56:51.09542Z","iopub.status.idle":"2022-02-17T22:57:33.828074Z","shell.execute_reply.started":"2022-02-17T22:56:51.095381Z","shell.execute_reply":"2022-02-17T22:57:33.827285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Find Items Purchased Together\nWe will use RAPID cuDF to speed up the dataframe search commands below","metadata":{}},{"cell_type":"code","source":"# FIND ITEMS PURCHASED TOGETHER\nvc = df.article_id.value_counts()\npairs = {}\nfor j,i in enumerate(vc.index.values[1000:1032]):\n    #if j%10==0: print(j,', ',end='')\n    USERS = df.loc[df.article_id==i.item(),'customer_id'].unique()\n    vc2 = df.loc[(df.customer_id.isin(USERS))&(df.article_id!=i.item()),'article_id'].value_counts()\n    pairs[i.item()] = [vc2.index[0], vc2.index[1], vc2.index[2]]","metadata":{"execution":{"iopub.status.busy":"2022-02-17T23:18:43.00049Z","iopub.execute_input":"2022-02-17T23:18:43.000749Z","iopub.status.idle":"2022-02-17T23:18:50.961901Z","shell.execute_reply.started":"2022-02-17T23:18:43.000719Z","shell.execute_reply":"2022-02-17T23:18:50.961149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Display Item Purchased Together\nWhen customers bought the item in the 1st column below, then those customers also bought the items in the 2nd, 3rd, and 4th column too!","metadata":{}},{"cell_type":"code","source":"items = cudf.read_csv('../input/h-and-m-personalized-fashion-recommendations/articles.csv')\nBASE = '../input/h-and-m-personalized-fashion-recommendations/images/'\n\nfor i,(k,v) in enumerate( pairs.items() ):\n    name1 = BASE+'0'+str(k)[:2]+'/0'+str(k)+'.jpg'\n    name2 = BASE+'0'+str(v[0])[:2]+'/0'+str(v[0])+'.jpg'\n    name3 = BASE+'0'+str(v[1])[:2]+'/0'+str(v[1])+'.jpg'\n    name4 = BASE+'0'+str(v[2])[:2]+'/0'+str(v[2])+'.jpg'\n    if exists(name1) & exists(name2) & exists(name3) & exists(name4):\n        plt.figure(figsize=(20,5))\n        img1 = cv2.imread(name1)[:,:,::-1]\n        img2 = cv2.imread(name2)[:,:,::-1]\n        img3 = cv2.imread(name3)[:,:,::-1]\n        img4 = cv2.imread(name4)[:,:,::-1]\n        plt.subplot(1,4,1)\n        plt.title('When customers buy this',size=18)\n        plt.imshow(img1)\n        plt.subplot(1,4,2)\n        plt.title('They buy this',size=18)\n        plt.imshow(img2)\n        plt.subplot(1,4,3)\n        plt.title('They buy this',size=18)\n        plt.imshow(img3)\n        plt.subplot(1,4,4)\n        plt.title('They buy this',size=18)\n        plt.imshow(img4)\n        plt.show()\n    #if i==63: break","metadata":{"execution":{"iopub.status.busy":"2022-02-17T23:19:03.561202Z","iopub.execute_input":"2022-02-17T23:19:03.561949Z","iopub.status.idle":"2022-02-17T23:19:41.738753Z","shell.execute_reply.started":"2022-02-17T23:19:03.561912Z","shell.execute_reply":"2022-02-17T23:19:41.738072Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]}]}