{"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_type":"markdown","source":"Importing Libraries and Data Loading \n* We are Using UGradientBoostingClassifier for Task of Classification"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"\n\nimport pandas as pd\nfrom hep_ml.gradientboosting import UGradientBoostingClassifier\nfrom hep_ml.losses import BinFlatnessLossFunction\n\ntrain = pd.read_csv(\"../input/training.csv\")\ntest  = pd.read_csv(\"../input/test.csv\")\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Feature Extraction \nLoading Pandas File and Generating Some New Features that other people on Kaggle uses for this competition. Also use in another challange \"Flavor of Physics find uu (4year Ago)\"."},{"metadata":{"trusted":true},"cell_type":"code","source":"def add_features(df):\n    # features used by the others on Kaggle\n    df['NEW_FD_SUMP']=df['FlightDistance']/(df['p0_p']+df['p1_p']+df['p2_p'])\n    df['NEW5_lt']=df['LifeTime']*(df['p0_IP']+df['p1_IP']+df['p2_IP'])/3\n    df['p_track_Chi2Dof_MAX'] = df.loc[:, ['p0_track_Chi2Dof', 'p1_track_Chi2Dof', 'p2_track_Chi2Dof']].max(axis=1)\n\n    df['flight_dist_sig2'] = (df['FlightDistance']/df['FlightDistanceError'])**2\n    # features from phunter\n    df['flight_dist_sig'] = df['FlightDistance']/df['FlightDistanceError']\n    df['NEW_IP_dira'] = df['IP']*df['dira']\n    df['p0p2_ip_ratio']=df['IP']/df['IP_p0p2']\n    df['p1p2_ip_ratio']=df['IP']/df['IP_p1p2']\n    df['DCA_MAX'] = df.loc[:, ['DOCAone', 'DOCAtwo', 'DOCAthree']].max(axis=1)\n    df['iso_bdt_min'] = df.loc[:, ['p0_IsoBDT', 'p1_IsoBDT', 'p2_IsoBDT']].min(axis=1)\n    df['iso_min'] = df.loc[:, ['isolationa', 'isolationb', 'isolationc','isolationd', 'isolatione', 'isolationf']].min(axis=1)\n    \n    #feature from ES\n    df['NEW_iso_abc'] = df['isolationa']*df['isolationb']*df['isolationc']\n    df['NEW_iso_def'] = df['isolationd']*df['isolatione']*df['isolationf']\n    df['NEW_pN_IP'] = df['p0_IP']+df['p1_IP']+df['p2_IP']\n    df['NEW_pN_p']  = df['p0_p']+df['p1_p']+df['p2_p']\n    df['NEW_IP_pNpN'] = df['IP_p0p2']*df['IP_p1p2']\n    df['NEW_pN_IPSig'] = df['p0_IPSig']+df['p1_IPSig']+df['p2_IPSig']\n    # \"super\" feature changing the result from 0.988641 to 0.991099\n    df['NEW_FD_LT']=df['FlightDistance']/df['LifeTime']\n    return df","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Make Variable for Training and Testing "},{"metadata":{"trusted":true},"cell_type":"code","source":"train = add_features(train)\ntest = add_features(test)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Feature Selection: Keep Only Important Features because unimportant features sometime leads to overfitting the model."},{"metadata":{"trusted":true},"cell_type":"code","source":"\n\nprint(\"Eliminate features\")\nfilter_out = ['id', 'min_ANNmuon', 'production', 'mass', 'signal',\n              'SPDhits','CDF1', 'CDF2', 'CDF3',\n              'isolationb', 'isolationc','p0_pt', 'p1_pt', 'p2_pt',\n              'p0_p', 'p1_p', 'p2_p', 'p0_eta', 'p1_eta', 'p2_eta',\n              'isolationa', 'isolationb', 'isolationc', 'isolationd', 'isolatione', 'isolationf',\n              'p0_IsoBDT', 'p1_IsoBDT', 'p2_IsoBDT',\n              'p0_IP', 'p1_IP', 'p2_IP',\n              'IP_p0p2', 'IP_p1p2',\n              'p0_track_Chi2Dof', 'p1_track_Chi2Dof', 'p2_track_Chi2Dof',\n              'p0_IPSig', 'p1_IPSig', 'p2_IPSig',\n              'DOCAone', 'DOCAtwo', 'DOCAthree']\n\nfeatures = list(f for f in train.columns if f not in filter_out)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Model Declaration and Fitting Training Data to Model. (Model Training)\nUGBoost Classifier with 900 estimators and Maximum depth of 6 with learning rate of 0.15"},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"Train a UGradientBoostingClassifier\")\nloss = BinFlatnessLossFunction(['mass'], n_bins=15, uniform_label=0 , fl_coefficient=15, power=2)\nugbc = UGradientBoostingClassifier(loss=loss, n_estimators=900,\n                                 max_depth=6,\n                                 learning_rate=0.15,\n                                 train_features=features,\n                                 subsample=0.7,\n                                 random_state=1234)\nugbc.fit(train[features + ['mass']], train['signal'])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Predicting using model on Test Dataset"},{"metadata":{"trusted":true},"cell_type":"code","source":"test_probs = ugbc.predict_proba(test[features])[:,1]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Submit Prediction for Results on Leaderboard. My Name will be not shown on Leaderboard because Competition deadline is over now. But we will gain overposition in Top 30 (انشا اللہ)"},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = pd.DataFrame({\"id\": test[\"id\"], \"prediction\": test_probs})\nsubmission.to_csv(\"AliAdAIFinalPaper.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"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.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}