{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 in \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 \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"train_data=pd.read_csv('../input/train_V2.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"052450ee65cd914fd3f87cb944c1449b9a84df83"},"cell_type":"code","source":"train_data.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"226d55fc3751ecf3950de5fccb71c5bd29551076"},"cell_type":"code","source":"train_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"499ffd39b51794c8234162e6c6923c8ade944e85"},"cell_type":"code","source":"test_data=pd.read_csv('../input/test_V2.csv')","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":false,"trusted":true,"_uuid":"9109691cbefb00c2c660617b8ba66070e7066aa7"},"cell_type":"code","source":"test_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f682e6430ab4e8bf9f2b947767ee7e26fe6fe9e0"},"cell_type":"code","source":"print(\"Shape of Train data : \"+str(train_data.shape))\nprint(\"Shape of Test data : \"+str(test_data.shape))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4a2430bcee2f7cd44deefde38b2b4e659d4013c3"},"cell_type":"code","source":"train_data.columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d3899ebd81d2a9b44ce6304726f9d42d7678eaa1"},"cell_type":"code","source":"#CHECKING MISSING VALUES\ntrain_data.isnull().sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"52a79277bc19b6c936b8f6fa879adf6a1f8556bd"},"cell_type":"code","source":"#Find missing value in winPlacePerc\ntrain_data[train_data.winPlacePerc.isnull()]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"55c0259cd2b06dd16d464294fafbd73325956393"},"cell_type":"code","source":"#Eliminating 2744604 from train_data\ntrain_data.drop(2744604,inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ef47dd71676a9933f0f9a63e895c254436cc3dac"},"cell_type":"code","source":"#CHECKING MISSING VALUES AGAIN\ntrain_data.isnull().sum()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e5f4ae0c843399f0b5fa95099031cbfee43e7dbb"},"cell_type":"code","source":"#MATCHES PLAYED IN TRAIN_DATA AND TEST_DATA.\n#cHECKING UNIQUE MATCH-ID\nlen(train_data.matchId.unique())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4c82e0819b93050f290b4a8f14a4d75af4c23380"},"cell_type":"code","source":"len(test_data.matchId.unique())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4de2aeb6e10836e0427742905c36e3f3c71a355d"},"cell_type":"code","source":"#Find players in matches\ntrain_data.groupby('matchId')['matchId'].count()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d7e1af0794236c288804a6b063a5c02a14b0b8d7"},"cell_type":"code","source":"#NEW FEATURE PLAYERS PLAYED\ntrain_data['playersPlayed']=train_data.groupby('matchId')['matchId'].transform('count')\nprint(train_data['playersPlayed'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6510b3648c07ff4199d58421424d05b39bafbfe6"},"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nplt.subplots(figsize=(15,8))\nsns.countplot(train_data[train_data['playersPlayed']>=60]['playersPlayed'])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"612c01d1db7df22b19b613936699747b64603dea"},"cell_type":"code","source":"#FINDING THE CHEATER | OUTLIERS","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f45a3e1b5714ae74ed21b86ad8be155a4d31ba11"},"cell_type":"code","source":"#TOTAL DISTANCE \n#TRAIN_DATA\ntrain_data['totalDistance']=train_data.rideDistance+train_data.walkDistance+train_data.swimDistance","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7366877d765ef728197736b27b7635f614c7e624"},"cell_type":"code","source":"train_data['totalDistance'].head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"859959c06b73776a989221faa452330aa1450573"},"cell_type":"code","source":"#TOTAL DISTANCE \n#TEST_DATA\ntest_data['totalDistance']=test_data.rideDistance+test_data.walkDistance+test_data.swimDistance","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"03711c6383d58aed5bcb18a706ed230a2809d48f"},"cell_type":"code","source":"train_data['totalDistance'].head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c8e52905f1798514e61286a6bfcab352511574c9"},"cell_type":"code","source":"#Kills without moving\n#train\ntrain_data['killsWithoutMove']=((train_data['kills']>0)&(train_data['totalDistance']==0))\n#test\ntest_data['killsWithoutMove']=((test_data['kills']>0)&(test_data['totalDistance']==0))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"db25854240155d2f96080b691904a92d856b916c"},"cell_type":"code","source":"train_data['killsWithoutMove'].value_counts()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"40e736e19b06168d50b546eaa3bc7008d4a1493c"},"cell_type":"code","source":"test_data['killsWithoutMove'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"752191f8f81a7c7987bbc6ffaebdebdcc509e833"},"cell_type":"code","source":"train_data.drop(train_data[train_data['killsWithoutMove']==True].index,inplace=True)\n#test_data.drop(test_data[test_data['killsWithoutMove']==True].index,inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b3f5bf90af3a4a18caf81f1e082e5c9686b1c45b"},"cell_type":"code","source":"test_data['killsWithoutMove'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f155cd879189787f87437693302ad7998eba5061"},"cell_type":"code","source":"train_data['killsWithoutMove'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2c83be8d8288c97e83e83b02e0dba78c9ea9405e"},"cell_type":"code","source":"train_data['roadKills'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7ab253355daff0f739c053352ff0a4af3faf0d11"},"cell_type":"code","source":"test_data['roadKills'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8a07dedd861be7d5fe3947aad32b4c7d1c9da0b7"},"cell_type":"code","source":"((train_data['roadKills']>=10)&(train_data['heals']==0)&(train_data['boosts']==0)).value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e829ce967ffd386f3575033257c14802bf0fcd86"},"cell_type":"code","source":"((test_data['roadKills']>=10)&(test_data['heals']==0)&(test_data['boosts']==0)).value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"83c091fb24d3a933e7a7d59d1f7784c95c36cd51"},"cell_type":"code","source":"train_data.drop(train_data[((train_data['roadKills']>=10)&(train_data['heals']==0)&(train_data['boosts']==0))==True].index,inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e5155f451d4f504aa8d4d1d55bdd0853815ab80b"},"cell_type":"code","source":"test_data.drop(test_data[((test_data['roadKills']>=10)&(test_data['heals']==0)&(test_data['boosts']==0))==True].index,inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f225b876633ef64cdb0ee89b65f906744e5aa484"},"cell_type":"code","source":"((test_data['roadKills']>=10)&(test_data['heals']==0)&(test_data['boosts']==0)).value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e18a1dea96e6d2b4ffd529b10694a6f15a73b547"},"cell_type":"code","source":"# kills count\n\ntrain_data['kills'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"380187c7e28aded60d3d34030139f204370be4c7"},"cell_type":"code","source":"#test\ntest_data['kills'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1549dc74184de62d1e68201eecab381d376db1f8"},"cell_type":"code","source":"#plot the kills graph\nplt.subplots(figsize=(15,8))\nsns.countplot(data=train_data,x=train_data['kills'])\nplt.title('Kills')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0cb355e0b4df7429541cb82e500b8ffcc8a08b72"},"cell_type":"code","source":"((train_data['kills']>=25)&(train_data['heals']==0)&(train_data['boosts']==0)).value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"06a7199637a1c1c7ca434c9c0a50982eec217436"},"cell_type":"code","source":"(train_data['kills']>=35).value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ba090f3f3d05110bc72d3c688416d0eee7f4ad9c"},"cell_type":"code","source":"# drop the outliers.\n\n#train\ntrain_data.drop(train_data[train_data['kills']>=35].index,inplace=True)\n\n#test\n\n#test_data.drop(test_data[test_data['kills']>=35].index,inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2b878fbed4b93e2b0b9c227bd093773cc88658cf"},"cell_type":"code","source":"(train_data['kills']>=35).value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ac9ea4658dda6414891a341aa0efb0a44a443662"},"cell_type":"code","source":"(test_data['kills']>=35).value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"010271d2bffc2eb53e2ca159ec92365cb142b30b"},"cell_type":"code","source":"((train_data['kills']>=25)&(train_data['heals']==0)&(train_data['boosts']==0)).value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ec51e8baa341a9b1d233a4e77e5e75caa69637da"},"cell_type":"code","source":"# drop the outliers.\n\n#train\ntrain_data.drop(train_data[((train_data['kills']>=25)&(train_data['heals']==0)&(train_data['boosts']==0))==True].index,inplace=True)\n\n#test\n\n#test_data.drop(test_data[((test_data['kills']>=25)&(test_data['heals']==0)&(test_data['boosts']==0))==True].index,inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"03daa30958ecd235d92f2c0c3a550cc38ea52fa0"},"cell_type":"code","source":"((train_data['kills']>=25)&(train_data['heals']==0)&(train_data['boosts']==0)).value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1fa7c605ae4cf2e71d297dffa1ac56f963c2af34"},"cell_type":"code","source":"#Longest kill\nplt.subplots(figsize=(15,8))\nsns.distplot(train_data['longestKill'],bins=20)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"06c55fe5972c5d2de7d02e57b1493e3f6d3bbd95"},"cell_type":"code","source":"(train_data['longestKill']>=1000).value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cb41f9543453219ddefcb9c50d5bc0964b5fad39"},"cell_type":"code","source":"#longest kills outliers \n#train\ntrain_data.drop(train_data[train_data['longestKill']>=1000].index,inplace=True)\n\n#test\n#test_data.drop(test_data[test_data['longestKill']>=1000].index,inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"542f8a894c48b87ab096046ce399132e1f02c157"},"cell_type":"code","source":"#CHECKING OUTLIERS\n#TRAIN\n(train_data['longestKill']>=1000).value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"94a6d3981be6527841633da873db0b7354c67352"},"cell_type":"code","source":"#CHECKING OUTLIERS\n#TEST\n(test_data['longestKill']>=1000).value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"882489698098978bcac349b12c259ef962b52436"},"cell_type":"code","source":"train_data[['rideDistance','walkDistance','swimDistance','totalDistance']].head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"726c5e4940758cc012ecc7984ed71c4d719b452e"},"cell_type":"code","source":"test_data[['rideDistance','walkDistance','swimDistance','totalDistance']].head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1c5311118324fbb13aa6bb0cd03cd6aae65b788d"},"cell_type":"code","source":"# plot walkDistance \n\nplt.figure(figsize=(15,8))\nsns.distplot(train_data['walkDistance'], bins=10)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f26f81209037d643afef204b69a7e7ada764ca2b"},"cell_type":"code","source":"#Remove the outliers\n\n#train\ntrain_data.drop(train_data[train_data['walkDistance']>=10000].index,inplace=True)\n\n#test\n#test_data.drop(test_data[test_data['walkDistance']>=10000].index,inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3d24257d2573e9c01e2c546d1ff59127909a6565"},"cell_type":"code","source":"# plot rideDistance\n\nplt.subplots(figsize=(12,4))\nsns.distplot(train_data.rideDistance,bins=10)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"474f1b437f625b5cd181dadce9718a490db66364"},"cell_type":"code","source":"#Remove the outliers.\n#test\ntrain_data.drop(train_data[train_data.rideDistance >=15000].index, inplace=True)\n\n#test\n#test_data.drop(test_data[test_data.rideDistance >=15000].index, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5176a35acd6d1df4e7cf627ef7d01ae669d9e66a"},"cell_type":"code","source":"(train_data['rideDistance']>=15000).value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1825e29c338933a0857e08a5958bf95ab4664078"},"cell_type":"code","source":"(test_data['rideDistance']>=15000).value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6483fef06d5d9d6b6fa7495fd7e2c5b2e0d1e5c9"},"cell_type":"code","source":"#SWIMDISTANCE\nplt.subplots(figsize=(12, 4))\nsns.distplot(train_data.swimDistance,bins=10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ad7659e9c2303d0a4423fa498f0040b64a2b5059"},"cell_type":"code","source":"(train_data['swimDistance']>=1000).value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0b31dad1ead57e90cb8fd11a33c614933f5f4bac"},"cell_type":"code","source":"#Remove the ouliers.\n#SwimDistance\n\n#train\ntrain_data.drop(train_data[train_data.swimDistance>=1000].index,inplace=True)\n\n#test\n#test_data.drop(test_data[test_data.swimDistance>=1000].index,inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6dbd2d5cc2f7c006dfe262434f0c9478777d5ee5"},"cell_type":"code","source":"(train_data['swimDistance']>=1000).value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7086b8c88f4905f561865e6cc1b9414bea7ad25f"},"cell_type":"code","source":"(test_data['swimDistance']>=1000).value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"60bc418bec2ddce4fa2d0984d00c2c68851f370c"},"cell_type":"code","source":"(train_data['totalDistance']>=15000).value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b77bbf0ad072f59db11b4f0edb6a00e9d8fc2543"},"cell_type":"code","source":"#Remove the outliers.\n\n#train\ntrain_data.drop(train_data[train_data.totalDistance>=15000].index,inplace=True)\n\n#test\n#test_data.drop(test_data[test_data.totalDistance>=15000].index,inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0f275363d02351e707f2561fd5e5e2976a9b15ed"},"cell_type":"code","source":"(train_data['totalDistance']>=15000).value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5463c0f0fbaf64d7e6be26339ad0b52972a788b8"},"cell_type":"code","source":"(test_data['totalDistance']>=15000).value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cd0228acd5e35aedf1257d188a9b60fafdf68999"},"cell_type":"code","source":"#REMOVING THE OUTLIERS RELATED TO HEALS","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7a77472dae24f6d23afdeedfcfe118c658a885c7"},"cell_type":"code","source":"#heals \nplt.subplots(figsize=(15,8))\nsns.countplot(data=train_data,x=train_data['heals'])\nplt.title('Heals')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2ce853389040ae7c6e368343f84b2f1cedb4301a"},"cell_type":"code","source":"(train_data['heals']>=40).value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ad6ea7bc7effec4fbbbb66dcf0d796849b2829b3"},"cell_type":"code","source":"# remove the outliers.\n#train\ntrain_data.drop(train_data[train_data.heals>=40].index,inplace=True)\n\n#test\n#test_data.drop(test_data[test_data.heals>=40].index,inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"abd09a90d4b9cd92bf00809014499d57947d34b8"},"cell_type":"code","source":"(train_data['heals']>=40).value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3735e8cc8d748eace2e9a75a399e3823df97075d"},"cell_type":"code","source":"#weapons\nplt.subplots(figsize=(15,8))\nsns.countplot(data=train_data,x=train_data['weaponsAcquired'])\nplt.title('weaponsAcquired')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"33f2b19a1b318b915f24dea144c670faec67dfde"},"cell_type":"code","source":"\n#weaponsAcquired\n#distplot\n\nplt.figure(figsize=(12,4))\nsns.distplot(train_data['weaponsAcquired'], bins=100)\nplt.show()\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4e56fbbc1d2cefb249575133f08483a66cc333f7"},"cell_type":"code","source":"(train_data['weaponsAcquired']>=50).value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e2214ec35d6eead6032b6ecb1ac15b59557b93dc"},"cell_type":"code","source":"# remove the outliers.\n\n#train\ntrain_data.drop(train_data[train_data.weaponsAcquired>=50].index,inplace=True)\n\n#test\n#test_data.drop(test_data[test_data.weaponsAcquired>=50].index,inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cfb5a60255108fd630dd3ee3d32dd07ecb3b61c3"},"cell_type":"code","source":"(train_data['weaponsAcquired']>=50).value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3fcfa957e12a7854b2ec1d4e3ee6d28fe79c39bc"},"cell_type":"code","source":"train_data.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ac9cd02487493bc674db966c553ccc2f513050fe"},"cell_type":"code","source":"#Categorical Variables","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"340561016e19f140326c9c13e486f65c4607ae41"},"cell_type":"code","source":"# Create the dummy variable for categorical variable present in our data set.\n#matchType\n#train\n\ntrain_data=pd.get_dummies(train_data,columns=['matchType'])\n\n#test\n\ntest_data=pd.get_dummies(test_data,columns=['matchType'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"64524bc9131b50658c30f57c47c37f3cdd8e84fe"},"cell_type":"code","source":"train_data.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"679f780fa966d681cbb267ff19a668ca37c191b0"},"cell_type":"code","source":"train_data.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ec3ece9ad6cd5114061546bd379c9557f5e1b8d6"},"cell_type":"code","source":"test_data.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"10c045f176b7b57d287310dbd777a8007a1a08d5"},"cell_type":"code","source":"#drop the all unnecessary columns\n#train_data\ntrain_data.drop(['killsWithoutMove'],axis=1,inplace=True)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c543d1c3f066008a11d098be0afc42e7b08dcf0e"},"cell_type":"code","source":"#drop the all unnecessary columns\n#test_data\n#test_data.drop(['killsWithoutMove'],axis=1,inplace=True)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f26cd57b245bef056bf11d5a3911702f3d23a7db"},"cell_type":"code","source":"#feature selection","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"31b811c6a524ab935607c3f254273ce9d7d3d9c7"},"cell_type":"code","source":"# feature selectionm using algorithm itself.\nfrom sklearn.ensemble import RandomForestRegressor","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"27fe9d87e85c9c175405a6f72807e36fff507e0d","_kg_hide-output":true},"cell_type":"code","source":"#before that first drop some unnecessary columns.\n\n#For train\n#Id #groupId  # matchId\ntrain_data.drop(['Id','groupId','matchId'],axis=1,inplace=True)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"16148d8387af184b841b9b14cca1a8df31e87720"},"cell_type":"code","source":"# For test\n# Save the test Id \ntest_id=test_data['Id']\n\ntest_data.drop(['Id','groupId','matchId'],axis=1,inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"939df8797985418a5aa90e88dcc6527ea9dbc880"},"cell_type":"code","source":"train_data.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8419152cd494ea3799a4c5022675ffe85d3e247b"},"cell_type":"code","source":"test_data.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e2f3781966abad564a9ddece2fd42d03c5ffb9b0"},"cell_type":"code","source":"#sample data for training\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"13f420ee735323b215868f303c1bf02ec5f4e516"},"cell_type":"code","source":"# Take sample for debugging and exploration\nsample = 500000\ndf_sample = train_data.sample(sample)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1a288ccc9d2bbdfd0ca311ec54c6e12b46694c84"},"cell_type":"code","source":"# Metric used for the PUBG competition (Mean Absolute Error (MAE))\nfrom sklearn.metrics import mean_absolute_error","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"86b0030a6a86268d8cca659d5880f641ca87ef01"},"cell_type":"code","source":"# Design model function. \n\ndef score(m : RandomForestRegressor):\n    res = ['mae train: ', mean_absolute_error(m.predict(X_train), y_train), \n           'mae val: ', mean_absolute_error(m.predict(X_valid), y_valid)]\n    if hasattr(m, 'oob_score_'): res.append(m.oob_score_)\n    print(res)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"39b264cd2c908d4d2112ababbcdbb94d108da74d"},"cell_type":"code","source":"df_sample.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5dea06cde845ee5fef7e182c7955da992a632115"},"cell_type":"code","source":"y=df_sample['winPlacePerc']\ndf = df_sample.drop(columns = ['winPlacePerc'])\ndf.shape\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6426d0ccdc1d30cb1984c5e93f69d136d930d4f6"},"cell_type":"code","source":"#train_data test split.\n\nfrom sklearn.model_selection import train_test_split\n\nX_train,X_valid,y_train,y_valid=train_test_split(df,y,test_size=0.3,random_state=40)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"054de695fcf8845d8c31a8613be36838fde1c683"},"cell_type":"code","source":"#model with certaion parameter. Ramdon forest algorithm with default parameters.\nm1 = RandomForestRegressor(n_estimators=40, min_samples_leaf=3, max_features='sqrt',\n                          n_jobs=-1)\n\nm1.fit(X_train, y_train)\nscore(m1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1042e26c22c8e874e7bda19f674e9da741143a2c"},"cell_type":"code","source":"# Find the important feature using random Forest algorithm.\n\nimportance=m1.feature_importances_\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3e6f631699fae896178f9e7f4410bece9d243e66"},"cell_type":"code","source":"# Create a new Dataframe for the given feature with their importance.\ndata=pd.DataFrame(sorted(zip(m1.feature_importances_, df.columns)), columns=['Value','Feature'])\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fb6f8bc3311c1d7a99fd19ec7f376082a5e098f8"},"cell_type":"code","source":"# Lets plot all the feature.\n\nplt.figure(figsize=(20, 10))\nsns.barplot(x=\"Value\", y=\"Feature\", data=data.sort_values(by=\"Value\", ascending=False))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3579f08c668e567885d693c6212d2c7f9c1033a6"},"cell_type":"code","source":"# select top 25 features and re-train the model.\n\nnew_data=data.sort_values(by='Value',ascending=False)[:25]\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f8b697e7d225e1dc7671db7340ba46ebd3ad9c1e"},"cell_type":"code","source":"new_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f79c492e4c903bdeb6037eb8a533959bf156a557"},"cell_type":"code","source":"\n\n# Plot all the variables.\nplt.subplots(figsize=(15,8))\nsns.barplot(x='Value',y='Feature',data=new_data)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"967803db743bbf970358349edf13704faae21949"},"cell_type":"code","source":"cols=new_data.Feature.values\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6ad3ff5a586e60873e6e586c7c20af8a1e17cf94"},"cell_type":"code","source":"cols","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2f18ef13af647f6e4d2b29a5131b3efcdcf3db46"},"cell_type":"code","source":"#Recreate the model using sample data.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"eb948932e50da9f0d6eddadf57c272a2a5d14beb"},"cell_type":"code","source":"# train and validation data\nX_train,X_valid,y_train,y_valid=train_test_split(df[cols],y,test_size=0.3,random_state=40)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5f48f346af5efc62c84683992cdfb25afcce630a"},"cell_type":"code","source":"X_train.shape\nprint(X_valid.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"faee05ec2b52ec01da4fb9e5827c59c2bca36ee3"},"cell_type":"code","source":"m1 = RandomForestRegressor(n_estimators=40, min_samples_leaf=3, max_features='sqrt',\n                          n_jobs=-1)\nm1.fit(X_train, y_train)\nscore(m1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"291dd3f60cd52ce5da65a790082ded9bee46847a"},"cell_type":"code","source":"#Final model training ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"431c857eb6d3355e6bd7cf5b460c5bcb42eee3f6"},"cell_type":"code","source":"y_final=train_data['winPlacePerc']\ndf_final = train_data.drop(columns = ['winPlacePerc'])\ndf_final.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"be8d973d2cef9a8e257ad53341ba5fe16546dc03"},"cell_type":"code","source":"X_train,X_valid,y_train,y_valid=train_test_split(df_final,y_final,test_size=0.3,random_state=40)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8c5d9b4673e117bfaa072d18837895cce9b27218"},"cell_type":"code","source":"m1 = RandomForestRegressor(n_estimators=40, min_samples_leaf=3, max_features='sqrt',\n                          n_jobs=-1)\nm1.fit(X_train, y_train)\nscore(m1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7f0b56894b460f8e3df2f85e1e0edb48ea882bb3"},"cell_type":"code","source":"# Replace all the infnite value from our test data. In Case ?\n\ntest_data.replace([np.inf, -np.inf], np.nan)\ntest_data.isnull().sum()\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c7000bbb9b7d1c62f5e55cf70ec42a5375ac2c91"},"cell_type":"code","source":"predictions = np.clip(a = m1.predict(test_data), a_min = 0.0, a_max = 1.0)\npred_df = pd.DataFrame({'Id' : test_id, 'winPlacePerc' : predictions})\n\n# Create submission file\npred_df.to_csv(\"submission.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"75d36cae22365e7117c0f2a4e4272d4b9c16eed3"},"cell_type":"code","source":"final_output=pd.read_csv('submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dedcc6a4f74bb10110ee4bf48fba9e5ed4ea97ab"},"cell_type":"code","source":"final_output.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ef5160072e5ca143fe3ab06257b3ef30dc74c9cd"},"cell_type":"code","source":"","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}