{"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":"# Create action df!!!","metadata":{}},{"cell_type":"markdown","source":"テストデータに含まれているユーザーの過去の行動からどのような商品に興味を持っているのかを調べてみます。","metadata":{}},{"cell_type":"markdown","source":"# 1.ライブラリのインポート","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport time\nimport gc\nimport copy\nimport glob","metadata":{"execution":{"iopub.status.busy":"2022-12-27T00:39:52.379475Z","iopub.execute_input":"2022-12-27T00:39:52.380030Z","iopub.status.idle":"2022-12-27T00:39:52.410025Z","shell.execute_reply.started":"2022-12-27T00:39:52.379920Z","shell.execute_reply":"2022-12-27T00:39:52.408991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2.データの作成","metadata":{}},{"cell_type":"code","source":"%%time\n# 与えられたパスのファイルを読み込む関数\ndef read_file(f):\n    return pd.DataFrame(data_cache[f])\n\n\n# ファイルのの容量をできるだけ小さくする関数。\ndef read_file_to_cache(f):\n    df = pd.read_parquet(f)\n    df.ts = ((df.ts // 1000)).astype('int32')\n    return df","metadata":{"execution":{"iopub.status.busy":"2022-12-27T00:39:52.412683Z","iopub.execute_input":"2022-12-27T00:39:52.413676Z","iopub.status.idle":"2022-12-27T00:39:55.212048Z","shell.execute_reply.started":"2022-12-27T00:39:52.413611Z","shell.execute_reply":"2022-12-27T00:39:55.211119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_cache = {}\nfiles = glob.glob('../input/otto-chunk-data-inparquet-format/test_parquet/*')\n\nfor f in files:\n    data_cache[f] = read_file_to_cache(f)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nDISK_PIECES = 1\n\nfor PART in range(DISK_PIECES):\n    print()\n    print('### DISK PART',PART+1)\n    \n    for f in range(len(files)):\n        df = read_file(files[f])\n        if f == 0:\n            tmp = df\n        else:\n            tmp = pd.concat([tmp, df], axis=0)","metadata":{"execution":{"iopub.status.busy":"2022-12-27T00:39:55.213589Z","iopub.execute_input":"2022-12-27T00:39:55.214238Z","iopub.status.idle":"2022-12-27T00:39:57.223603Z","shell.execute_reply.started":"2022-12-27T00:39:55.214187Z","shell.execute_reply":"2022-12-27T00:39:57.222155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_action_df = tmp.copy()\n\n# aidを文字列に変換し、前にスペースを入れます。\n# これをユーザー別、行動別にグループ分けし、足し合わせていきます。\n# ここで、文字列にしておいたことで、12345 12346のようにaidのリストのようなものを作ることができます。\ntest_action_df.aid = ' ' + tmp.aid.astype(str)\ntest_action_df = test_action_df.groupby(['session', 'type'])['aid'].sum().reset_index()","metadata":{"execution":{"iopub.status.busy":"2022-12-27T00:39:57.226438Z","iopub.execute_input":"2022-12-27T00:39:57.226825Z","iopub.status.idle":"2022-12-27T00:40:05.446781Z","shell.execute_reply.started":"2022-12-27T00:39:57.226792Z","shell.execute_reply":"2022-12-27T00:40:05.445347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ある商品をクリックした人にはその人が過去にクリックした商品をお勧めします。\nfor_clicks = test_action_df[test_action_df['type'] == 'clicks']\n\n# 同様にある商品をカートに入れた人にもその人が過去にクリックした商品をお勧めします。\nfor_carts = for_clicks.copy()\nfor_carts['type'] = 'carts'","metadata":{"execution":{"iopub.status.busy":"2022-12-27T00:40:05.448673Z","iopub.execute_input":"2022-12-27T00:40:05.449206Z","iopub.status.idle":"2022-12-27T00:40:05.899222Z","shell.execute_reply.started":"2022-12-27T00:40:05.449123Z","shell.execute_reply":"2022-12-27T00:40:05.898145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ある商品を注文した人にはその人が過去にクリックした商品と、カートに入れた商品の両方をお勧めします。\nfor_orders = test_action_df[test_action_df['type'] == 'carts']\nfor_orders = pd.merge(for_orders, for_clicks[['session', 'aid']], on='session', how='left')\nfor_orders['type'] = 'orders'\nfor_orders['aid'] = for_orders['aid_x'] + for_orders['aid_y']\nfor_orders.drop(['aid_x', 'aid_y'], axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-12-27T00:40:05.900472Z","iopub.execute_input":"2022-12-27T00:40:05.900790Z","iopub.status.idle":"2022-12-27T00:40:06.963020Z","shell.execute_reply.started":"2022-12-27T00:40:05.900761Z","shell.execute_reply":"2022-12-27T00:40:06.961887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for_clicks.to_parquet('for_clicks.parquet')\nfor_carts.to_parquet('for_carts.parquet')\nfor_orders.to_parquet('for_orders.parquet')","metadata":{"execution":{"iopub.status.busy":"2022-12-27T00:40:06.964732Z","iopub.execute_input":"2022-12-27T00:40:06.965255Z","iopub.status.idle":"2022-12-27T00:40:09.065838Z","shell.execute_reply.started":"2022-12-27T00:40:06.965202Z","shell.execute_reply":"2022-12-27T00:40:09.064379Z"},"trusted":true},"execution_count":null,"outputs":[]}]}