{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.10.12"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":93783,"databundleVersionId":11331308,"sourceType":"competition"}],"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false},"papermill":{"default_parameters":{},"duration":14.216835,"end_time":"2025-03-10T09:42:54.211228","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2025-03-10T09:42:39.994393","version":"2.6.0"}},"nbformat_minor":4,"nbformat":4,"cells":[{"id":"2df5ea52","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":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.execute_input":"2025-03-10T09:42:42.823224Z","iopub.status.busy":"2025-03-10T09:42:42.822854Z","iopub.status.idle":"2025-03-10T09:42:43.759339Z","shell.execute_reply":"2025-03-10T09:42:43.758113Z"},"papermill":{"duration":0.942541,"end_time":"2025-03-10T09:42:43.761214","exception":false,"start_time":"2025-03-10T09:42:42.818673","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"3d5e02ae","cell_type":"code","source":"# Load channels 41-46 and ground truth from the training set\nchannel_names = [f\"channel_{x}\" for x in range(41,46 + 1)]\ncolumns_to_load = [\"id\"] + channel_names + [\"is_anomaly\"]\ntrain_df = pd.read_parquet(\"/kaggle/input/esa-adb-challenge/train.parquet\", columns=columns_to_load)\ntrain_df","metadata":{"execution":{"iopub.execute_input":"2025-03-10T09:42:43.768171Z","iopub.status.busy":"2025-03-10T09:42:43.767655Z","iopub.status.idle":"2025-03-10T09:42:46.581760Z","shell.execute_reply":"2025-03-10T09:42:46.580729Z"},"papermill":{"duration":2.819445,"end_time":"2025-03-10T09:42:46.583626","exception":false,"start_time":"2025-03-10T09:42:43.764181","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"06793ac5","cell_type":"code","source":"# Calculate mean and std of nominal samples for each channel\ntrain_nominal_data = train_df.loc[train_df[\"is_anomaly\"] == 0, channel_names].values\nmeans = np.mean(train_nominal_data, axis=0)\nstds = np.std(train_nominal_data, axis=0)\nmeans, stds","metadata":{"execution":{"iopub.execute_input":"2025-03-10T09:42:46.590719Z","iopub.status.busy":"2025-03-10T09:42:46.590388Z","iopub.status.idle":"2025-03-10T09:42:47.338673Z","shell.execute_reply":"2025-03-10T09:42:47.337397Z"},"papermill":{"duration":0.753999,"end_time":"2025-03-10T09:42:47.340729","exception":false,"start_time":"2025-03-10T09:42:46.586730","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"17ce5d64","cell_type":"code","source":"# Load test set\ntest_df = pd.read_parquet(\"/kaggle/input/esa-adb-challenge/test.parquet\", columns=[\"id\"] + channel_names)\ntest_df","metadata":{"execution":{"iopub.execute_input":"2025-03-10T09:42:47.348863Z","iopub.status.busy":"2025-03-10T09:42:47.348525Z","iopub.status.idle":"2025-03-10T09:42:47.534915Z","shell.execute_reply":"2025-03-10T09:42:47.533364Z"},"papermill":{"duration":0.19328,"end_time":"2025-03-10T09:42:47.537850","exception":false,"start_time":"2025-03-10T09:42:47.344570","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"9092adf5","cell_type":"code","source":"# Find test samples with values outside of (mean ± N * std) for any channel\nN = 3\ntest_data = test_df.loc[:, channel_names]\ndetections = (test_data > (means + N * stds)) | (test_data < (means - N * stds))\naggregated_detections = detections.max(axis=1).astype(np.uint8)","metadata":{"execution":{"iopub.execute_input":"2025-03-10T09:42:47.546407Z","iopub.status.busy":"2025-03-10T09:42:47.546020Z","iopub.status.idle":"2025-03-10T09:42:47.662511Z","shell.execute_reply":"2025-03-10T09:42:47.661479Z"},"papermill":{"duration":0.122876,"end_time":"2025-03-10T09:42:47.664306","exception":false,"start_time":"2025-03-10T09:42:47.541430","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"582969bc","cell_type":"code","source":"# Save to submission file\nsubmission_df = pd.DataFrame(columns=[\"id\", \"is_anomaly\"])\nsubmission_df[\"id\"] = test_df[\"id\"]\nsubmission_df[\"is_anomaly\"] = aggregated_detections\nsubmission_df.to_parquet(f\"/kaggle/working/GlobalSTD{N}-channels41-46.parquet\")\n# Generate a file supported by the Kaggle \"Submit to competition\" option\nsubmission_df.to_csv(f\"/kaggle/working/submission.csv\", index=False)","metadata":{"execution":{"iopub.execute_input":"2025-03-10T09:42:47.672229Z","iopub.status.busy":"2025-03-10T09:42:47.671829Z","iopub.status.idle":"2025-03-10T09:42:48.191030Z","shell.execute_reply":"2025-03-10T09:42:48.189919Z"},"papermill":{"duration":0.525087,"end_time":"2025-03-10T09:42:48.192788","exception":false,"start_time":"2025-03-10T09:42:47.667701","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"aff4778e","cell_type":"code","source":"submission_df.plot(\"id\", \"is_anomaly\", title=f\"Results for N={N}\")","metadata":{"execution":{"iopub.execute_input":"2025-03-10T09:42:48.200715Z","iopub.status.busy":"2025-03-10T09:42:48.200408Z","iopub.status.idle":"2025-03-10T09:42:50.915646Z","shell.execute_reply":"2025-03-10T09:42:50.914619Z"},"papermill":{"duration":2.720963,"end_time":"2025-03-10T09:42:50.917357","exception":false,"start_time":"2025-03-10T09:42:48.196394","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"77cfe461","cell_type":"code","source":"# Try with different N\nN = 5\ndetections = (test_data > (means + N * stds)) | (test_data < (means - N * stds))\naggregated_detections = detections.max(axis=1).astype(np.uint8)\nsubmission_df[\"is_anomaly\"] = aggregated_detections\nsubmission_df.to_parquet(f\"/kaggle/working/GlobalSTD{N}-channels41-46.parquet\")","metadata":{"execution":{"iopub.execute_input":"2025-03-10T09:42:50.926707Z","iopub.status.busy":"2025-03-10T09:42:50.926301Z","iopub.status.idle":"2025-03-10T09:42:51.069680Z","shell.execute_reply":"2025-03-10T09:42:51.068500Z"},"papermill":{"duration":0.150335,"end_time":"2025-03-10T09:42:51.071715","exception":false,"start_time":"2025-03-10T09:42:50.921380","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"46501122","cell_type":"code","source":"submission_df.plot(\"id\", \"is_anomaly\", title=f\"Results for N={N}\")","metadata":{"execution":{"iopub.execute_input":"2025-03-10T09:42:51.081354Z","iopub.status.busy":"2025-03-10T09:42:51.080947Z","iopub.status.idle":"2025-03-10T09:42:53.484339Z","shell.execute_reply":"2025-03-10T09:42:53.483235Z"},"papermill":{"duration":2.410053,"end_time":"2025-03-10T09:42:53.485981","exception":false,"start_time":"2025-03-10T09:42:51.075928","status":"completed"},"tags":[]},"outputs":[],"execution_count":null}]}