{"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":"2025-04-01T23:04:23.913529Z","iopub.execute_input":"2025-04-01T23:04:23.913853Z","iopub.status.idle":"2025-04-01T23:04:25.363425Z","shell.execute_reply.started":"2025-04-01T23:04:23.913814Z","shell.execute_reply":"2025-04-01T23:04:25.362213Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# install the autogluon library\n!pip install -U autogluon.tabular > /dev/null","metadata":{"execution":{"iopub.status.busy":"2024-06-21T10:02:09.411870Z","iopub.execute_input":"2024-06-21T10:02:09.412431Z","iopub.status.idle":"2024-06-21T10:02:39.434624Z","shell.execute_reply.started":"2024-06-21T10:02:09.412391Z","shell.execute_reply":"2024-06-21T10:02:39.432417Z"},"trusted":true},"outputs":[],"execution_count":null},{"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":"2025-04-01T23:04:29.318104Z","iopub.execute_input":"2025-04-01T23:04:29.318448Z","iopub.status.idle":"2025-04-01T23:04:32.277038Z","shell.execute_reply.started":"2025-04-01T23:04:29.318423Z","shell.execute_reply":"2025-04-01T23:04:32.275879Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(df_train)","metadata":{"execution":{"iopub.status.busy":"2025-04-01T23:04:37.226607Z","iopub.execute_input":"2025-04-01T23:04:37.226977Z","iopub.status.idle":"2025-04-01T23:04:37.234297Z","shell.execute_reply.started":"2025-04-01T23:04:37.226945Z","shell.execute_reply":"2025-04-01T23:04:37.233237Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# display the available features\nprint(df_train.columns)","metadata":{"execution":{"iopub.status.busy":"2025-04-01T23:05:04.269769Z","iopub.execute_input":"2025-04-01T23:05:04.270143Z","iopub.status.idle":"2025-04-01T23:05:04.277986Z","shell.execute_reply.started":"2025-04-01T23:05:04.270114Z","shell.execute_reply":"2025-04-01T23:05:04.276409Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.target.value_counts()","metadata":{"execution":{"iopub.status.busy":"2025-04-01T23:05:10.408524Z","iopub.execute_input":"2025-04-01T23:05:10.409464Z","iopub.status.idle":"2025-04-01T23:05:10.430981Z","shell.execute_reply.started":"2025-04-01T23:05:10.409427Z","shell.execute_reply":"2025-04-01T23:05:10.429726Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.query('target==0 and clusterEoP<1.2').clusterEoP.hist(bins=20,label='h').legend()\ndf_train.query('target==1 and clusterEoP<1.2').clusterEoP.hist(bins=20,label='e').legend()\ndf_train.query('target==2 and clusterEoP<1.2').clusterEoP.hist(bins=20,label='mu').legend()","metadata":{"execution":{"iopub.status.busy":"2025-04-01T23:07:39.506506Z","iopub.execute_input":"2025-04-01T23:07:39.507032Z","iopub.status.idle":"2025-04-01T23:07:39.990115Z","shell.execute_reply.started":"2025-04-01T23:07:39.506970Z","shell.execute_reply":"2025-04-01T23:07:39.988800Z"},"trusted":true},"outputs":[],"execution_count":null},{"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']\nfeature_column = ['clusterEoP',\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\ndf_train_sub = df_train[target_column + feature_column]","metadata":{"execution":{"iopub.status.busy":"2024-06-21T10:06:31.020738Z","iopub.execute_input":"2024-06-21T10:06:31.021693Z","iopub.status.idle":"2024-06-21T10:06:31.055984Z","shell.execute_reply.started":"2024-06-21T10:06:31.021655Z","shell.execute_reply":"2024-06-21T10:06:31.054589Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2025-04-01T23:08:26.324221Z","iopub.execute_input":"2025-04-01T23:08:26.324687Z","iopub.status.idle":"2025-04-01T23:08:26.352626Z","shell.execute_reply.started":"2025-04-01T23:08:26.324646Z","shell.execute_reply":"2025-04-01T23:08:26.351408Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 2. Setup autogluon and perfrom the training","metadata":{}},{"cell_type":"code","source":"from autogluon.tabular import TabularPredictor\n\n# Split the training set to train and validation\ntrain_data = df_train_sub.sample(frac=0.8, random_state=0)\nvalidation_data = df_train_sub.drop(train_data.index)","metadata":{"execution":{"iopub.status.busy":"2024-06-21T11:18:20.565805Z","iopub.execute_input":"2024-06-21T11:18:20.566268Z","iopub.status.idle":"2024-06-21T11:18:20.692362Z","shell.execute_reply.started":"2024-06-21T11:18:20.566236Z","shell.execute_reply":"2024-06-21T11:18:20.690829Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define and fit the AutoGluon classifier\nag = TabularPredictor(label='target', eval_metric='f1_macro') # many more configuration\npredictor = ag.fit(train_data, time_limit=3600) # adjust the time_limit","metadata":{"execution":{"iopub.status.busy":"2024-06-21T11:18:26.846023Z","iopub.execute_input":"2024-06-21T11:18:26.846424Z","iopub.status.idle":"2024-06-21T12:00:40.177098Z","shell.execute_reply.started":"2024-06-21T11:18:26.846392Z","shell.execute_reply":"2024-06-21T12:00:40.175601Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# display the trained models\npredictor.leaderboard(validation_data)","metadata":{"execution":{"iopub.status.busy":"2024-06-21T12:00:40.181188Z","iopub.execute_input":"2024-06-21T12:00:40.181665Z","iopub.status.idle":"2024-06-21T12:02:05.034196Z","shell.execute_reply.started":"2024-06-21T12:00:40.181626Z","shell.execute_reply":"2024-06-21T12:02:05.032843Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictor.feature_importance(validation_data)","metadata":{"execution":{"iopub.status.busy":"2024-06-21T12:02:05.036303Z","iopub.execute_input":"2024-06-21T12:02:05.036784Z","iopub.status.idle":"2024-06-21T12:02:15.061349Z","shell.execute_reply.started":"2024-06-21T12:02:05.036741Z","shell.execute_reply":"2024-06-21T12:02:15.057704Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# print the evaluation result for the best model\nresults = predictor.evaluate(validation_data)\nprediction_probabilities = predictor.predict_proba(validation_data)\nprint(prediction_probabilities)","metadata":{"execution":{"iopub.status.busy":"2024-06-21T12:02:15.066385Z","iopub.execute_input":"2024-06-21T12:02:15.066864Z","iopub.status.idle":"2024-06-21T12:02:19.160041Z","shell.execute_reply.started":"2024-06-21T12:02:15.066827Z","shell.execute_reply":"2024-06-21T12:02:19.158575Z"},"trusted":true},"outputs":[],"execution_count":null},{"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\n# make predictions on unlabeled sample\ny_pred = predictor.predict(df_test,model='CatBoost')\ny_pred","metadata":{"execution":{"iopub.status.busy":"2024-06-21T12:12:11.391481Z","iopub.execute_input":"2024-06-21T12:12:11.392878Z","iopub.status.idle":"2024-06-21T12:12:14.222859Z","shell.execute_reply.started":"2024-06-21T12:12:11.392834Z","shell.execute_reply":"2024-06-21T12:12:14.221540Z"},"trusted":true},"outputs":[],"execution_count":null},{"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\ndf_test['predict'] = y_pred\n\n# Select only the columns you need for the competition\ndf_for_submission = df_test[['index', 'predict']]\n\n# display the submission file\ndf_for_submission","metadata":{"execution":{"iopub.status.busy":"2024-06-21T12:12:24.741704Z","iopub.execute_input":"2024-06-21T12:12:24.742119Z","iopub.status.idle":"2024-06-21T12:12:24.758088Z","shell.execute_reply.started":"2024-06-21T12:12:24.742086Z","shell.execute_reply":"2024-06-21T12:12:24.756816Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Save to CSV for competition submission\ncompetition_file_submission_csv = 'my_submission.csv'\ndf_for_submission.to_csv(competition_file_submission_csv, index=False)","metadata":{"execution":{"iopub.status.busy":"2024-06-21T12:12:29.909100Z","iopub.execute_input":"2024-06-21T12:12:29.909545Z","iopub.status.idle":"2024-06-21T12:12:30.073805Z","shell.execute_reply.started":"2024-06-21T12:12:29.909507Z","shell.execute_reply":"2024-06-21T12:12:30.072626Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Now ready to submit! Good Luck!","metadata":{}}]}