{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n#for dirname, _, filenames in os.walk('/kaggle/input'):\n#    for filename in filenames:\n#        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-12-31T01:44:56.124338Z","iopub.execute_input":"2022-12-31T01:44:56.124816Z","iopub.status.idle":"2022-12-31T01:44:56.131887Z","shell.execute_reply.started":"2022-12-31T01:44:56.124780Z","shell.execute_reply":"2022-12-31T01:44:56.130652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_json(\"/kaggle/input/otto-recommender-system/test.jsonl\", lines=True)\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-31T01:44:56.134026Z","iopub.execute_input":"2022-12-31T01:44:56.134523Z","iopub.status.idle":"2022-12-31T01:45:19.808397Z","shell.execute_reply.started":"2022-12-31T01:44:56.134469Z","shell.execute_reply":"2022-12-31T01:45:19.807024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def getCounts(values):\n    counts = {}\n    for value in values:\n        counts[value] = counts.get(value, 0) + 1\n    return counts","metadata":{"execution":{"iopub.status.busy":"2022-12-31T01:45:19.810663Z","iopub.execute_input":"2022-12-31T01:45:19.811035Z","iopub.status.idle":"2022-12-31T01:45:19.817414Z","shell.execute_reply.started":"2022-12-31T01:45:19.810996Z","shell.execute_reply":"2022-12-31T01:45:19.816170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sorted_session_aid = {}\n\nfor i in test_df.index:\n    session = test_df.loc[i, \"session\"]\n    events = test_df.loc[i, \"events\"]\n    \n    session_aid = []\n    for event in events:\n        session_aid.append(event[\"aid\"])\n    session_aid_count = getCounts(session_aid)\n    sorted_aid_vals = sorted(set(session_aid), key=lambda x :session_aid_count[x], reverse=True)\n    sorted_session_aid[session] = sorted_aid_vals","metadata":{"execution":{"iopub.status.busy":"2022-12-31T01:45:19.819057Z","iopub.execute_input":"2022-12-31T01:45:19.819474Z","iopub.status.idle":"2022-12-31T01:46:29.133075Z","shell.execute_reply.started":"2022-12-31T01:45:19.819441Z","shell.execute_reply":"2022-12-31T01:46:29.132004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_data = {}\nsubmission_data[\"session_type\"] = []\nsubmission_data[\"labels\"] = []\n\nsessions = test_df.session","metadata":{"execution":{"iopub.status.busy":"2022-12-31T01:46:29.135262Z","iopub.execute_input":"2022-12-31T01:46:29.136500Z","iopub.status.idle":"2022-12-31T01:46:29.142291Z","shell.execute_reply.started":"2022-12-31T01:46:29.136454Z","shell.execute_reply":"2022-12-31T01:46:29.141192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for session in sessions:\n    top_20 = \" \".join(map(str, sorted_session_aid[session][:20]))\n    for item in [\"clicks\", \"carts\", \"orders\"]:\n        submission_data[\"session_type\"].append(\"{}_{}\".format(session, item))\n        submission_data[\"labels\"].append(top_20)","metadata":{"execution":{"iopub.status.busy":"2022-12-31T01:46:29.143578Z","iopub.execute_input":"2022-12-31T01:46:29.143950Z","iopub.status.idle":"2022-12-31T01:46:35.632355Z","shell.execute_reply.started":"2022-12-31T01:46:29.143918Z","shell.execute_reply":"2022-12-31T01:46:35.631304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.DataFrame(submission_data)\nsubmission_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-31T01:46:35.633704Z","iopub.execute_input":"2022-12-31T01:46:35.634023Z","iopub.status.idle":"2022-12-31T01:46:37.014183Z","shell.execute_reply.started":"2022-12-31T01:46:35.633995Z","shell.execute_reply":"2022-12-31T01:46:37.013017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-12-31T01:53:00.315350Z","iopub.execute_input":"2022-12-31T01:53:00.315976Z","iopub.status.idle":"2022-12-31T01:53:09.587637Z","shell.execute_reply.started":"2022-12-31T01:53:00.315930Z","shell.execute_reply":"2022-12-31T01:53:09.586195Z"},"trusted":true},"execution_count":null,"outputs":[]}]}