{"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 pandas as pd\nimport numpy as np\nimport pickle\nfrom collections import defaultdict","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-22T23:31:59.101424Z","iopub.execute_input":"2022-10-22T23:31:59.102172Z","iopub.status.idle":"2022-10-22T23:31:59.113098Z","shell.execute_reply.started":"2022-10-22T23:31:59.102100Z","shell.execute_reply":"2022-10-22T23:31:59.111896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This notebook calculates the global win rate based on the 'position_at_finish'.\n\nI uses the jockey data sorted in the smallest order by position_at_finish.","metadata":{}},{"cell_type":"code","source":"with open(\"../input/race-time-outlier-visualization-v0/rcsttk_table.pkl\", 'rb') as f:\n    df = pickle.load(f)\ndf","metadata":{"execution":{"iopub.status.busy":"2022-10-22T23:31:59.119161Z","iopub.execute_input":"2022-10-22T23:31:59.119488Z","iopub.status.idle":"2022-10-22T23:32:00.825663Z","shell.execute_reply.started":"2022-10-22T23:31:59.119459Z","shell.execute_reply":"2022-10-22T23:32:00.824238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"jockey_df = df[['jockey']]\njockey_df","metadata":{"execution":{"iopub.status.busy":"2022-10-22T23:32:00.827048Z","iopub.execute_input":"2022-10-22T23:32:00.827427Z","iopub.status.idle":"2022-10-22T23:32:00.845809Z","shell.execute_reply.started":"2022-10-22T23:32:00.827394Z","shell.execute_reply":"2022-10-22T23:32:00.844452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"win_dict = defaultdict(dict)\ntotal_dict = defaultdict(dict)\nfor i, row in jockey_df.iterrows():\n    jk_list = row['jockey']\n    N = len(jk_list)\n    for idx in range(N):\n        cur_jk = jk_list[idx]\n        for sub_idx in range(N):\n            sub_jk = jk_list[sub_idx]\n            if sub_idx == idx:\n                continue\n            elif sub_idx > idx:\n                win_dict[cur_jk][sub_jk] = win_dict[cur_jk].get(sub_jk, 0) + 1\n            else:\n                win_dict[cur_jk][sub_jk] = win_dict[cur_jk].get(sub_jk, 0)\n            total_dict[cur_jk][sub_jk] = total_dict[cur_jk].get(sub_jk, 0) + 1","metadata":{"execution":{"iopub.status.busy":"2022-10-22T23:32:00.849520Z","iopub.execute_input":"2022-10-22T23:32:00.850004Z","iopub.status.idle":"2022-10-22T23:32:01.106094Z","shell.execute_reply.started":"2022-10-22T23:32:00.849957Z","shell.execute_reply":"2022-10-22T23:32:01.104901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"win_rate_dict = defaultdict(dict)\n\nfor key in win_dict:\n    sub_win_dict = win_dict[key]\n    sub_total_dict = total_dict[key]\n    for sub_key in sub_win_dict:\n        win_rate = sub_win_dict[sub_key]/sub_total_dict[sub_key]\n        win_rate_dict[key][sub_key] = win_rate","metadata":{"execution":{"iopub.status.busy":"2022-10-22T23:32:01.107434Z","iopub.execute_input":"2022-10-22T23:32:01.107756Z","iopub.status.idle":"2022-10-22T23:32:01.116360Z","shell.execute_reply.started":"2022-10-22T23:32:01.107727Z","shell.execute_reply":"2022-10-22T23:32:01.115383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_size = 5\nmanuel_franco_win_rate = win_rate_dict['Manuel Franco'].copy()\nmanuel_franco_win_rate = {x: manuel_franco_win_rate[x] for x in sorted(manuel_franco_win_rate)[:sample_size]}\nprint('Manuel Franco Win Rate Dict')\nmanuel_franco_win_rate\n","metadata":{"execution":{"iopub.status.busy":"2022-10-22T23:32:01.117546Z","iopub.execute_input":"2022-10-22T23:32:01.118145Z","iopub.status.idle":"2022-10-22T23:32:01.135557Z","shell.execute_reply.started":"2022-10-22T23:32:01.118109Z","shell.execute_reply":"2022-10-22T23:32:01.134350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame(win_rate_dict)\ndf = df.reindex(sorted(df.columns), axis=1)\ndf = df.sort_index()\ndf","metadata":{"execution":{"iopub.status.busy":"2022-10-22T23:32:01.137283Z","iopub.execute_input":"2022-10-22T23:32:01.137794Z","iopub.status.idle":"2022-10-22T23:32:01.188639Z","shell.execute_reply.started":"2022-10-22T23:32:01.137763Z","shell.execute_reply":"2022-10-22T23:32:01.187454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_size = 5\nprint('Manuel Franco Win Rate')\ndf.loc[:, 'Manuel Franco'].dropna().head(sample_size)","metadata":{"execution":{"iopub.status.busy":"2022-10-22T23:32:01.189994Z","iopub.execute_input":"2022-10-22T23:32:01.190409Z","iopub.status.idle":"2022-10-22T23:32:01.199443Z","shell.execute_reply.started":"2022-10-22T23:32:01.190379Z","shell.execute_reply":"2022-10-22T23:32:01.198333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The above table is a win_rate table based on the complete data.\n\nIt expresses the case where the player in the column beats the player in the index.\n\nA value of nan means no game between players.","metadata":{}},{"cell_type":"markdown","source":"In this notebook, the win rate value was calculated as \n\nan index to be used as a criterion for analyzing user data.\n\nThanks.","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}