{"cells":[{"metadata":{"trusted":true,"_uuid":"74e337bc3474703aadecceb1f65b26c6586f3fa3"},"cell_type":"code","source":"%load_ext autoreload\n%autoreload 2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ee56043e07b59bece549e47a09bc7cab69bb6e84"},"cell_type":"code","source":"!pip install fastai==0.7.0 --no-deps\n!pip install torch==0.4.1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dc3905588e88d8416741f361b2f2e116e65a44d2"},"cell_type":"code","source":"%matplotlib inline\nfrom fastai.imports import *\nfrom fastai.structured import *\nfrom pandas_summary import DataFrameSummary\nfrom sklearn.ensemble import RandomForestRegressor, RandomForestClassifier\nfrom IPython.display import display\n\nfrom sklearn import metrics","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c220513e535183458a8794199e83ea8bc309feee","_kg_hide-input":true},"cell_type":"code","source":"df_raw = pd.read_csv('../input/train_V2.csv')\ntrain_cats(df_raw)\nos.makedirs('tmp', exist_ok=True)\ndf_raw.to_feather('tmp/pubg-raw')\ndf_raw = pd.read_feather('tmp/pubg-raw')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d4864b15065bfd2dcae49b750e770d6620eb8b06"},"cell_type":"code","source":"df_test = pd.read_csv('../input/test_V2.csv')\ntrain_cats(df_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b9ea2a65187aaff76966d34031b736fd9f32d8be"},"cell_type":"code","source":"Id = df_test.Id","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"93cb133c4d6325f9d3762a4783fff28689a70316"},"cell_type":"code","source":"#df_raw.info()\n#df_raw.isnull().sum().sort_values(ascending=False)\ndf_raw['winPlacePerc'].fillna(0,inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"16a531e86cc36343a83a0f3945b455277bc790f3"},"cell_type":"code","source":"df_trn, y_trn, nas = proc_df(df_raw, 'winPlacePerc')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"973e243c619ea04360c855c86145ecbce5320dd8"},"cell_type":"code","source":"df_test,_,_ = proc_df(df_test,na_dict = nas)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fc05ef460f358361b3ed93eb7b6724b819bde9ef"},"cell_type":"code","source":"Id = df_test.Id","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7ed6eab8e3f2586947a8917697505b2a81b48a37"},"cell_type":"code","source":"def split_vals(a,n): return a[:n], a[n:]\nn_valid = 90000\nn_trn = len(df_trn)-n_valid\nX_train, X_valid = split_vals(df_trn, n_trn)\ny_train, y_valid = split_vals(y_trn, n_trn)\nraw_train, raw_valid = split_vals(df_raw, n_trn)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5942a439d7690bbf24cdd85ed87bf70bdb7b3c5f"},"cell_type":"code","source":"def rmse(x,y): return math.sqrt(((x-y)**2).mean())\n\ndef print_score(m):\n    res = [rmse(m.predict(X_train), y_train), rmse(m.predict(X_valid), y_valid),\n                m.score(X_train, y_train), m.score(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":"4e5cc347bf4bca294be5d42ce0af543095c03725"},"cell_type":"code","source":"set_rf_samples(500000)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"800c55b4092522c7bd346c3a1af288e811b193f2"},"cell_type":"code","source":"#m = RandomForestRegressor(n_estimators=40, min_samples_leaf=3, max_features=0.5, n_jobs=-1, oob_score=True)\n#m.fit(X_train, y_train)\n#print_score(m)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8338d048f653ae301e89f6609a801eff3baf70ec"},"cell_type":"code","source":"m = RandomForestRegressor(n_estimators=40, min_samples_leaf=3, max_features=0.6, n_jobs=-1, oob_score=True)\nm.fit(X_train, y_train)\nprint_score(m)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"fa7b51663afa6cc382d0871dd7d7de31e621dbd2"},"cell_type":"markdown","source":"## Feature Importance"},{"metadata":{"trusted":true,"_uuid":"2a98ba345bc11bac8385528d791c8ca03a3993f6"},"cell_type":"code","source":"fi = rf_feat_importance(m, df_trn);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5c84332fe847c519c8ab6aa03a1ef6a566fca07e"},"cell_type":"code","source":"def plot_fi(fi): return fi.plot('cols', 'imp', 'barh', figsize=(12,7), legend=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c10c3d912e22e5a3cf66c7823588e5044d1ba31b"},"cell_type":"code","source":"plot_fi(fi[:29])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"86523fca413bdb2dfe32d0866d351bbcf89bd49c"},"cell_type":"code","source":"to_keep = fi[fi.imp>0.005].cols; len(to_keep)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"adc1b2a8f297563ec436be406e858ec414190dab"},"cell_type":"code","source":"df_keep = df_trn[to_keep].copy()\nX_train, X_valid = split_vals(df_keep, n_trn)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"83eb2669c7be6878825a6a081226eac4854fc1eb"},"cell_type":"code","source":"m = RandomForestRegressor(n_estimators=40, min_samples_leaf=3, max_features=0.6, n_jobs=-1, oob_score=True)\nm.fit(X_train, y_train)\nprint_score(m)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"22a21436381cd57b4648dcbf52e9188aa574da12"},"cell_type":"code","source":"fi = rf_feat_importance(m, df_keep)\nplot_fi(fi);","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c93ccbad6001553d57de3aee35c2a84dc5eca3b8"},"cell_type":"markdown","source":"## Final model"},{"metadata":{"trusted":true,"_uuid":"91ba9bc77987a7822ce633e5d90c368b55abf716"},"cell_type":"code","source":"reset_rf_samples()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6155506db417198340d3fd6089743aca0a16ba66"},"cell_type":"code","source":"m = RandomForestRegressor(n_estimators=40, min_samples_leaf=3, max_features=0.6, n_jobs=-1, oob_score=True)\nm.fit(X_train, y_train)\nprint_score(m)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cd5ea821a8f2425467620b68e8c79e384f07848b"},"cell_type":"code","source":"#m = RandomForestRegressor(n_estimators=160, min_samples_leaf=3, max_features=0.6, n_jobs=-1, oob_score=True)\n#%time m.fit(X_train, y_train)\n#print_score(m)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8db99d0038b165616584870cc7f76c9243faf801"},"cell_type":"code","source":"df_ktest = df_test[to_keep].copy()\nypred = m.predict(df_ktest)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e5c3926879582a0c892c108b971cd960a1d8ede6"},"cell_type":"code","source":"submission = pd.DataFrame({ 'Id': Id,\n                            'winPlacePerc': ypred})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7821e5b1a835e522fe688bea089c21728dfc9c92"},"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4a2bb6d28ca5fc837afa7121112f522a1421f27d"},"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}