{"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":30746,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"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","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 1. Create train test files for participants","metadata":{}},{"cell_type":"code","source":"import uproot\nimport numpy as np\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split\nfrom tqdm.auto import tqdm\ntqdm.pandas()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_files = uproot.concatenate('Dstellnu_fsp.root:fsp',library=\"np\")\ndf = pd.DataFrame(input_files)\ndf.mcPDG.value_counts()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# remove the fake tracks(and 1000010050)\ndf_TrueParticle = df.query('mcPDG<3000').copy()\n# define the target variable\ndf_TrueParticle['mcPDG'] = df_TrueParticle['mcPDG'].astype(int)\nmapping = {-211:0,\n           211:0,\n           -321:0,\n           321:0,\n           -2212:0,\n           2212:0,\n           -11:1,\n           11:1,\n           -13:2,\n           13:2}\ndf_TrueParticle['target'] = df_TrueParticle['mcPDG'].map(mapping)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# prepare the train, test files\ntarget_column = 'target'\nfeature_column = [\n    'E','p','px','py','pz','pt','M','dM',\n    'clusterEoP', 'clusterE1E9', 'clusterE9E21', 'clusterLAT',\n    'clusterAbsZernikeMoment40', 'clusterAbsZernikeMoment51',\n    'clusterZernikeMVA', 'clusterDeltaLTemp',\n    'clusterPulseShapeDiscriminationMVA',\n    'eID_CDC', 'eID_TOP', 'eID_ARICH',\n    'muID_CDC', 'muID_TOP', 'muID_ARICH', 'muID_KLM']\n\nX = df_TrueParticle[feature_column]\ny = df_TrueParticle[target_column]\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)\n\n# Combine features and target for the training set\ntrain_df = X_train.copy()\ntrain_df[target_column] = y_train\ntrain_df.to_csv('train.csv', index=False)\n\n# For the test set, save without the target variable\ntest_df = X_test.copy()\ntest_df.to_csv('test.csv', index_label='index')\n\n# combine features and true target for validation purposes\ntest_with_label = X_test.copy()\ntest_with_label[target_column] = y_test\ntest_with_label.to_csv('test_with_label.csv', index_label='index')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df_TrueParticle.isna().sum())","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2. Create the solution","metadata":{}},{"cell_type":"code","source":"import pandas as pd\ntest_with_label = pd.read_csv('test_with_label.csv')\nsolution = test_with_label[['index','target']].copy()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"public_sample = solution.sample(frac=0.3, random_state=0).index\nprivate_sample = solution.drop(public_sample).index\n\nsolution.loc[public_sample, 'Usage'] = 'Public'\nsolution.loc[private_sample, 'Usage'] = 'Private'\n\n# Save the solution CSV file\nsolution.to_csv('solution.csv', index=False)\n\nsolution","metadata":{},"execution_count":null,"outputs":[]}]}