{"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-19T19:26:33.971577Z","iopub.execute_input":"2022-02-19T19:26:33.972166Z","iopub.status.idle":"2022-02-19T19:26:33.977725Z","shell.execute_reply.started":"2022-02-19T19:26:33.972132Z","shell.execute_reply":"2022-02-19T19:26:33.976780Z"},"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-19T19:26:33.979453Z","iopub.execute_input":"2022-02-19T19:26:33.980193Z","iopub.status.idle":"2022-02-19T19:26:37.118012Z","shell.execute_reply.started":"2022-02-19T19:26:33.980153Z","shell.execute_reply":"2022-02-19T19:26:37.117311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2022-02-19T19:26:37.120642Z","iopub.execute_input":"2022-02-19T19:26:37.121063Z","iopub.status.idle":"2022-02-19T19:26:37.129609Z","shell.execute_reply.started":"2022-02-19T19:26:37.121024Z","shell.execute_reply":"2022-02-19T19:26:37.128876Z"},"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-19T19:26:37.130793Z","iopub.execute_input":"2022-02-19T19:26:37.131052Z","iopub.status.idle":"2022-02-19T19:26:45.146124Z","shell.execute_reply.started":"2022-02-19T19:26:37.131020Z","shell.execute_reply":"2022-02-19T19:26:45.145402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Display Item Purchased Together\nWhen customers bought the itemarticle_idthe 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":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-19T19:53:53.239454Z","iopub.execute_input":"2022-02-19T19:53:53.239706Z","iopub.status.idle":"2022-02-19T19:54:31.416738Z","shell.execute_reply.started":"2022-02-19T19:53:53.239678Z","shell.execute_reply":"2022-02-19T19:54:31.416081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}