{"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-25T12:58:47.868504Z","iopub.execute_input":"2022-09-25T12:58:47.869474Z","iopub.status.idle":"2022-09-25T12:58:47.907245Z","shell.execute_reply.started":"2022-09-25T12:58:47.869334Z","shell.execute_reply":"2022-09-25T12:58:47.905919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df0 = pd.read_csv('/kaggle/input/big-data-derby-2022/nyra_tracking_table.csv')\ndf0 = df0.iloc[:,:5]\ndf0  = df0[df0['track_id'] == 'AQU']\ndf0.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-25T12:58:47.910092Z","iopub.execute_input":"2022-09-25T12:58:47.911064Z","iopub.status.idle":"2022-09-25T12:58:57.501851Z","shell.execute_reply.started":"2022-09-25T12:58:47.911012Z","shell.execute_reply":"2022-09-25T12:58:57.500734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1 = pd.read_csv('/kaggle/input/big-data-derby-2022/nyra_start_table.csv')\ndf1.columns = [\"track_id\",\"race_date\",\"race_number\",\"program_number\",\"weight_carried\",\"jockey\",\"odds\",\"position_at_finish\"]\ndf1.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-25T12:58:57.503180Z","iopub.execute_input":"2022-09-25T12:58:57.503510Z","iopub.status.idle":"2022-09-25T12:58:57.550160Z","shell.execute_reply.started":"2022-09-25T12:58:57.503480Z","shell.execute_reply":"2022-09-25T12:58:57.548821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2 = pd.read_csv('/kaggle/input/big-data-derby-2022/nyra_race_table.csv')\ndf2  = df2[df2['track_id'] == 'AQU']\ndf2  = df2[df2['course_type'] == 'D']\ndf2.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-25T12:58:57.552426Z","iopub.execute_input":"2022-09-25T12:58:57.552795Z","iopub.status.idle":"2022-09-25T12:58:57.583338Z","shell.execute_reply.started":"2022-09-25T12:58:57.552763Z","shell.execute_reply":"2022-09-25T12:58:57.582023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df4 = pd.merge(df0, df1, on=[\"track_id\",\"race_date\",\"race_number\",\"program_number\"], how='left')\ndf4.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-25T12:58:57.584689Z","iopub.execute_input":"2022-09-25T12:58:57.585166Z","iopub.status.idle":"2022-09-25T12:58:58.620416Z","shell.execute_reply.started":"2022-09-25T12:58:57.585123Z","shell.execute_reply":"2022-09-25T12:58:58.619062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_merge = pd.merge(df4, df2, on=[\"track_id\",\"race_date\",\"race_number\"], how='left')\ndf_merge = df_merge.dropna(subset=['position_at_finish'])\ndf_merge.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-25T12:58:58.621760Z","iopub.execute_input":"2022-09-25T12:58:58.622085Z","iopub.status.idle":"2022-09-25T12:59:00.886785Z","shell.execute_reply.started":"2022-09-25T12:58:58.622056Z","shell.execute_reply":"2022-09-25T12:59:00.885666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#予測のターゲット（レース順位）\ntarget_df = df_merge[[\"position_at_finish\"]]","metadata":{"execution":{"iopub.status.busy":"2022-09-25T12:59:00.888202Z","iopub.execute_input":"2022-09-25T12:59:00.888635Z","iopub.status.idle":"2022-09-25T12:59:00.898318Z","shell.execute_reply.started":"2022-09-25T12:59:00.888599Z","shell.execute_reply":"2022-09-25T12:59:00.897213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nsns.set(rc={'figure.figsize':(24,8)})\nax=sns.lineplot(data=df_merge,x='race_date',y='position_at_finish',hue='weight_carried')\nax.axes.set_title(\"\\weight_carried\\n\",fontsize=20)","metadata":{"execution":{"iopub.status.busy":"2022-09-25T12:59:00.899472Z","iopub.execute_input":"2022-09-25T12:59:00.900559Z","iopub.status.idle":"2022-09-25T12:59:51.900380Z","shell.execute_reply.started":"2022-09-25T12:59:00.900507Z","shell.execute_reply":"2022-09-25T12:59:51.899530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#特徴量\ntrain_df = df_merge.drop(\"position_at_finish\",axis=1)\ntrain_df = train_df.drop(\"track_id\",axis=1)\ntrain_df = train_df.drop(\"race_date\",axis=1)\ntrain_df = train_df.drop(\"race_number\",axis=1)\ntrain_df = train_df.drop(\"program_number\",axis=1)\ntrain_df = pd.get_dummies(train_df, drop_first=True)\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-24T23:48:56.047647Z","iopub.execute_input":"2022-09-24T23:48:56.047986Z","iopub.status.idle":"2022-09-24T23:48:59.733670Z","shell.execute_reply.started":"2022-09-24T23:48:56.047955Z","shell.execute_reply":"2022-09-24T23:48:59.732355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test = train_test_split(train_df, target_df, test_size=0.3)","metadata":{"execution":{"iopub.status.busy":"2022-09-24T23:48:59.734941Z","iopub.execute_input":"2022-09-24T23:48:59.735271Z","iopub.status.idle":"2022-09-24T23:49:03.995712Z","shell.execute_reply.started":"2022-09-24T23:48:59.735241Z","shell.execute_reply":"2022-09-24T23:49:03.993911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import xgboost as xgb","metadata":{"execution":{"iopub.status.busy":"2022-09-24T23:49:03.997358Z","iopub.execute_input":"2022-09-24T23:49:03.997772Z","iopub.status.idle":"2022-09-24T23:49:04.033960Z","shell.execute_reply.started":"2022-09-24T23:49:03.997737Z","shell.execute_reply":"2022-09-24T23:49:04.032410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dtrain = xgb.DMatrix(X_train, y_train)\ndvalid = xgb.DMatrix(X_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2022-09-24T23:49:04.035998Z","iopub.execute_input":"2022-09-24T23:49:04.036389Z","iopub.status.idle":"2022-09-24T23:49:12.340204Z","shell.execute_reply.started":"2022-09-24T23:49:04.036355Z","shell.execute_reply":"2022-09-24T23:49:12.339156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"params = {\n    \"objective\" : \"reg:squarederror\",\n    \"eval_metric\" : \"rmse\"\n}","metadata":{"execution":{"iopub.status.busy":"2022-09-24T23:49:12.341994Z","iopub.execute_input":"2022-09-24T23:49:12.342744Z","iopub.status.idle":"2022-09-24T23:49:12.347354Z","shell.execute_reply.started":"2022-09-24T23:49:12.342704Z","shell.execute_reply":"2022-09-24T23:49:12.346382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = xgb.train(\n    params = params,\n    dtrain = dtrain,\n    evals = [(dtrain, \"train\"), (dvalid, \"valid\")],\n    num_boost_round = 100\n)","metadata":{"execution":{"iopub.status.busy":"2022-09-24T23:49:12.351497Z","iopub.execute_input":"2022-09-24T23:49:12.352215Z","iopub.status.idle":"2022-09-24T23:57:02.828061Z","shell.execute_reply.started":"2022-09-24T23:49:12.352161Z","shell.execute_reply":"2022-09-24T23:57:02.826693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(xgb.DMatrix(X_test))\nprint(pred)","metadata":{"execution":{"iopub.status.busy":"2022-09-24T23:57:02.830323Z","iopub.execute_input":"2022-09-24T23:57:02.831253Z","iopub.status.idle":"2022-09-24T23:57:05.749771Z","shell.execute_reply.started":"2022-09-24T23:57:02.831203Z","shell.execute_reply":"2022-09-24T23:57:05.748513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\nplt.scatter(y_test, pred, alpha = 0.5)\nplt.plot(np.linspace(0, 20, 100), np.linspace(0, 20, 100), \"red\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-24T23:57:05.751645Z","iopub.execute_input":"2022-09-24T23:57:05.752157Z","iopub.status.idle":"2022-09-24T23:57:07.252032Z","shell.execute_reply.started":"2022-09-24T23:57:05.752101Z","shell.execute_reply":"2022-09-24T23:57:07.250663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results_dict = {}\nmodel = xgb.train(\n    params = params,\n    dtrain = dtrain,\n    evals = [(dtrain, \"train\"), (dvalid, \"valid\")],\n    num_boost_round = 100,\n    evals_result = results_dict\n)","metadata":{"execution":{"iopub.status.busy":"2022-09-24T23:57:07.253958Z","iopub.execute_input":"2022-09-24T23:57:07.254343Z","iopub.status.idle":"2022-09-25T00:04:59.162916Z","shell.execute_reply.started":"2022-09-24T23:57:07.254308Z","shell.execute_reply":"2022-09-25T00:04:59.161829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(results_dict[\"train\"][\"rmse\"], color = \"red\", label = \"train\")\nplt.plot(results_dict[\"valid\"][\"rmse\"], color = \"blue\", label = \"valid\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-25T00:04:59.164393Z","iopub.execute_input":"2022-09-25T00:04:59.168135Z","iopub.status.idle":"2022-09-25T00:04:59.381668Z","shell.execute_reply.started":"2022-09-25T00:04:59.168084Z","shell.execute_reply":"2022-09-25T00:04:59.380713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"param = {'objective': 'multi:softmax', 'num_class': 15}\n\nnum_round = 100\nbst = xgb.train(param, dtrain, num_round)\n\ndtest = xgb.DMatrix(X_test)\npred = bst.predict(dtest)\n\nfrom sklearn.metrics import accuracy_score\n\nscore = accuracy_score(y_test, pred)\nprint('score:{0:.4f}'.format(score))","metadata":{"execution":{"iopub.status.busy":"2022-09-25T00:04:59.383269Z","iopub.execute_input":"2022-09-25T00:04:59.384199Z","iopub.status.idle":"2022-09-25T01:32:51.838195Z","shell.execute_reply.started":"2022-09-25T00:04:59.384152Z","shell.execute_reply":"2022-09-25T01:32:51.836856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xgb.plot_importance(bst)","metadata":{"execution":{"iopub.status.busy":"2022-09-25T01:32:51.840164Z","iopub.execute_input":"2022-09-25T01:32:51.840929Z","iopub.status.idle":"2022-09-25T01:32:54.968195Z","shell.execute_reply.started":"2022-09-25T01:32:51.840875Z","shell.execute_reply":"2022-09-25T01:32:54.967101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-09-25T01:33:25.095232Z","iopub.execute_input":"2022-09-25T01:33:25.095743Z","iopub.status.idle":"2022-09-25T01:33:25.103125Z","shell.execute_reply.started":"2022-09-25T01:33:25.095702Z","shell.execute_reply":"2022-09-25T01:33:25.101670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}