{"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":"need to turn on GPU","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-05-25T10:43:16.127874Z","iopub.execute_input":"2022-05-25T10:43:16.128155Z","iopub.status.idle":"2022-05-25T10:43:16.134022Z","shell.execute_reply.started":"2022-05-25T10:43:16.128125Z","shell.execute_reply":"2022-05-25T10:43:16.133247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-05-25T10:43:16.181172Z","iopub.execute_input":"2022-05-25T10:43:16.181932Z","iopub.status.idle":"2022-05-25T10:43:19.454194Z","shell.execute_reply.started":"2022-05-25T10:43:16.181885Z","shell.execute_reply":"2022-05-25T10:43:19.453394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-25T10:43:19.456026Z","iopub.execute_input":"2022-05-25T10:43:19.456368Z","iopub.status.idle":"2022-05-25T10:43:19.475945Z","shell.execute_reply.started":"2022-05-25T10:43:19.456328Z","shell.execute_reply":"2022-05-25T10:43:19.475183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Find Items Purchased Together","metadata":{}},{"cell_type":"code","source":"# FIND ITEMS PURCHASED TOGETHER\nvc = df.article_id.value_counts()\npairs = {}","metadata":{"execution":{"iopub.status.busy":"2022-05-25T10:43:19.477091Z","iopub.execute_input":"2022-05-25T10:43:19.477905Z","iopub.status.idle":"2022-05-25T10:43:19.526460Z","shell.execute_reply.started":"2022-05-25T10:43:19.477863Z","shell.execute_reply":"2022-05-25T10:43:19.525735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vc","metadata":{"execution":{"iopub.status.busy":"2022-05-25T10:43:19.528389Z","iopub.execute_input":"2022-05-25T10:43:19.529075Z","iopub.status.idle":"2022-05-25T10:43:19.538382Z","shell.execute_reply.started":"2022-05-25T10:43:19.529031Z","shell.execute_reply":"2022-05-25T10:43:19.537464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vc.index.values[1000:1032]","metadata":{"execution":{"iopub.status.busy":"2022-05-25T10:43:19.539665Z","iopub.execute_input":"2022-05-25T10:43:19.540088Z","iopub.status.idle":"2022-05-25T10:43:19.547186Z","shell.execute_reply.started":"2022-05-25T10:43:19.540048Z","shell.execute_reply":"2022-05-25T10:43:19.546421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vc.index.values[400:432]","metadata":{"execution":{"iopub.status.busy":"2022-05-25T10:43:19.548565Z","iopub.execute_input":"2022-05-25T10:43:19.549280Z","iopub.status.idle":"2022-05-25T10:43:19.558810Z","shell.execute_reply.started":"2022-05-25T10:43:19.549241Z","shell.execute_reply":"2022-05-25T10:43:19.558067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for j,i in enumerate(vc.index.values[400:432]):\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-05-25T10:43:19.560043Z","iopub.execute_input":"2022-05-25T10:43:19.560771Z","iopub.status.idle":"2022-05-25T10:43:27.543921Z","shell.execute_reply.started":"2022-05-25T10:43:19.560699Z","shell.execute_reply":"2022-05-25T10:43:27.543139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pairs","metadata":{"execution":{"iopub.status.busy":"2022-05-25T10:43:27.545278Z","iopub.execute_input":"2022-05-25T10:43:27.545655Z","iopub.status.idle":"2022-05-25T10:43:27.556022Z","shell.execute_reply.started":"2022-05-25T10:43:27.545620Z","shell.execute_reply":"2022-05-25T10:43:27.555254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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()","metadata":{"execution":{"iopub.status.busy":"2022-05-25T10:43:27.557587Z","iopub.execute_input":"2022-05-25T10:43:27.558106Z","iopub.status.idle":"2022-05-25T10:44:07.539404Z","shell.execute_reply.started":"2022-05-25T10:43:27.558069Z","shell.execute_reply":"2022-05-25T10:44:07.538602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# top set pair","metadata":{}},{"cell_type":"code","source":"# FIND ITEMS PURCHASED TOGETHER\nvc = df.article_id.value_counts()\npairs_t = {}","metadata":{"execution":{"iopub.status.busy":"2022-05-25T10:44:07.541801Z","iopub.execute_input":"2022-05-25T10:44:07.542121Z","iopub.status.idle":"2022-05-25T10:44:07.593663Z","shell.execute_reply.started":"2022-05-25T10:44:07.542083Z","shell.execute_reply":"2022-05-25T10:44:07.593006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for j,i in enumerate(vc.index.values[0:32]):\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_t[i.item()] = [vc2.index[0], vc2.index[1], vc2.index[2]]","metadata":{"execution":{"iopub.status.busy":"2022-05-25T10:44:07.595011Z","iopub.execute_input":"2022-05-25T10:44:07.595239Z","iopub.status.idle":"2022-05-25T10:44:15.694233Z","shell.execute_reply.started":"2022-05-25T10:44:07.595207Z","shell.execute_reply":"2022-05-25T10:44:15.693572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pairs_t","metadata":{"execution":{"iopub.status.busy":"2022-05-25T10:44:15.695494Z","iopub.execute_input":"2022-05-25T10:44:15.695744Z","iopub.status.idle":"2022-05-25T10:44:15.704267Z","shell.execute_reply.started":"2022-05-25T10:44:15.695695Z","shell.execute_reply":"2022-05-25T10:44:15.703418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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_t.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()","metadata":{"execution":{"iopub.status.busy":"2022-05-25T10:44:15.706100Z","iopub.execute_input":"2022-05-25T10:44:15.706385Z","iopub.status.idle":"2022-05-25T10:44:55.653226Z","shell.execute_reply.started":"2022-05-25T10:44:15.706348Z","shell.execute_reply":"2022-05-25T10:44:55.652538Z"},"trusted":true},"execution_count":null,"outputs":[]}]}