{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":81378,"databundleVersionId":8859157,"sourceType":"competition"}],"dockerImageVersionId":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## 0. File location and library installation","metadata":{}},{"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\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 read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-06-20T16:33:12.663754Z","iopub.execute_input":"2024-06-20T16:33:12.664573Z","iopub.status.idle":"2024-06-20T16:33:13.778308Z","shell.execute_reply.started":"2024-06-20T16:33:12.664536Z","shell.execute_reply":"2024-06-20T16:33:13.777022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# install the autogluon library\n!pip install -U autogluon.tabular > /dev/null","metadata":{"execution":{"iopub.status.busy":"2024-06-20T16:33:17.032210Z","iopub.execute_input":"2024-06-20T16:33:17.033228Z","iopub.status.idle":"2024-06-20T16:33:44.600097Z","shell.execute_reply.started":"2024-06-20T16:33:17.033182Z","shell.execute_reply":"2024-06-20T16:33:44.598581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 1. Load the training sample","metadata":{}},{"cell_type":"code","source":"import pandas as pd\n\n# Create Pandas DataFrames (df) from csv files and shuffle the rows\ndf_train = pd.read_csv('/kaggle/input/2024-b2sw-ml/train.csv').sample(frac=1)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T16:36:23.658900Z","iopub.execute_input":"2024-06-20T16:36:23.659258Z","iopub.status.idle":"2024-06-20T16:36:26.616846Z","shell.execute_reply.started":"2024-06-20T16:36:23.659231Z","shell.execute_reply":"2024-06-20T16:36:26.615659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# display the available features\nprint(df_train.columns)\n\ndf_train.hist('target', bins=15)\n\ndf_train.isna() # Check if there are null values","metadata":{"execution":{"iopub.status.busy":"2024-06-16T16:37:36.127413Z","iopub.execute_input":"2024-06-16T16:37:36.12877Z","iopub.status.idle":"2024-06-16T16:37:36.138588Z","shell.execute_reply.started":"2024-06-16T16:37:36.128707Z","shell.execute_reply":"2024-06-16T16:37:36.136874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# define relevant variables to your training\n# choose features that you think are relevant\n# read more in the discussion tab on kaggle\ntarget_column = ['target']\n# feature_column = ['clusterEoP', 'eID_CDC','muID_KLM']\n\nfeature_column = ['clusterEoP',                  # 'p', 'px', 'py', 'pz', 'pt',\n'clusterE1E9', 'clusterE9E21', 'clusterLAT',\\\n'clusterAbsZernikeMoment40', 'clusterAbsZernikeMoment51',\\\n'clusterZernikeMVA', 'clusterDeltaLTemp',\\\n'clusterPulseShapeDiscriminationMVA', 'eID_CDC', 'eID_TOP', 'eID_ARICH',\\\n'muID_CDC', 'muID_TOP', 'muID_ARICH', 'muID_KLM']\n\ntempVar = ['p']\n\n\n\ndf_train_sub = df_train[target_column + feature_column + tempVar]","metadata":{"execution":{"iopub.status.busy":"2024-06-16T16:38:40.938078Z","iopub.execute_input":"2024-06-16T16:38:40.938613Z","iopub.status.idle":"2024-06-16T16:38:40.957518Z","shell.execute_reply.started":"2024-06-16T16:38:40.938573Z","shell.execute_reply":"2024-06-16T16:38:40.956115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2. Setup autogluon and perfrom the training","metadata":{}},{"cell_type":"code","source":"from autogluon.tabular import TabularPredictor\n\nlowMom = df_train_sub[df_train_sub[\"p\"] < 1.6]\n#midMom = df_train_sub[(df_train_sub[\"p\"] >= 1.2) & (df_train_sub[\"p\"] < 3.0)]\nhighMom = df_train_sub[df_train_sub[\"p\"] >= 1.6]","metadata":{"execution":{"iopub.status.busy":"2024-06-16T16:39:10.765942Z","iopub.execute_input":"2024-06-16T16:39:10.767Z","iopub.status.idle":"2024-06-16T16:39:12.184042Z","shell.execute_reply.started":"2024-06-16T16:39:10.766954Z","shell.execute_reply":"2024-06-16T16:39:12.182571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from autogluon.tabular import TabularPredictor\n\n# Split the training set to train and validation\n\n#train_data = df_train_sub.sample(frac=0.8, random_state=0)\n#validation_data = df_train_sub.drop(train_data.index)\n\nlowMom_train_data = lowMom.sample(frac=0.8, random_state=0)\nlowMom_validation_data = lowMom.drop(lowMom_train_data.index)\n\n#midMom_train_data = midMom.sample(frac=0.8, random_state=0)\n#midMom_validation_data = midMom.drop(midMom_train_data.index)\n\nhighMom_train_data = highMom.sample(frac=0.8, random_state=0)\nhighMom_validation_data = highMom.drop(highMom_train_data.index)\n\n#------------\n\nlowMom_train_noP = lowMom_train_data[target_column + feature_column]\nlowMom_validation_noP = lowMom_validation_data[target_column + feature_column]\n\n#midMom_train_noP = midMom_train_data[target_column + feature_column]\n#midMom_validation_noP = midMom_validation_data[target_column + feature_column]\n\nhighMom_train_noP = highMom_train_data[target_column + feature_column]\nhighMom_validation_noP = highMom_validation_data[target_column + feature_column]\n\n#predictor = TabularPredictor(label='target').fit(train_data=<file-name>)","metadata":{"execution":{"iopub.status.busy":"2024-06-16T16:39:22.182516Z","iopub.execute_input":"2024-06-16T16:39:22.183954Z","iopub.status.idle":"2024-06-16T16:41:06.909212Z","shell.execute_reply.started":"2024-06-16T16:39:22.183905Z","shell.execute_reply":"2024-06-16T16:41:06.90803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define and fit the AutoGluon classifier\n#ag = TabularPredictor(label='target', eval_metric='f1_macro') # many more configuration\n\n# f1 metric is being used for evaluation, target is particle label\n\nlowMom_ag = TabularPredictor(label='target', eval_metric='f1_macro')\n#midMom_ag = TabularPredictor(label='target', eval_metric='f1_macro')\nhighMom_ag = TabularPredictor(label='target', eval_metric='f1_macro')\n\n# predictor = ag.fit(train_data, presets='best_quality',num_bag_folds=10,num_stack_levels=3, time_limit=3000)\n# adjust the time_limit ~ Need to extend to longer than 100 s\n\nlowMom_predictor = lowMom_ag.fit(lowMom_train_noP, presets='best_quality',auto_stack=True, time_limit=18000)\n#midMom_predictor = midMom_ag.fit(midMom_train_noP, presets='best_quality',num_bag_folds=10,num_stack_levels=3, time_limit=3300)\nhighMom_predictor = highMom_ag.fit(highMom_train_noP, presets='best_quality',auto_stack=True, time_limit=13000)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# display the trained models\n# predictor.leaderboard(validation_data)\n\nlowMom_predictor.leaderboard(lowMom_validation_noP)\nprint('')\n#midMom_predictor.leaderboard(midMom_validation_noP)\nprint('')\nhighMom_predictor.leaderboard(highMom_validation_noP)\n\n#print(\"AutoGluon infers problem type is: \", predictor.problem_type)\n#print(\"AutoGluon identified the following types of features:\")\n#print(predictor.feature_metadata)\n\n#Feature performance!\n#predictor.feature_importance(train_data)","metadata":{"execution":{"iopub.status.busy":"2024-06-16T16:41:39.724998Z","iopub.execute_input":"2024-06-16T16:41:39.726468Z","iopub.status.idle":"2024-06-16T16:41:42.12156Z","shell.execute_reply.started":"2024-06-16T16:41:39.726427Z","shell.execute_reply":"2024-06-16T16:41:42.120245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#predictor.model_best\n\nprint(lowMom_predictor.model_best)\n#print(midMom_predictor.model_best)\nprint(highMom_predictor.model_best)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print the evaluation result for the best model\n#results = predictor.evaluate(validation_data)\n#print(results)\n\nlowMom_results = lowMom_predictor.evaluate(lowMom_validation_data)\nprint(lowMom_results)\nprint('')\n#midMom_results = midMom_predictor.evaluate(midMom_validation_data)\n#print(midMom_results)\nprint('')\nhighMom_results = highMom_predictor.evaluate(highMom_validation_data)\nprint(highMom_results)","metadata":{"execution":{"iopub.status.busy":"2024-06-16T16:42:08.094811Z","iopub.execute_input":"2024-06-16T16:42:08.095197Z","iopub.status.idle":"2024-06-16T16:42:09.749715Z","shell.execute_reply.started":"2024-06-16T16:42:08.095169Z","shell.execute_reply":"2024-06-16T16:42:09.748505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 3. Load the test sample and make predictions","metadata":{}},{"cell_type":"code","source":"df_test = pd.read_csv('/kaggle/input/2024-b2sw-ml/test.csv')\n\ndf_test['sorting'] = list(range(0, len(df_test)))\ndf_test['former_index'] = df_test.index\n\nlowMom_test = df_test[df_test[\"p\"] < 1.6]\n#midMom_test = df_test[(df_test[\"p\"] >= 1.2) & (df_test[\"p\"] < 3.0)]\nhighMom_test = df_test[df_test[\"p\"] >= 1.6]\n\n# make predictions on unlabeled sample\nlowMom_y_pred = lowMom_predictor.predict(lowMom_test)\n#midMom_y_pred = midMom_predictor.predict(midMom_test)\nhighMom_y_pred = highMom_predictor.predict(highMom_test)\n\nlowMom_y_pred\n#midMom_y_pred\nhighMom_y_pred","metadata":{"execution":{"iopub.status.busy":"2024-06-16T16:43:54.658095Z","iopub.execute_input":"2024-06-16T16:43:54.658527Z","iopub.status.idle":"2024-06-16T16:43:58.734916Z","shell.execute_reply.started":"2024-06-16T16:43:54.658467Z","shell.execute_reply":"2024-06-16T16:43:58.733601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 4. Save the submission file for the competition","metadata":{}},{"cell_type":"code","source":"# Append the results of the evaluation to the dataframe with the test data\n#df_test['predict'] = y_pred\n\n#lowMom_test['predict'] = lowMom_y_pred\n#midMom_test['predict'] = midMom_y_pred\n#highMom_test['predict'] = highMom_y_pred\n\nframes = [lowMom_y_pred, highMom_y_pred]\nresult = pd.concat(frames)\n\nframes2 = [lowMom_test, highMom_test]\nreformed_test = pd.concat(frames2)\n\nreformed_test['predict'] = result\n\n#result = result.set_index('sorting')\n#result = result.reindex(index=df_test['sorting'])\n#result = result.set_index('former_index')\n\nreformed_test = reformed_test.set_index('sorting')\nreformed_test = reformed_test.reindex(index=df_test['sorting'])\nreformed_test = reformed_test.set_index('former_index')\n\n# Select only the columns you need for the competition\n#df_for_submission = df_test[['index', 'predict']] \n\ndf_for_submission = reformed_test[['index', 'predict']] \n\n# display the submission file\ndf_for_submission","metadata":{"execution":{"iopub.status.busy":"2024-06-16T16:50:16.360784Z","iopub.execute_input":"2024-06-16T16:50:16.361249Z","iopub.status.idle":"2024-06-16T16:50:16.375863Z","shell.execute_reply.started":"2024-06-16T16:50:16.361216Z","shell.execute_reply":"2024-06-16T16:50:16.374572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save to CSV for competition submission\ncompetition_file_submission_csv = 'submission.csv'\ndf_for_submission.to_csv(competition_file_submission_csv, index=False)","metadata":{"execution":{"iopub.status.busy":"2024-06-16T16:50:45.806027Z","iopub.execute_input":"2024-06-16T16:50:45.806539Z","iopub.status.idle":"2024-06-16T16:50:45.967024Z","shell.execute_reply.started":"2024-06-16T16:50:45.806504Z","shell.execute_reply":"2024-06-16T16:50:45.965878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Now ready to submit! Good Luck!","metadata":{}}]}