{"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":"! pip install tensorflow_decision_forests","metadata":{"id":"ZsRrfipJecny","outputId":"ce54126e-a1a0-43f2-8061-6612360cfbd9","execution":{"iopub.status.busy":"2022-10-27T22:36:07.357486Z","iopub.execute_input":"2022-10-27T22:36:07.358362Z","iopub.status.idle":"2022-10-27T22:36:39.919052Z","shell.execute_reply.started":"2022-10-27T22:36:07.358239Z","shell.execute_reply":"2022-10-27T22:36:39.918065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split\nimport tensorflow_decision_forests as tfdf\nimport tensorflow as tf","metadata":{"id":"nKUc9shVeVmM","execution":{"iopub.status.busy":"2022-10-27T22:36:39.921174Z","iopub.execute_input":"2022-10-27T22:36:39.921603Z","iopub.status.idle":"2022-10-27T22:36:45.940620Z","shell.execute_reply.started":"2022-10-27T22:36:39.921563Z","shell.execute_reply":"2022-10-27T22:36:45.939574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Modify the dtypes to make the dataset compatible with TFDF (float16 -> float32) , took from tf-decision-trees.ipynb notebook in kaggle\ndtypes = {'game_num': 'int32',\n 'event_id': 'int32',\n 'event_time': 'float32',\n 'ball_pos_x': 'float32',\n 'ball_pos_y': 'float32',\n 'ball_pos_z': 'float32',\n 'ball_vel_x': 'float32',\n 'ball_vel_y': 'float32',\n 'ball_vel_z': 'float32',\n 'p0_pos_x': 'float32',\n 'p0_pos_y': 'float32',\n 'p0_pos_z': 'float32',\n 'p0_vel_x': 'float32',\n 'p0_vel_y': 'float32',\n 'p0_vel_z': 'float32',\n 'p0_boost': 'float32',\n 'p1_pos_x': 'float32',\n 'p1_pos_y': 'float32',\n 'p1_pos_z': 'float32',\n 'p1_vel_x': 'float32',\n 'p1_vel_y': 'float32',\n 'p1_vel_z': 'float32',\n 'p1_boost': 'float32',\n 'p2_pos_x': 'float32',\n 'p2_pos_y': 'float32',\n 'p2_pos_z': 'float32',\n 'p2_vel_x': 'float32',\n 'p2_vel_y': 'float32',\n 'p2_vel_z': 'float32',\n 'p2_boost': 'float32',\n 'p3_pos_x': 'float32',\n 'p3_pos_y': 'float32',\n 'p3_pos_z': 'float32',\n 'p3_vel_x': 'float32',\n 'p3_vel_y': 'float32',\n 'p3_vel_z': 'float32',\n 'p3_boost': 'float32',\n 'p4_pos_x': 'float32',\n 'p4_pos_y': 'float32',\n 'p4_pos_z': 'float32',\n 'p4_vel_x': 'float32',\n 'p4_vel_y': 'float32',\n 'p4_vel_z': 'float32',\n 'p4_boost': 'float32',\n 'p5_pos_x': 'float32',\n 'p5_pos_y': 'float32',\n 'p5_pos_z': 'float32',\n 'p5_vel_x': 'float32',\n 'p5_vel_y': 'float32',\n 'p5_vel_z': 'float32',\n 'p5_boost': 'float32',\n 'boost0_timer': 'float32',\n 'boost1_timer': 'float32',\n 'boost2_timer': 'float32',\n 'boost3_timer': 'float32',\n 'boost4_timer': 'float32',\n 'boost5_timer': 'float32',\n 'player_scoring_next': 'str',\n 'team_scoring_next': 'object',\n 'team_A_scoring_within_10sec': 'int32',\n 'team_B_scoring_within_10sec': 'int32'}\ndf0 = pd.read_csv('/kaggle/input/tabular-playground-series-oct-2022/train_0.csv', dtype=dtypes)\ndf1 = pd.read_csv('/kaggle/input/tabular-playground-series-oct-2022/train_1.csv', dtype=dtypes)\n#df2 = pd.read_csv('/kaggle/input/tabular-playground-series-oct-2022/train_2.csv', dtype=dtypes)\n#df3 = pd.read_csv('/kaggle/input/tabular-playground-series-oct-2022/train_3.csv', dtype=dtypes)\n#df4 = pd.read_csv('/kaggle/input/tabular-playground-series-oct-2022/train_4.csv', dtype=dtypes)\n#df5 = pd.read_csv('/kaggle/input/tabular-playground-series-oct-2022/train_5.csv', dtype=dtypes)\n\ndf = pd.concat([df0,df1], axis=0)\n\n#df = pd.concat([df0,df1,df2,df3,df4,df5], axis=0)\n#NOTE: Submission was made with train data from 0 to 5\n\n","metadata":{"id":"sT77iHeme3aa","execution":{"iopub.status.busy":"2022-10-27T22:36:49.494302Z","iopub.execute_input":"2022-10-27T22:36:49.494954Z","iopub.status.idle":"2022-10-27T22:37:49.964143Z","shell.execute_reply.started":"2022-10-27T22:36:49.494918Z","shell.execute_reply":"2022-10-27T22:37:49.962961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del df0,df1 #,df2,df3,df4,df5 #,df6,df7,df8,df9\n","metadata":{"id":"eUy4MSQDe8Yz","execution":{"iopub.status.busy":"2022-10-27T22:38:06.942109Z","iopub.execute_input":"2022-10-27T22:38:06.943330Z","iopub.status.idle":"2022-10-27T22:38:06.979290Z","shell.execute_reply.started":"2022-10-27T22:38:06.943277Z","shell.execute_reply":"2022-10-27T22:38:06.978036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#To compute distance amog ball or person with the goal keeping area\ndef getDistancePorteria(x):\n    return np.sqrt((x[0])**2+(x[1]-100)**2+(x[2])**2)\n\n#To compute velocity magnitude\ndef getVelocityMagnitude(x):\n    return np.linalg.norm(np.array(x))","metadata":{"id":"kOnlhnEwdZqm","execution":{"iopub.status.busy":"2022-10-27T22:38:29.300432Z","iopub.execute_input":"2022-10-27T22:38:29.300826Z","iopub.status.idle":"2022-10-27T22:38:29.306851Z","shell.execute_reply.started":"2022-10-27T22:38:29.300797Z","shell.execute_reply":"2022-10-27T22:38:29.306055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['dp'] = df[['ball_pos_x', 'ball_pos_y','ball_pos_z']].apply(getDistancePorteria, axis=1)\ndf['vb'] = df[['ball_vel_x','ball_vel_y','ball_vel_z']].apply(getVelocityMagnitude, axis=1)","metadata":{"id":"S5EDaL5OfqCT","execution":{"iopub.status.busy":"2022-10-27T22:38:31.264843Z","iopub.execute_input":"2022-10-27T22:38:31.265256Z","iopub.status.idle":"2022-10-27T22:41:37.692877Z","shell.execute_reply.started":"2022-10-27T22:38:31.265223Z","shell.execute_reply":"2022-10-27T22:41:37.691771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for j in range(6):\n    df['dp'+str(j)] = df[['p'+str(j)+'_pos_x','p'+str(j)+'_pos_y','p'+str(j)+'_pos_z']].apply(getDistancePorteria, axis=1)\n    df['vp'+str(j)] = df[['p'+str(j)+'_vel_x', 'p'+str(j)+'_vel_y','p'+str(j)+'_vel_z']].apply(getVelocityMagnitude,axis=1)","metadata":{"id":"Dp2DkxFKfyll","execution":{"iopub.status.busy":"2022-10-27T22:43:21.379009Z","iopub.execute_input":"2022-10-27T22:43:21.379389Z","iopub.status.idle":"2022-10-27T23:02:02.167748Z","shell.execute_reply.started":"2022-10-27T22:43:21.379359Z","shell.execute_reply":"2022-10-27T23:02:02.165794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"drop_cols = ['game_num',\n'p0_pos_x','p0_pos_y','p0_pos_z','p1_pos_x','p1_pos_y','p1_pos_z','p2_pos_x','p2_pos_y','p2_pos_z',\n    'p3_pos_x','p3_pos_y','p3_pos_z','p4_pos_x','p4_pos_y','p4_pos_z','p5_pos_x','p5_pos_y','p5_pos_z',\n    'p0_vel_x','p0_vel_y','p0_vel_z','p1_vel_x','p1_vel_y','p1_vel_z','p2_vel_x','p2_vel_y','p2_vel_z',\n    'p3_vel_x','p3_vel_y','p3_vel_z','p4_vel_x','p4_vel_y','p4_vel_z','p5_vel_x','p5_vel_y','p5_vel_z',\n    'ball_pos_x','ball_pos_y','ball_pos_z','ball_vel_x','ball_vel_y','ball_vel_z'\n]","metadata":{"id":"LC1EKhN_f-ZQ","execution":{"iopub.status.busy":"2022-10-27T23:02:04.744272Z","iopub.execute_input":"2022-10-27T23:02:04.745214Z","iopub.status.idle":"2022-10-27T23:02:04.753694Z","shell.execute_reply.started":"2022-10-27T23:02:04.745158Z","shell.execute_reply":"2022-10-27T23:02:04.752638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.drop(columns=drop_cols,inplace=True)","metadata":{"id":"_LfzyPTfhsOa","execution":{"iopub.status.busy":"2022-10-27T23:02:06.179654Z","iopub.execute_input":"2022-10-27T23:02:06.180507Z","iopub.status.idle":"2022-10-27T23:02:07.758358Z","shell.execute_reply.started":"2022-10-27T23:02:06.180456Z","shell.execute_reply":"2022-10-27T23:02:07.757157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#took from tf-decision-trees.ipynb notebook in kaggle\ndef get_scorer(x):\n    t = {\n        (0,0): \"Neither\",\n        (1,0): \"A\",\n        (0,1): \"B\"\n    }\n    return t[tuple(x.to_list())]\n\n\ndf['target'] = df[['team_A_scoring_within_10sec', 'team_B_scoring_within_10sec']].apply(get_scorer, axis=1)","metadata":{"id":"MznKToa3fO6g","execution":{"iopub.status.busy":"2022-10-27T23:02:10.057309Z","iopub.execute_input":"2022-10-27T23:02:10.057793Z","iopub.status.idle":"2022-10-27T23:02:45.144437Z","shell.execute_reply.started":"2022-10-27T23:02:10.057755Z","shell.execute_reply":"2022-10-27T23:02:45.142684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check balance of data\ndf.target.value_counts()","metadata":{"id":"iFubXLjKfXsU","outputId":"3ca2d7b3-3f38-4223-de74-374338a7fe84","execution":{"iopub.status.busy":"2022-10-27T23:02:48.374998Z","iopub.execute_input":"2022-10-27T23:02:48.375567Z","iopub.status.idle":"2022-10-27T23:02:48.567917Z","shell.execute_reply.started":"2022-10-27T23:02:48.375521Z","shell.execute_reply":"2022-10-27T23:02:48.566501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#df.to_csv('/kaggle/input/tabular-playground-series-oct-2022/d0-d1.csv')","metadata":{"id":"K6d4u093kvTo"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"id":"lcv9FcIusTge","outputId":"c69a5e8c-d9a5-4771-9c3c-98ed5199f029","execution":{"iopub.status.busy":"2022-10-27T23:02:55.791887Z","iopub.execute_input":"2022-10-27T23:02:55.792446Z","iopub.status.idle":"2022-10-27T23:02:55.828200Z","shell.execute_reply.started":"2022-10-27T23:02:55.792401Z","shell.execute_reply":"2022-10-27T23:02:55.826744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train, val = train_test_split(df, test_size=0.3)\n","metadata":{"id":"Xwe2E0sTovZm","outputId":"73d37cb7-32e0-41fe-b72b-9a095dbb8e67","execution":{"iopub.status.busy":"2022-10-27T23:03:10.062561Z","iopub.execute_input":"2022-10-27T23:03:10.063628Z","iopub.status.idle":"2022-10-27T23:03:14.371563Z","shell.execute_reply.started":"2022-10-27T23:03:10.063574Z","shell.execute_reply":"2022-10-27T23:03:14.370569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.target.value_counts()","metadata":{"id":"4GsAc0JrcOJV","outputId":"64d0e812-13f1-4bba-e1e4-7d98729074db","execution":{"iopub.status.busy":"2022-10-27T23:03:16.340065Z","iopub.execute_input":"2022-10-27T23:03:16.340774Z","iopub.status.idle":"2022-10-27T23:03:16.471256Z","shell.execute_reply.started":"2022-10-27T23:03:16.340731Z","shell.execute_reply":"2022-10-27T23:03:16.470364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"drop_cols = [\n    'team_scoring_next', 'team_A_scoring_within_10sec', \n    'team_B_scoring_within_10sec', 'event_id', \n    'event_time', 'player_scoring_next'\n]","metadata":{"id":"9PMZDKEnphh5","execution":{"iopub.status.busy":"2022-10-27T23:03:18.040763Z","iopub.execute_input":"2022-10-27T23:03:18.042114Z","iopub.status.idle":"2022-10-27T23:03:18.047250Z","shell.execute_reply.started":"2022-10-27T23:03:18.042051Z","shell.execute_reply":"2022-10-27T23:03:18.045948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.drop(drop_cols, axis=1).head()","metadata":{"id":"RGyMpSsFfy_J","outputId":"8fb3db9e-a82f-403c-c2c0-a523c04ad1f4","execution":{"iopub.status.busy":"2022-10-27T23:03:19.494097Z","iopub.execute_input":"2022-10-27T23:03:19.494589Z","iopub.status.idle":"2022-10-27T23:03:19.785253Z","shell.execute_reply.started":"2022-10-27T23:03:19.494549Z","shell.execute_reply":"2022-10-27T23:03:19.783911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds_train = tfdf.keras.pd_dataframe_to_tf_dataset(train.drop(drop_cols, axis=1), label='target').prefetch(1)\nds_val = tfdf.keras.pd_dataframe_to_tf_dataset(val.drop(drop_cols, axis=1), label='target').prefetch(1)","metadata":{"id":"gFZbFOFIoz1Y","outputId":"7eca0b1a-ee7c-461a-f3ad-e9028ce77df7","execution":{"iopub.status.busy":"2022-10-27T23:03:23.044819Z","iopub.execute_input":"2022-10-27T23:03:23.046449Z","iopub.status.idle":"2022-10-27T23:03:26.890825Z","shell.execute_reply.started":"2022-10-27T23:03:23.046378Z","shell.execute_reply":"2022-10-27T23:03:26.889493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.drop(drop_cols, axis=1).keys()","metadata":{"id":"136ODrfqsafo","outputId":"7f369202-b55b-4827-eaed-a788263c8d0b","execution":{"iopub.status.busy":"2022-10-27T23:03:32.224857Z","iopub.execute_input":"2022-10-27T23:03:32.225354Z","iopub.status.idle":"2022-10-27T23:03:32.505408Z","shell.execute_reply.started":"2022-10-27T23:03:32.225315Z","shell.execute_reply":"2022-10-27T23:03:32.504151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\ncpus = os.cpu_count()\nprint(cpus)\nmodel = tfdf.keras.RandomForestModel(num_threads=cpus)","metadata":{"id":"nPVBgpYJo29q","outputId":"f0e2e647-6c2c-4fc5-8f18-dd2a7d6525f6","execution":{"iopub.status.busy":"2022-10-27T23:03:34.670566Z","iopub.execute_input":"2022-10-27T23:03:34.671463Z","iopub.status.idle":"2022-10-27T23:03:34.730060Z","shell.execute_reply.started":"2022-10-27T23:03:34.671396Z","shell.execute_reply":"2022-10-27T23:03:34.728657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(x=ds_train, validation_data=ds_val)","metadata":{"id":"FUPc37Rno6aO","outputId":"3a68af22-1b11-441b-be5c-d76527460e63","execution":{"iopub.status.busy":"2022-10-27T23:03:41.630815Z","iopub.execute_input":"2022-10-27T23:03:41.632191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model.save(\"/kaggle/input/tabular-playground-series-oct-2022/RF2\")","metadata":{"id":"XxN1FbUao84E","outputId":"50dcbc67-8dc4-4e94-a49b-9f5b763893e1"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_preds = model.predict(ds_val)","metadata":{"id":"CUr7NlySpCR0","outputId":"8adf684f-c3c8-4fce-d28a-e7d28c8a701d"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_preds","metadata":{"id":"dVqhRxeaYOjx","outputId":"1d2aac8f-5bbf-47ac-c97f-64e193fd05d5"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val['A_pred'] = val_preds[:,0]\nval['B_pred'] = val_preds[:,1]","metadata":{"id":"sODlqEwTpD-_"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv(\"/content/drive/MyDrive/Colab Notebooks/Kaggle/Playground/CarsVideoGame/test.csv\", dtype=dtypes)","metadata":{"id":"QPeTB3zfOPw-"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['dp'] = test_df[['ball_pos_x', 'ball_pos_y','ball_pos_z']].apply(getDistancePorteria, axis=1)\ntest_df['vb'] = test_df[['ball_vel_x','ball_vel_y','ball_vel_z']].apply(getVelocityMagnitude, axis=1)\nfor j in range(6):\n    test_df['dp'+str(j)] = test_df[['p'+str(j)+'_pos_x','p'+str(j)+'_pos_y','p'+str(j)+'_pos_z']].apply(getDistancePorteria, axis=1)\n    test_df['vp'+str(j)] = test_df[['p'+str(j)+'_vel_x', 'p'+str(j)+'_vel_y','p'+str(j)+'_vel_z']].apply(getVelocityMagnitude,axis=1)","metadata":{"id":"Q4XzdxPZSSbk"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"drop_cols_test = ['p0_pos_x','p0_pos_y','p0_pos_z','p1_pos_x','p1_pos_y','p1_pos_z','p2_pos_x','p2_pos_y','p2_pos_z',\n    'p3_pos_x','p3_pos_y','p3_pos_z','p4_pos_x','p4_pos_y','p4_pos_z','p5_pos_x','p5_pos_y','p5_pos_z',\n    'p0_vel_x','p0_vel_y','p0_vel_z','p1_vel_x','p1_vel_y','p1_vel_z','p2_vel_x','p2_vel_y','p2_vel_z',\n    'p3_vel_x','p3_vel_y','p3_vel_z','p4_vel_x','p4_vel_y','p4_vel_z','p5_vel_x','p5_vel_y','p5_vel_z',\n    'ball_pos_x','ball_pos_y','ball_pos_z','ball_vel_x','ball_vel_y','ball_vel_z'\n]\ntest_df.drop(columns=drop_cols_test, inplace=True)\ntest_df.keys()","metadata":{"id":"zPA8yjt-S2hE","outputId":"9fe790cd-9421-41c4-a9b6-6ad054493ebf"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds_test = tfdf.keras.pd_dataframe_to_tf_dataset(test_df.drop(columns=['id']))","metadata":{"id":"54CTPfJdSXnn"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(ds_test)","metadata":{"id":"EDkCUMIkUFw_","outputId":"3e227de1-c0e4-411a-cc23-c1c8fdd932f2"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def softmax(x):\n    \"\"\"Compute softmax values for each sets of scores in x.\"\"\"\n    return np.exp(x) / np.sum(np.exp(x), axis=1,keepdims=True)","metadata":{"id":"szSfTtJpXOqt"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = test_df[['id']]","metadata":{"id":"A6uSJm5XUF4p"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df['team_A_scoring_within_10sec'] = softmax(pred)[:,0]\nsubmission_df['team_B_scoring_within_10sec'] = softmax(pred)[:,1]","metadata":{"id":"WvpZw_aEbgdi","outputId":"84488695-ec54-45a6-8df3-737471e2ac71"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv(\"/kaggle/input/tabular-playground-series-oct-2022/submissionJb.csv\", index=False)","metadata":{"id":"TwN1M0jWUOEE"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tfdf.model_plotter.plot_model_in_colab(model)","metadata":{"id":"e3h14xXuZg9-","outputId":"72fbfe5b-7f20-4866-df94-0e88e0d723b5"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"rbW-p-XUcVIu"},"execution_count":null,"outputs":[]}]}