{"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":"\n## Exploring Articles frequently purchased together\n\nThis notebook is being used to test out a baseline model concept, wherein we will explore which items were purchased frequently together. The idea here is that we can use only this information to predict which items will a customer buy,through observation of what they've already purchased","metadata":{"_uuid":"2a76f454-ae8c-4a8b-b891-d02a828bc1f4","_cell_guid":"9b7b8ec9-270d-4368-8b4b-f67f2e59bb90","trusted":true}},{"cell_type":"markdown","source":"#### Importing necessary libraries","metadata":{"_uuid":"fbddbaf4-6e2d-485b-8cf8-2d003a5c76ff","_cell_guid":"dd4ddcde-e846-4aff-ac75-98f25bd2ab11","trusted":true}},{"cell_type":"code","source":"import cudf, gc\nimport cv2, matplotlib.pyplot as plt\nfrom os.path import exists\nprint('RAPIDS version',cudf.__version__)","metadata":{"_uuid":"7c011944-7d2c-42a3-b9ec-cf629790dece","_cell_guid":"201f463a-08d2-4019-bbf7-9a2befe8551e","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-09-08T17:40:57.813683Z","iopub.execute_input":"2022-09-08T17:40:57.814306Z","iopub.status.idle":"2022-09-08T17:40:57.822929Z","shell.execute_reply.started":"2022-09-08T17:40:57.814263Z","shell.execute_reply":"2022-09-08T17:40:57.819306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Loading Transcations dataframe and using memory reduction strategies","metadata":{"_uuid":"8a26d257-16d7-4916-8edb-84d7b9338a3f","_cell_guid":"84606d8b-a232-4b32-ade1-f3ca738264bb","trusted":true}},{"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":{"_uuid":"b22aa78c-dc21-4449-90b3-7f6c1f351e48","_cell_guid":"656ddc39-94a9-4659-901d-c75cb5b6d9ae","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-09-08T17:40:58.381866Z","iopub.execute_input":"2022-09-08T17:40:58.382639Z","iopub.status.idle":"2022-09-08T17:41:01.515127Z","shell.execute_reply.started":"2022-09-08T17:40:58.382591Z","shell.execute_reply":"2022-09-08T17:41:01.514337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Finding out items that were purchased together using CUDF (speedup using GPU)\nWe will use RAPID cuDF to speed up searching through the dataframes","metadata":{"_uuid":"a5a3fa18-a4be-4e53-b331-34a558aa706e","_cell_guid":"1151c20f-cb25-4185-86fa-fefe6aa3d5dc","trusted":true}},{"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":{"_uuid":"32f21b22-ace0-4750-96fb-2531f29116af","_cell_guid":"9e5bc373-e7b2-4f55-9153-920452006ca7","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-09-08T17:41:01.516998Z","iopub.execute_input":"2022-09-08T17:41:01.517314Z","iopub.status.idle":"2022-09-08T17:41:09.504455Z","shell.execute_reply.started":"2022-09-08T17:41:01.517275Z","shell.execute_reply":"2022-09-08T17:41:09.503682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Displaying images of items bought together\n\nThe way the visualisation is created, it shows the item that has been bought in the first column, with items bought by the same customers in the 2nd,3rd and 4th column","metadata":{"_uuid":"b2acb99f-bcd9-45b6-87e7-a57781387a17","_cell_guid":"5deb0923-3f1d-4d80-8a32-8c1e7c33033e","trusted":true}},{"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 Article',size=18)\n        plt.imshow(img1)\n        plt.subplot(1,4,2)\n        plt.title('They also buy this',size=18)\n        plt.imshow(img2)\n        plt.subplot(1,4,3)\n        plt.title('As well as this',size=18)\n        plt.imshow(img3)\n        plt.subplot(1,4,4)\n        plt.title('As well as this',size=18)\n        plt.imshow(img4)\n        plt.show()\n    #if i==63: break","metadata":{"_uuid":"2e6f32f2-d539-4aae-b4bc-e359358cf3bd","_cell_guid":"c167aa56-4549-4285-b494-e65f64082aab","collapsed":false,"_kg_hide-input":true,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-09-08T17:41:09.505570Z","iopub.execute_input":"2022-09-08T17:41:09.505808Z","iopub.status.idle":"2022-09-08T17:41:47.534579Z","shell.execute_reply.started":"2022-09-08T17:41:09.505776Z","shell.execute_reply":"2022-09-08T17:41:47.533857Z"},"trusted":true},"execution_count":null,"outputs":[]}]}