{"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":"code","source":"import cudf\nimport gc","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-04-08T11:11:47.549915Z","iopub.execute_input":"2022-04-08T11:11:47.550743Z","iopub.status.idle":"2022-04-08T11:11:51.062773Z","shell.execute_reply.started":"2022-04-08T11:11:47.550636Z","shell.execute_reply":"2022-04-08T11:11:51.062042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## The idea here comes from Chris Deotte notebook that calculates the articles usually purchased together. (https://www.kaggle.com/code/cdeotte/customers-who-bought-this-frequently-buy-this)","metadata":{}},{"cell_type":"code","source":"# Load the dataset and discard unused columns\ntrain = cudf.read_csv('../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv')\ndel train['price']\ndel train['sales_channel_id']\ngc.collect()\n\n# Convert customer_id to int to save memory and speedup processings.\ntrain['customer_id'] = train['customer_id'].factorize()[0].astype('int32')\ntrain['t_dat'] = train['t_dat'].factorize()[0].astype('int16')\ngc.collect()\n\n# number of rows of train\nprint(train.shape)\ntrain.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-04-08T11:11:51.064244Z","iopub.execute_input":"2022-04-08T11:11:51.064800Z","iopub.status.idle":"2022-04-08T11:12:43.626703Z","shell.execute_reply.started":"2022-04-08T11:11:51.064772Z","shell.execute_reply":"2022-04-08T11:12:43.626011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def calc_pairs(train):\n    # Calculate all articles purchased together\n    dt = train.groupby(['customer_id','t_dat'])['article_id'].agg(list).rename('pair').reset_index()\n    df = train[['customer_id', 't_dat', 'article_id']].merge(dt, on=['customer_id', 't_dat'], how='left')\n    del dt\n    gc.collect()\n\n    # Explode the rows vs list of articles\n    df = df[['article_id', 'pair']].explode(column='pair')\n    gc.collect()\n    \n    # Discard duplicates\n    df = df.loc[df['article_id']!=df['pair']].reset_index(drop=True)\n    gc.collect()\n\n    # Count how many times each pair combination happens\n    df = df.groupby(['article_id', 'pair']).size().rename('count').reset_index()\n    gc.collect()\n    \n    # Sort by frequency\n    df = df.sort_values(['article_id' ,'count'], ascending=False).reset_index(drop=True)\n    gc.collect()\n    \n    # Pick only top1 most frequent pair\n    df['rank'] = df.groupby('article_id')['pair'].cumcount()\n    df = df.loc[df['rank']==0].reset_index(drop=True)\n    del df['rank']\n    gc.collect()\n    \n    return df","metadata":{"execution":{"iopub.status.busy":"2022-04-08T11:12:43.628290Z","iopub.execute_input":"2022-04-08T11:12:43.629063Z","iopub.status.idle":"2022-04-08T11:12:43.639187Z","shell.execute_reply.started":"2022-04-08T11:12:43.629006Z","shell.execute_reply":"2022-04-08T11:12:43.638383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\npairs = calc_pairs(train)\npairs","metadata":{"execution":{"iopub.status.busy":"2022-04-08T11:12:43.640592Z","iopub.execute_input":"2022-04-08T11:12:43.641125Z","iopub.status.idle":"2022-04-08T11:12:47.277512Z","shell.execute_reply.started":"2022-04-08T11:12:43.641086Z","shell.execute_reply":"2022-04-08T11:12:47.276796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pairs.to_parquet('top1-article-pairs.parquet')","metadata":{"execution":{"iopub.status.busy":"2022-04-08T11:12:47.279289Z","iopub.execute_input":"2022-04-08T11:12:47.280010Z","iopub.status.idle":"2022-04-08T11:12:47.365189Z","shell.execute_reply.started":"2022-04-08T11:12:47.279969Z","shell.execute_reply":"2022-04-08T11:12:47.364384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}