{"cells":[{"metadata":{"_uuid":"06ffe44120217acd66a63ebbfce32a227ec023d0"},"cell_type":"markdown","source":"This notebook is going to use Random Forest Algorithm"},{"metadata":{"trusted":true,"_uuid":"0c542d6e41a03883827261c7cd136df07e8ea011"},"cell_type":"code","source":"import pandas as pd\nimport numpy as np","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"217e00c8d1fe8533906d221ebce435511f55ad06"},"cell_type":"code","source":"trainRaw = pd.read_csv(\"../input/train_V2.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"70a7185faee253d26ac25457b6095a49983bb954"},"cell_type":"code","source":"testRaw = pd.read_csv(\"../input/test_V2.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"efa2b8d690ab35e006a668b7b46c435ffcacc004"},"cell_type":"code","source":"trainRaw[trainRaw['winPlacePerc'].isnull()]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6d1c9efae61ebce8454b14b819b2bf16dfc0ebea"},"cell_type":"markdown","source":"Drop the NaN observation"},{"metadata":{"trusted":true,"_uuid":"0be932bd7b61bd4d0b1cae9d8f9d113fe77d09da"},"cell_type":"code","source":"train = trainRaw.drop(2744604, axis=0)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2e6ba86c647b690f9434ad8d926f987aedff3a41"},"cell_type":"markdown","source":"![](http://)Headshot Rate might be an important feature. Idea brought from [this kernel](https://www.kaggle.com/rejasupotaro/cheaters-and-zombies)."},{"metadata":{"trusted":true,"_uuid":"44b0b2031f7af0a11bb3ea083b1a3183524d5c33"},"cell_type":"code","source":"train.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3367f60348b735ed558c698c6d68195776d30fde"},"cell_type":"code","source":"train['headshotRate'] = train['headshotKills'] / train['kills']\ntrain['headshotRate'] = train['headshotRate'].fillna(0)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1d5ad62b7f73a17333519c32231b601e49568f75"},"cell_type":"markdown","source":"If there are more than one people in the group, then all people's rank will depend on the last died one. We find maximum and minimum values for 'assists', 'walkDistance', 'kills', 'DBNOs' columns then put them back to our data."},{"metadata":{"trusted":true,"_uuid":"b83cbe5e1ddadd6fcfdffd18677ad825d5bd3aaa"},"cell_type":"code","source":"groupMax = train[['groupId', 'assists', 'walkDistance', 'kills', 'DBNOs']].groupby('groupId').max()\ngroupMin = train[['groupId', 'assists', 'walkDistance', 'kills', 'DBNOs']].groupby('groupId').min()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"38b03b115c825c72e44c9bb99905479098d7b8c4"},"cell_type":"code","source":"groupMax = groupMax.rename(columns = {'assists': 'assistsMax', 'walkDistance': 'walkDistanceMax', 'kills': 'killsMax', 'DBNOs': 'DBNOsMax'})\ngroupMin = groupMin.rename(columns = {'assists': 'assistsMin', 'walkDistance': 'walkDistanceMin', 'kills': 'killsMin', 'DBNOs': 'DBNOsMin'})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d564884483ec2724fc35a76d1774f2c3dfaf1579"},"cell_type":"code","source":"groupMax.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"27cf79cf84a39ac0c905e9410ad7996373a8ff57"},"cell_type":"code","source":"groupMin.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f349c167962c4905b01af37ab2d9df27e7da9e9e"},"cell_type":"code","source":"dataAll = pd.merge(train, groupMax, on = 'groupId')\ndataAll = pd.merge(dataAll, groupMin, on = 'groupId')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"462ea3992a6711c632fbb286751824a81f2a7e39"},"cell_type":"code","source":"dataAll.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"2d168a110f54088a25f2e78552e8ac778fdee712"},"cell_type":"code","source":"dataAll.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a27b9831cb42225ab32dcd0b78c88d095dd4fe1e"},"cell_type":"code","source":"dataAll.tail()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ecfc92b992ad0fb45fbae8aff9d827c11c43b5fb"},"cell_type":"markdown","source":"By the inspiration from killPlace, we think it might be helpful to get ranks in a match for more features."},{"metadata":{"trusted":true,"_uuid":"3001c70c4ca9f2c6d58eb7e6d48261c09436401b"},"cell_type":"code","source":"# matchRanks = train[['matchId', 'assists', 'boosts', 'damageDealt', 'heals', 'killStreaks', 'longestKill', 'walkDistance', \\\n#      'swimDistance', 'rideDistance']].groupby('matchId').rank(ascending = False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"481655bbd5bcfad4a09f5eb9eacbb4700e5729b8"},"cell_type":"code","source":"# matchRanks.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6b8bd90511ce2c2e097dc66f5e00e41e335e2162"},"cell_type":"code","source":"# matchRanks = matchRanks.rename(columns = {'assists': 'assistsPlace', 'boosts': 'boostsPlace', \n#                                          'damageDealt': 'damageDealtPlace', \n#                                          'heals': 'heals', 'killStreaks': 'killStreaksPlace', \n#                                          'longestKill': 'longestKillPlace', 'walkDistance': 'walkDistancePlace', \n#                                          'swimDistance': 'swimDistancePlace', 'rideDistance': 'rideDistancePlace'})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dd634deb69a47e72ebc45b468fe554a1dbb530f3"},"cell_type":"code","source":"# matchRanks['matchId'] = train['matchId']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"e7cc4117194148679b4c82b0fb0bb5f19b1fb162"},"cell_type":"code","source":"# matchRanks.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f10e2cb1b8339fb11b58383af9504021cf5e4c0f"},"cell_type":"code","source":"# matchRanks.tail()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"efd938604c06e7ade6c70669e651bab1f4d22fa9"},"cell_type":"code","source":"# matchRanks.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4d8c311c867d297a1500460cd832024957b542ee"},"cell_type":"code","source":"# dataAll = pd.concat([dataAll.reset_index(drop=True),matchRanks.reset_index(drop=True)], axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fb960af8570bf4e2f7bff45d13ff995e86291a62"},"cell_type":"code","source":"dataAll.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"10f0a531b76e574dcbc05d9dfc0d09f461e748aa"},"cell_type":"code","source":"dataAll.tail()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c54a6e78bbe1c20b7182e67b5922de05503b117a"},"cell_type":"code","source":"dataAll.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3f70f09cdeb54571f8b3cf30b6ae27d1fbde2c87"},"cell_type":"code","source":"features = pd.get_dummies(dataAll.drop(['Id', 'groupId', 'matchId', 'winPlacePerc'], axis = 1))\nlabels = np.array(dataAll['winPlacePerc'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f08b8404eedb7631855ba55b7f755ae5711852f3"},"cell_type":"code","source":"labels[np.isnan(labels)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5d4f6c5eefa6ba1d3bdfae3a62eb2b1727542536"},"cell_type":"code","source":"features.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"35df2fc306f3e2686ce786b97c28adce9576cd8e"},"cell_type":"code","source":"print('features', features.shape, 'labels', labels.shape)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b1f9b9189798efdc1d1fff8ce18f352c3a513178"},"cell_type":"markdown","source":"Train Model\n\nTODO: Use cross-validation to play around and tweak parameters"},{"metadata":{"trusted":true,"_uuid":"a4cf7a4b07a6c8d4859ae242bafad5489b929f87"},"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8b713ec26b29463000b6348e52f87c27fb5c7b3c"},"cell_type":"code","source":"RFmodel = RandomForestRegressor(n_estimators=80, random_state=1937, n_jobs=3, min_samples_leaf=3, max_features='sqrt')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"283cbe5416f5b1881a3acfbf3e50701ba4ae2d5e"},"cell_type":"code","source":"print('start training')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"efbb2c3d79fcdb0a39e179293d5f8a3d9e443498"},"cell_type":"code","source":"RFmodel.fit(features, labels)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7be7b43ad373c7603fe763477f7d08f9fe682299"},"cell_type":"markdown","source":"Prepare test data"},{"metadata":{"trusted":true,"_uuid":"16b7932a65d6666a4609a18830d7f4d2c82add47"},"cell_type":"code","source":"test = testRaw.copy()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"78c86d9526caaa148eab7ea6f90342f9d3e5cefa"},"cell_type":"code","source":"test['headshotRate'] = test['headshotKills'] / test['kills']\ntest['headshotRate'] = test['headshotRate'].fillna(0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1bc3500a21b5980cd03da4f975bcd3c07f43d6b9"},"cell_type":"code","source":"groupMaxTest = test[['groupId', 'assists', 'walkDistance', 'kills', 'DBNOs']].groupby('groupId').max()\ngroupMinTest = test[['groupId', 'assists', 'walkDistance', 'kills', 'DBNOs']].groupby('groupId').min()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0ef912075a50eea197b6cdc676a2aeb7376b3ebe"},"cell_type":"code","source":"groupMaxTest = groupMaxTest.rename(columns = {'assists': 'assistsMax', 'walkDistance': 'walkDistanceMax', 'kills': 'killsMax', 'DBNOs': 'DBNOsMax'})\ngroupMinTest = groupMinTest.rename(columns = {'assists': 'assistsMin', 'walkDistance': 'walkDistanceMin', 'kills': 'killsMin', 'DBNOs': 'DBNOsMin'})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1423c9fdd4fe309b63bf3c599c61949bf8d4b391"},"cell_type":"code","source":"test = pd.merge(test, groupMaxTest, on = 'groupId')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6f1bef058fd5cd4291f78e93d2bc8038d6d58195"},"cell_type":"code","source":"test = pd.merge(test, groupMinTest, on = 'groupId')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"558ea098be2de7828edef99083a93c182a675851"},"cell_type":"code","source":"# matchRanksTest = test[['matchId', 'assists', 'boosts', 'damageDealt', 'heals', 'killStreaks', 'longestKill', 'walkDistance', \\\n#      'swimDistance', 'rideDistance']].groupby('matchId').rank()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ec17f057612868bc0423c88be8cb0b233a361b80"},"cell_type":"code","source":"# matchRanksTest = matchRanksTest.rename(columns = {'assists': 'assistsPlace', 'boosts': 'boostsPlace', \n#                                           'damageDealt': 'damageDealtPlace', \n#                                           'heals': 'heals', 'killStreaks': 'killStreaksPlace', \n#                                           'longestKill': 'longestKillPlace', 'walkDistance': 'walkDistancePlace', \n#                                           'swimDistance': 'swimDistancePlace', 'rideDistance': 'rideDistancePlace'})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"30688c87fd5802e52a9769b46746f74e25b56cd1"},"cell_type":"code","source":"# test = pd.concat([test.reset_index(drop=True),matchRanksTest.reset_index(drop=True)], axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0ae9cada987803b2f44f5e4d5fee46cf9ad4e5da"},"cell_type":"code","source":"test.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e79df7e09a6d239a2b4556f3abdbb0624b477c23"},"cell_type":"code","source":"features.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3834732aab5e30c4ed2cb90a8b513e4265caee5f"},"cell_type":"code","source":"test = test.drop(['Id', 'groupId', 'matchId'],axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fe3b96752a7d498c8e981088cbf60b6c1bd930de"},"cell_type":"code","source":"test = pd.get_dummies(test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"531dcc2b41ec753ab60cd3c6207a1d9b109c4fa4"},"cell_type":"code","source":"test.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"23fc254766576368dfa097610341f648b330b3ce"},"cell_type":"code","source":"features.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"a9acb6c2f278befabf6d5304d7ba4e3d11278c1c"},"cell_type":"code","source":"test.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8b82e3b09ffb19761c65fb56ac8020b9d2bbacfa"},"cell_type":"markdown","source":"Submission !!!"},{"metadata":{"trusted":true,"_uuid":"bc6824749321d11380d2ff0f56a3317475e86554"},"cell_type":"code","source":"prediction = RFmodel.predict(test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ea67c192c1bf7b01908c0339c04cbef7d3d774a3"},"cell_type":"code","source":"submission = pd.DataFrame({\"Id\":testRaw['Id'], \"winPlacePerc\":prediction})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ecb574662f4495902b5b265f435db7fb73c38c41"},"cell_type":"code","source":"submission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"93e01473aa7057ec772af2cef8c0ecd5d3a816c7"},"cell_type":"code","source":"submission.tail()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bcb71a8505e49f9383f41cf0d9635fa7c557de20"},"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}