{"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\nfor 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-09-17T20:26:21.544691Z","iopub.execute_input":"2022-09-17T20:26:21.545281Z","iopub.status.idle":"2022-09-17T20:26:21.583798Z","shell.execute_reply.started":"2022-09-17T20:26:21.545175Z","shell.execute_reply":"2022-09-17T20:26:21.582449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"※誤り等がありましたらご指摘いただけると幸いです。","metadata":{}},{"cell_type":"markdown","source":">** Goal of the Competition**\nこの大会の目的は、競馬の戦術、起草戦略、および経路効率を分析することです。これまでに公開されたことのない座標データと基本的なレース情報を使用してモデルを開発します。 あなたの仕事は、競走馬の所有者、トレーナー、獣医師が、馬のパフォーマンスと福祉がどのように結びついているかをよりよく理解するのに役立ちます.データ分析が改善されれば、馬の福祉は大幅に改善される可能性があります。","metadata":{}},{"cell_type":"markdown","source":"> ****Score\nコンテストへのエントリは、次の 5 つのコンポーネントで評価されるノートブックの提出で構成されます。0 は低スコア、100 は高スコアです。提出物は、以下にどれだけうまく対応しているかに基づいて審査されます。 \n\nイノベーション (合計 25 ポイント) これは追跡データを見る斬新な方法ですか? (10ポイント) \n\n統計/機械学習のアプローチは最新の標準を使用していますか? (5点) \n\n結論は、競馬方法の現状に挑戦しますか? (10ポイント) \n\n関連性 (合計 30 ポイント) \n\n結論は、馬の福祉、馬の成績、またはライダーの意思決定に影響を与えることができますか? (10ポイント) \n\nこの結論は、将来の (より大規模でより詳細な) データ セットに関する今後の研究の基礎となるでし\nょうか? (10ポイント) \n\n結論は、競馬の参加者 (馬主、調教師、獣医師など) が理解し、消化し、議論できるものですか? (10ポイント) \n\n能力 (合計 25 ポイント) \n\nデータが与えられた場合、統計モデルは適切ですか? (5点) \n\n結論はデータによって裏付けられていますか? (10ポイント) \n\n分析は正確ですか？ (10ポイント) \n\nプレゼンテーション（合計20点） \n\n文章は明確で専門用語がありませんか? (5点) \n\n提供されたチャートや表は興味深く、視覚的に魅力的で、正確ですか? (5点) \n\nプレゼンテーション全体を通して分析スレッドをたどることができますか? (10ポイント)","metadata":{}},{"cell_type":"markdown","source":"今回のコンペは予測のスコアを競うようなものではなさそう。","metadata":{}},{"cell_type":"code","source":"#モジュールの読み込み\nimport numpy as np\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport plotly.express as px\n\nimport sklearn ","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:26:21.586544Z","iopub.execute_input":"2022-09-17T20:26:21.587434Z","iopub.status.idle":"2022-09-17T20:26:23.499938Z","shell.execute_reply.started":"2022-09-17T20:26:21.587339Z","shell.execute_reply":"2022-09-17T20:26:23.498722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#データ型\ndtype_dict = {\"track_id\": object ,\n              #'race_date' : np.datetime64,\n              \"race_number\" : np.int8, \n              \"program_number\" : object,\n              \"trakus_index\" : np.int16,  \n              \"latitude\" : np.float64,\n              \"longitude\" : np.float64,\n              \"distance_id\" : np.int16,  \n              \"course_type\" : object, \n              \"track_condition\" : object,\n              \"run_up_distance\" : np.int8,  \n              \"race_type\" : object, \n              \"purse\" : np.int32,  \n              \"post_time\" : np.int16,  \n              \"weight_carried\" : np.int16,  \n              \"jockey\" : object, \n              \"odds\" : np.int16,\n             }","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:26:23.501460Z","iopub.execute_input":"2022-09-17T20:26:23.501935Z","iopub.status.idle":"2022-09-17T20:26:23.509843Z","shell.execute_reply.started":"2022-09-17T20:26:23.501900Z","shell.execute_reply":"2022-09-17T20:26:23.508336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#start_data列名\nstart_name = [\"track_id\",\"race_date\",\"race_number\",\"program_number\",\"weight_carried\",\"jockey\",\"odds\",\"position_at_finish\"]\n#total_data列名\ntotal_name = [\"track_id\",\"race_date\",\"race_number\",\"program_number\",\"trakus_index\",\"latitude\",\"longitude\",\"distance_id\",\"course_type\",\"track_condition\",\"run_up_distance\",\"race_type\",\"purse\",\"post_time\",\"weight_carried\",\"jockey\",\"odds\",\"position_at_finish\"]","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:26:23.512043Z","iopub.execute_input":"2022-09-17T20:26:23.513069Z","iopub.status.idle":"2022-09-17T20:26:23.524300Z","shell.execute_reply.started":"2022-09-17T20:26:23.513013Z","shell.execute_reply":"2022-09-17T20:26:23.522736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#データの読み込み\ntrack_data = pd.read_csv(\"/kaggle/input/big-data-derby-2022/nyra_tracking_table.csv\", dtype=dtype_dict, parse_dates=[\"race_date\"]) #追跡データ\nstart_data = pd.read_csv(\"/kaggle/input/big-data-derby-2022/nyra_start_table.csv\", header = None) #競馬騎手レースデータ\nrace_data = pd.read_csv(\"/kaggle/input/big-data-derby-2022/nyra_race_table.csv\", dtype=dtype_dict, parse_dates=[\"race_date\"]) #競馬場のレースデータ\ntotal_data = pd.read_csv(\"/kaggle/input/big-data-derby-2022/nyra_2019_complete.csv\", header=None) #結合データ","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:26:23.527882Z","iopub.execute_input":"2022-09-17T20:26:23.528280Z","iopub.status.idle":"2022-09-17T20:26:54.994548Z","shell.execute_reply.started":"2022-09-17T20:26:23.528243Z","shell.execute_reply":"2022-09-17T20:26:54.993158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#列名をつける\nstart_data.columns = start_name\ntotal_data.columns = total_name","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:26:54.996013Z","iopub.execute_input":"2022-09-17T20:26:54.996432Z","iopub.status.idle":"2022-09-17T20:26:55.003367Z","shell.execute_reply.started":"2022-09-17T20:26:54.996393Z","shell.execute_reply":"2022-09-17T20:26:55.001750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total_data","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:26:55.005126Z","iopub.execute_input":"2022-09-17T20:26:55.005640Z","iopub.status.idle":"2022-09-17T20:26:55.052347Z","shell.execute_reply.started":"2022-09-17T20:26:55.005600Z","shell.execute_reply":"2022-09-17T20:26:55.051413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#データの確認\nrace_data[(race_data[\"distance_id\"]==600) & (race_data[\"run_up_distance\"]==48) & (race_data[\"race_type\"]==\"CLM\")]","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:26:55.053701Z","iopub.execute_input":"2022-09-17T20:26:55.054534Z","iopub.status.idle":"2022-09-17T20:26:55.084237Z","shell.execute_reply.started":"2022-09-17T20:26:55.054486Z","shell.execute_reply":"2022-09-17T20:26:55.082845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"コンペの説明ページではnyra_2019_complete.csvのカラムが計17と記載あるが実際には18だった。抜けているのはnyra_race_table.csvにあるpurce。","metadata":{}},{"cell_type":"markdown","source":"nyra_start_table.csv \n\ntrack_id - レースが行われたトラックの 3 文字の ID。 AQU -水道橋、BEL - ベルモント、SAR - サラトガ。 \n\nrace_date - レースが行われた日付。 YYYY-MM-DD。 \n\nrace_number - レース番号。 3 文字として渡されますが、このデータ セットの int にキャストまたは変換できます。 \n\nprogram_number - 3 文字で渡されるレースの馬のプログラム番号。数字だけに限定されないため、3 文字のままにする必要があります。基本的に、レースでの馬の一意の識別子です。 \n\nweight_carried - レースで馬が運ぶ重量の整数。 \n\nジョッキー - レースで馬に乗ったジョッキーの名前。最大50文字 \n\nオッズ - 整数として渡されるレースに勝つオッズ。 100 で割り、オッズを 1 にします。例 - 1280 は 12.8-1 になります。 \n\nposition_at_finish - 馬のフィニッシュ位置の整数。 (22/9/8 データセット追加) \n\nnyra_race_table.csv \n\ntrack_id - レースが行われたトラックの 3 文字の ID。 AQU -水道橋、BEL - ベルモント、SAR - サラトガ。 \n\nrace_date - レースが行われた日付。 YYYY-MM-DD。 \n\nrace_number - レース番号。 3 文字として渡されますが、このデータ セットの int にキャストまたは変換できます。 \n\ndistance_id - 整数として渡されるハロン単位のレースの距離。例 - 600 は 6 ハロンになります。 \n\ncourse_type - レースが通過したコースが 1 文字として渡されます。 M - ハードル、D - ダート、O - アウター ターフ、I - インナー ターフ、T - ターフ。 \n\ntrack_condition - レースが行われたコースの状態が 3 文字で渡されます。 YL - イールド、FM - しっかり、SY - ずさん、GD - 良い、FT - 速い、MY - 泥だらけ、\nSF - 柔らかい。 \n\nrun_up_distance - 整数として渡されるレース開始までのゲートからフィート単位の距離。 \n\nrace_type - レースの分類は 5 文字として渡されます。 STK - ステークス、WCL - 権利放棄請求、WMC - 権利放棄乙女請求、SST - スターター ステークス、SHP - スターター ハンディキャップ、CLM - 請求、STR - スターター許可、AOC - オプション許可請求、SOC - スターター オプション請求、MCL - 乙女請求、ALW - 手当、MSW - メイデン スペシャル ウェイト。 財布 - 小数点以下 2 桁のお金として渡されたレースの米ドルの財布。 \n\npost_time - レースが開始された時刻が 5 文字として渡されます。例 - 01220 は 12:20 になります。 \n\nnyra_tracking_table.csv \n\ntrack_id - レースが行われたトラックの 3 文字の ID。 AQU -水道橋、BEL - ベルモント、SAR - サラトガ。 \n\nrace_date - レースが行われた日付。 YYYY-MM-DD。 \n\nrace_number - レース番号。 3 文字として渡されますが、このデータ セットの int にキャストまたは変換できます。 \n\nprogram_number - 3 文字で渡されるレースの馬のプログラム番号。数字だけに限定されないため、3 文字のままにする必要があります。基本的に、レースでの馬の一意の識別子です。 \n\ntrakus_index - 整数として渡されるレースでの馬の緯度/経度のポイントの共通コレクション。私たちが知る限り、それは 0.25 秒ごとに収集されます。 \n\nlatitude - レースでの馬の緯度は float として渡されます。 経度 - レース中の馬の経度が float として渡されます。","metadata":{}},{"cell_type":"markdown","source":"## データの確認","metadata":{}},{"cell_type":"code","source":"#データを確認\nprint(total_data.shape)\ntotal_data.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:26:55.086053Z","iopub.execute_input":"2022-09-17T20:26:55.086516Z","iopub.status.idle":"2022-09-17T20:26:55.109706Z","shell.execute_reply.started":"2022-09-17T20:26:55.086477Z","shell.execute_reply":"2022-09-17T20:26:55.108163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total_data.info()","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:26:55.111186Z","iopub.execute_input":"2022-09-17T20:26:55.111587Z","iopub.status.idle":"2022-09-17T20:26:55.131423Z","shell.execute_reply.started":"2022-09-17T20:26:55.111554Z","shell.execute_reply":"2022-09-17T20:26:55.130079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#欠損値の確認\ntotal_data.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:26:55.133497Z","iopub.execute_input":"2022-09-17T20:26:55.134229Z","iopub.status.idle":"2022-09-17T20:26:56.751599Z","shell.execute_reply.started":"2022-09-17T20:26:55.134178Z","shell.execute_reply":"2022-09-17T20:26:56.750178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#レース中の位置情報によりデータが重複しているため重複削除\ndata = total_data.copy()\ndata = data.drop([\"trakus_index\",\"latitude\",\"longitude\"],axis=1)\nprint(\"重複削除前:\" + str(data.shape))\ndata = data.drop_duplicates()\nprint(\"重複削除後:\" + str(data.shape))","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:26:56.753135Z","iopub.execute_input":"2022-09-17T20:26:56.753576Z","iopub.status.idle":"2022-09-17T20:27:03.620094Z","shell.execute_reply.started":"2022-09-17T20:26:56.753538Z","shell.execute_reply":"2022-09-17T20:27:03.613397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#基本統計量の確認\ndata.describe()","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:27:03.627337Z","iopub.execute_input":"2022-09-17T20:27:03.628652Z","iopub.status.idle":"2022-09-17T20:27:03.810347Z","shell.execute_reply.started":"2022-09-17T20:27:03.628600Z","shell.execute_reply":"2022-09-17T20:27:03.805422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#EDA","metadata":{}},{"cell_type":"code","source":"#race_dataを日付型に変換\ntotal_data[\"race_date\"] = pd.to_datetime(total_data[\"race_date\"])\ndata[\"race_date\"] = pd.to_datetime(data[\"race_date\"])","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:27:03.839344Z","iopub.execute_input":"2022-09-17T20:27:03.841660Z","iopub.status.idle":"2022-09-17T20:27:05.005308Z","shell.execute_reply.started":"2022-09-17T20:27:03.841565Z","shell.execute_reply":"2022-09-17T20:27:05.002165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#月のデータを作成\ndef extrack_month(x):\n    return x.month\n\ntotal_data[\"month\"] = total_data[\"race_date\"].apply(extrack_month)\ndata[\"month\"] = data[\"race_date\"].apply(extrack_month)","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:27:05.007746Z","iopub.execute_input":"2022-09-17T20:27:05.009779Z","iopub.status.idle":"2022-09-17T20:27:30.027952Z","shell.execute_reply.started":"2022-09-17T20:27:05.009544Z","shell.execute_reply":"2022-09-17T20:27:30.026734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"まずは平均的にオッズの高い順位の騎手をみてみる","metadata":{}},{"cell_type":"code","source":"tmp = data.groupby(\"odds\").agg({\"track_id\":\"count\"}).reset_index()\nsns.barplot(data=tmp, x=\"odds\", y=\"track_id\")","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:27:30.029550Z","iopub.execute_input":"2022-09-17T20:27:30.030060Z","iopub.status.idle":"2022-09-17T20:27:38.966452Z","shell.execute_reply.started":"2022-09-17T20:27:30.030014Z","shell.execute_reply":"2022-09-17T20:27:38.965259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tmp = data.groupby(\"jockey\").agg({\"odds\":\"mean\",\"race_date\":\"count\"}).reset_index().sort_values(by=\"odds\")","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:27:38.967887Z","iopub.execute_input":"2022-09-17T20:27:38.968231Z","iopub.status.idle":"2022-09-17T20:27:38.983157Z","shell.execute_reply.started":"2022-09-17T20:27:38.968199Z","shell.execute_reply":"2022-09-17T20:27:38.981860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.barplot(data=tmp, x=\"jockey\", y=\"odds\")\nplt.xticks(rotation=90);","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:27:38.984817Z","iopub.execute_input":"2022-09-17T20:27:38.985217Z","iopub.status.idle":"2022-09-17T20:27:42.309990Z","shell.execute_reply.started":"2022-09-17T20:27:38.985183Z","shell.execute_reply":"2022-09-17T20:27:42.308586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"騎手の名前までは見えなかった...\n騎手によりoddsに差があるためレースに勝つためには騎手は重要な要因となる。","metadata":{}},{"cell_type":"code","source":"tmp.rename(columns={\"race_date\":\"appearances\"},inplace=True)\nsns.histplot(data=tmp[\"appearances\"])\nplt.xlim(0,100)\nplt.xticks(rotation=90);","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:27:42.311840Z","iopub.execute_input":"2022-09-17T20:27:42.313241Z","iopub.status.idle":"2022-09-17T20:27:42.924294Z","shell.execute_reply.started":"2022-09-17T20:27:42.313168Z","shell.execute_reply":"2022-09-17T20:27:42.922888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"データ内のほとんどの騎手は0~20回の総出場回数となる","metadata":{}},{"cell_type":"code","source":"tmp.rename(columns={\"race_date\":\"appearances\"},inplace=True)\nsns.histplot(data=tmp[\"appearances\"])\nplt.xlim(0,50)\nplt.xticks(rotation=90);","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:27:42.926379Z","iopub.execute_input":"2022-09-17T20:27:42.926895Z","iopub.status.idle":"2022-09-17T20:27:43.548009Z","shell.execute_reply.started":"2022-09-17T20:27:42.926849Z","shell.execute_reply":"2022-09-17T20:27:43.546731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"総出場回数が2000回までの騎手に絞ると1~5回まで約100人存在している","metadata":{}},{"cell_type":"code","source":"sns.scatterplot(data=tmp, x=\"appearances\", y=\"odds\")","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:27:43.549686Z","iopub.execute_input":"2022-09-17T20:27:43.550128Z","iopub.status.idle":"2022-09-17T20:27:43.802404Z","shell.execute_reply.started":"2022-09-17T20:27:43.550090Z","shell.execute_reply":"2022-09-17T20:27:43.801285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"総出場回数と平均順位に相関がありそう\n出場回数が多いほど平均順位が良くなり一番出場回数の多い騎手はoddsが低くなる\n出場回数のヒストグラムより出場回数が少なくても散布図の０近傍でoddsが低い騎手がいる\n出場回数が少ないと若手選手の可能性があり大穴としてoddsが高いイメージがあるが...","metadata":{}},{"cell_type":"markdown","source":"#レースで運ぶ重さに変化はあるのか","metadata":{}},{"cell_type":"code","source":"tmp = data.groupby(\"track_id\").agg({\"weight_carried\":\"mean\"}).reset_index().sort_values(by=\"weight_carried\")\ntmp","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:27:43.803998Z","iopub.execute_input":"2022-09-17T20:27:43.804344Z","iopub.status.idle":"2022-09-17T20:27:43.820742Z","shell.execute_reply.started":"2022-09-17T20:27:43.804313Z","shell.execute_reply":"2022-09-17T20:27:43.819761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.barplot(data=tmp, x=\"track_id\", y=\"weight_carried\")\nplt.xticks(rotation=90);","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:27:43.822619Z","iopub.execute_input":"2022-09-17T20:27:43.822963Z","iopub.status.idle":"2022-09-17T20:27:44.027811Z","shell.execute_reply.started":"2022-09-17T20:27:43.822926Z","shell.execute_reply":"2022-09-17T20:27:44.026625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#選手毎のweight_cattiedを確認\ntmp = data.groupby(\"jockey\").agg({\"weight_carried\":\"mean\"}).reset_index()\nsns.boxplot(data=tmp[\"weight_carried\"])","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:27:44.029091Z","iopub.execute_input":"2022-09-17T20:27:44.029921Z","iopub.status.idle":"2022-09-17T20:27:44.212261Z","shell.execute_reply.started":"2022-09-17T20:27:44.029887Z","shell.execute_reply":"2022-09-17T20:27:44.211134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"レースで馬が運ぶ重さはほとんど変化がなくほぼ一緒と考えて良さそう。\n若干SARの時に重くなる\n騎手べつにみると下振れ、上ぶれしている騎手が確認できる。","metadata":{}},{"cell_type":"code","source":"#上振れしている騎手を確認\ntmp1 = tmp[tmp[\"weight_carried\"] > 130]\ntmp2 = pd.merge(data,tmp1,on=\"jockey\",how=\"inner\") #該当する選手のみ抽出\ntmp2","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:27:44.214066Z","iopub.execute_input":"2022-09-17T20:27:44.215926Z","iopub.status.idle":"2022-09-17T20:27:44.260441Z","shell.execute_reply.started":"2022-09-17T20:27:44.215865Z","shell.execute_reply":"2022-09-17T20:27:44.258868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"distance_id,course_type,track_conditionが同じ値そう","metadata":{}},{"cell_type":"code","source":"#確認\ntmp2.groupby([\"distance_id\",\"course_type\",\"track_condition\"]).agg({\"track_id\":\"count\"})","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:27:44.262977Z","iopub.execute_input":"2022-09-17T20:27:44.263838Z","iopub.status.idle":"2022-09-17T20:27:44.279117Z","shell.execute_reply.started":"2022-09-17T20:27:44.263689Z","shell.execute_reply":"2022-09-17T20:27:44.277819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"distance_id,track_conditionはたまたま一緒だっただけでした。\ncource_typeがMのものがweight_carriedの外れ値に当たるものと考えて良さそう。","metadata":{}},{"cell_type":"markdown","source":"レース中の座標軸","metadata":{}},{"cell_type":"code","source":"#月ごとのデータ数を確認\ntotal_data.groupby([\"track_id\",\"month\"]).agg({\"track_id\":\"count\"})","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:27:44.280822Z","iopub.execute_input":"2022-09-17T20:27:44.281216Z","iopub.status.idle":"2022-09-17T20:27:45.069315Z","shell.execute_reply.started":"2022-09-17T20:27:44.281183Z","shell.execute_reply":"2022-09-17T20:27:45.068453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"track_id毎に月のデータ数が異なる\nAQUは11月が一番多く、BELは6月、SARは7月","metadata":{}},{"cell_type":"code","source":"tmp = total_data[total_data[\"track_id\"]==\"AQU\"]\ntmp1 = tmp[tmp[\"month\"] == 11]\n\nplt.figure(figsize=(20,10))\nsns.scatterplot(data=tmp1, x=\"longitude\", y=\"latitude\", hue=\"race_number\")\nplt.title(\"track_id:AQU\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:27:45.070656Z","iopub.execute_input":"2022-09-17T20:27:45.071636Z","iopub.status.idle":"2022-09-17T20:28:01.997128Z","shell.execute_reply.started":"2022-09-17T20:27:45.071598Z","shell.execute_reply":"2022-09-17T20:28:01.996168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tmp = total_data[total_data[\"track_id\"]==\"BEL\"]\ntmp1 = tmp[tmp[\"month\"] == 6]\n\nplt.figure(figsize=(20,10))\nsns.scatterplot(data=tmp1, x=\"longitude\", y=\"latitude\", hue=\"race_number\")\nplt.title(\"track_id:BEL\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:28:01.998338Z","iopub.execute_input":"2022-09-17T20:28:01.999407Z","iopub.status.idle":"2022-09-17T20:28:14.718788Z","shell.execute_reply.started":"2022-09-17T20:28:01.999371Z","shell.execute_reply":"2022-09-17T20:28:14.717517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tmp = total_data[total_data[\"track_id\"]==\"SAR\"]\ntmp1 = tmp[tmp[\"month\"] == 7]\n\nplt.figure(figsize=(20,10))\nsns.scatterplot(data=tmp1, x=\"longitude\", y=\"latitude\", hue=\"race_number\")\nplt.title(\"track_id:SAR\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:28:14.720379Z","iopub.execute_input":"2022-09-17T20:28:14.720870Z","iopub.status.idle":"2022-09-17T20:28:25.180106Z","shell.execute_reply.started":"2022-09-17T20:28:14.720827Z","shell.execute_reply":"2022-09-17T20:28:25.178799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"平均順位の良い騎手でみてみる","metadata":{}},{"cell_type":"code","source":"#騎手毎に平均oddsを算出し成績優秀な選手と成績が悪い選手を算出\ntmp = data.groupby(\"jockey\").agg({\"odds\":\"mean\",\"track_id\":\"count\"}).reset_index()\nprint(tmp[tmp[\"odds\"]>7000].sort_values(by=\"track_id\", ascending=False))\nprint(tmp[tmp[\"odds\"]<500].sort_values(by=\"track_id\",ascending=False))","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:28:25.181408Z","iopub.execute_input":"2022-09-17T20:28:25.181723Z","iopub.status.idle":"2022-09-17T20:28:25.225664Z","shell.execute_reply.started":"2022-09-17T20:28:25.181695Z","shell.execute_reply":"2022-09-17T20:28:25.224488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"出場回数5回、平均odds293.0という騎手発見[Shaun Bridgmohan]\n出場回数15回、平均odds7075.0の騎手発見[Talbert Howell]","metadata":{}},{"cell_type":"code","source":"tmp = total_data[total_data[\"track_id\"]==\"BEL\"]\ntmp1 = tmp[tmp[\"jockey\"] == \"Shaun Bridgmohan\"]\n\nplt.figure(figsize=(20,10))\nsns.scatterplot(data=tmp1, x=\"longitude\", y=\"latitude\", hue=\"race_number\")\nplt.title(\"jockey:Shaun Bridgmohan\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:28:25.227037Z","iopub.execute_input":"2022-09-17T20:28:25.227375Z","iopub.status.idle":"2022-09-17T20:28:26.335702Z","shell.execute_reply.started":"2022-09-17T20:28:25.227329Z","shell.execute_reply":"2022-09-17T20:28:26.334431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tmp = total_data[total_data[\"track_id\"]==\"BEL\"]\ntmp1 = tmp[tmp[\"jockey\"] == \"Talbert Howell\"]\n\nplt.figure(figsize=(20,10))\nsns.scatterplot(data=tmp1, x=\"longitude\", y=\"latitude\", hue=\"race_number\")\nplt.title(\"jockey:Talbert Howell\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:28:26.337136Z","iopub.execute_input":"2022-09-17T20:28:26.337613Z","iopub.status.idle":"2022-09-17T20:28:27.569188Z","shell.execute_reply.started":"2022-09-17T20:28:26.337579Z","shell.execute_reply":"2022-09-17T20:28:27.567880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"騎手別にコースの動き方を確認","metadata":{}},{"cell_type":"code","source":"#AQUで出場回数の多い騎手で比較してみる\ntmp = data[data[\"track_id\"]==\"AQU\"]\ntmp.groupby(\"jockey\").agg({\"odds\":\"mean\",\"track_id\":\"count\"}).sort_values(by=\"track_id\").tail()","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:28:27.570699Z","iopub.execute_input":"2022-09-17T20:28:27.571956Z","iopub.status.idle":"2022-09-17T20:28:27.640305Z","shell.execute_reply.started":"2022-09-17T20:28:27.571917Z","shell.execute_reply":"2022-09-17T20:28:27.639078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tmp = total_data[total_data[\"track_id\"]==\"AQU\"]\ntmp1 = tmp[tmp[\"jockey\"] == \"Manuel Franco\"]\n\nplt.figure(figsize=(20,10))\nsns.scatterplot(data=tmp1, x=\"longitude\", y=\"latitude\", hue=\"race_number\")\nplt.title(\"jockey:Manuel Franco\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:28:27.641875Z","iopub.execute_input":"2022-09-17T20:28:27.642298Z","iopub.status.idle":"2022-09-17T20:28:33.912518Z","shell.execute_reply.started":"2022-09-17T20:28:27.642263Z","shell.execute_reply":"2022-09-17T20:28:33.911314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tmp = total_data[total_data[\"track_id\"]==\"AQU\"]\ntmp1 = tmp[tmp[\"jockey\"] == \"Eric Cancel\"]\n\nplt.figure(figsize=(20,10))\nsns.scatterplot(data=tmp1, x=\"longitude\", y=\"latitude\", hue=\"race_number\")\nplt.title(\"jockey:Eric Cancel\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:28:33.914343Z","iopub.execute_input":"2022-09-17T20:28:33.914804Z","iopub.status.idle":"2022-09-17T20:28:38.644934Z","shell.execute_reply.started":"2022-09-17T20:28:33.914766Z","shell.execute_reply":"2022-09-17T20:28:38.643619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Manuel Francoはコーナー付近で走りにブレがある\nEric CancelはManuel Francoと比べてブレが少なそうだが","metadata":{}},{"cell_type":"markdown","source":"#各種データのプロット確認","metadata":{}},{"cell_type":"code","source":"data.columns","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:28:38.646679Z","iopub.execute_input":"2022-09-17T20:28:38.647057Z","iopub.status.idle":"2022-09-17T20:28:38.654706Z","shell.execute_reply.started":"2022-09-17T20:28:38.647023Z","shell.execute_reply":"2022-09-17T20:28:38.653407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#course_type\ntmp = data.groupby(\"course_type\").agg({\"track_id\":\"count\"}).reset_index().rename(columns={\"track_id\":\"size\"})\nsns.barplot(data=tmp, x=\"course_type\", y=\"size\")","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:28:38.656654Z","iopub.execute_input":"2022-09-17T20:28:38.657056Z","iopub.status.idle":"2022-09-17T20:28:38.875236Z","shell.execute_reply.started":"2022-09-17T20:28:38.657019Z","shell.execute_reply":"2022-09-17T20:28:38.874146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Dが一番多い。次に多いのがIとTでありMはほとんどない","metadata":{}},{"cell_type":"code","source":"#track_condition\ntmp = data.groupby(\"track_condition\").agg({\"track_id\":\"count\"}).reset_index().rename(columns={\"track_id\":\"size\"})\nsns.barplot(data=tmp, x=\"track_condition\", y=\"size\")","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:28:38.877085Z","iopub.execute_input":"2022-09-17T20:28:38.877487Z","iopub.status.idle":"2022-09-17T20:28:39.136514Z","shell.execute_reply.started":"2022-09-17T20:28:38.877429Z","shell.execute_reply":"2022-09-17T20:28:39.135114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"FTが一番多く、FM,GD SYとなっている。MY、YLはほとんどない","metadata":{}},{"cell_type":"markdown","source":"同一レースの走り方をみてみる","metadata":{}},{"cell_type":"code","source":"total_data.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:28:39.138409Z","iopub.execute_input":"2022-09-17T20:28:39.138768Z","iopub.status.idle":"2022-09-17T20:28:39.165933Z","shell.execute_reply.started":"2022-09-17T20:28:39.138738Z","shell.execute_reply":"2022-09-17T20:28:39.164195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total_data.columns","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:28:39.173983Z","iopub.execute_input":"2022-09-17T20:28:39.174481Z","iopub.status.idle":"2022-09-17T20:28:39.182422Z","shell.execute_reply.started":"2022-09-17T20:28:39.174445Z","shell.execute_reply":"2022-09-17T20:28:39.180914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tmp = total_data[(total_data[\"track_id\"]==\"AQU\") & (total_data[\"race_number\"]==9) & (total_data[\"race_date\"]== \"2019-01-01\") ]\n\nplt.figure(figsize=(20,10))\nsns.scatterplot(data=tmp1, x=\"longitude\", y=\"latitude\", hue=\"program_number\")\nplt.title(\"race:2019-01-01_number9\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:28:39.184511Z","iopub.execute_input":"2022-09-17T20:28:39.184913Z","iopub.status.idle":"2022-09-17T20:28:47.315954Z","shell.execute_reply.started":"2022-09-17T20:28:39.184879Z","shell.execute_reply":"2022-09-17T20:28:47.314897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tmp = total_data[(total_data[\"track_id\"]==\"AQU\") & (total_data[\"race_number\"]==1) & (total_data[\"race_date\"]== \"2019-04-28\") ]\n\nplt.figure(figsize=(20,10))\nsns.scatterplot(data=tmp1, x=\"longitude\", y=\"latitude\", hue=\"program_number\")\nplt.title(\"race:2019-04-28_number9\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:28:47.317383Z","iopub.execute_input":"2022-09-17T20:28:47.317727Z","iopub.status.idle":"2022-09-17T20:28:55.391309Z","shell.execute_reply.started":"2022-09-17T20:28:47.317697Z","shell.execute_reply":"2022-09-17T20:28:55.390137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"日付を変えてレースを確認\n素人目だがぱっと見同じような走行をしている気がする\nコースが変わっている訳ではないので当然といえば当然か・・・","metadata":{}},{"cell_type":"markdown","source":"#コンディションでも比べてみる","metadata":{}},{"cell_type":"code","source":"total_data[\"track_condition\"].unique()","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:28:55.392947Z","iopub.execute_input":"2022-09-17T20:28:55.393764Z","iopub.status.idle":"2022-09-17T20:28:55.722515Z","shell.execute_reply.started":"2022-09-17T20:28:55.393729Z","shell.execute_reply":"2022-09-17T20:28:55.721322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#競技場のコンディションによる違いを確認\ntotal_data[total_data[\"track_condition\"] == 'YL ']","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:28:55.724389Z","iopub.execute_input":"2022-09-17T20:28:55.725146Z","iopub.status.idle":"2022-09-17T20:28:56.113179Z","shell.execute_reply.started":"2022-09-17T20:28:55.725099Z","shell.execute_reply":"2022-09-17T20:28:56.112068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tmp = total_data[(total_data[\"track_id\"]==\"BEL\") & (total_data[\"race_number\"]==7) & (total_data[\"race_date\"]== \"2019-09-06\") ]\n\nplt.figure(figsize=(20,10))\nsns.scatterplot(data=tmp1, x=\"longitude\", y=\"latitude\", hue=\"program_number\")\nplt.title(\"race:2019-09-06_number7\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:28:56.114737Z","iopub.execute_input":"2022-09-17T20:28:56.115083Z","iopub.status.idle":"2022-09-17T20:29:04.144474Z","shell.execute_reply.started":"2022-09-17T20:28:56.115052Z","shell.execute_reply":"2022-09-17T20:29:04.143569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#競技場のコンディションによる違いを確認\ntotal_data[total_data[\"track_condition\"] == 'FM ']","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:29:04.146044Z","iopub.execute_input":"2022-09-17T20:29:04.146653Z","iopub.status.idle":"2022-09-17T20:29:04.762682Z","shell.execute_reply.started":"2022-09-17T20:29:04.146616Z","shell.execute_reply":"2022-09-17T20:29:04.761635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tmp = total_data[(total_data[\"track_id\"]==\"BEL\") & (total_data[\"race_number\"]==5) & (total_data[\"race_date\"]== \"2019-09-13\") ]\n\nplt.figure(figsize=(20,10))\nsns.scatterplot(data=tmp1, x=\"longitude\", y=\"latitude\", hue=\"program_number\")\nplt.title(\"race:2019-09-13_number5\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-17T20:29:04.764310Z","iopub.execute_input":"2022-09-17T20:29:04.764802Z","iopub.status.idle":"2022-09-17T20:29:12.887316Z","shell.execute_reply.started":"2022-09-17T20:29:04.764758Z","shell.execute_reply":"2022-09-17T20:29:12.885566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"同一競技場のコンディションによる違いもぱっと見は確認できない","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}