{"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":11.837758,"end_time":"2025-03-10T09:42:27.846000","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2025-03-10T09:42:16.008242","version":"2.6.0"}},"nbformat_minor":4,"nbformat":4,"cells":[{"id":"c6abb493","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:19.658583Z","iopub.status.busy":"2025-03-10T09:42:19.658084Z","iopub.status.idle":"2025-03-10T09:42:20.708713Z","shell.execute_reply":"2025-03-10T09:42:20.707376Z"},"papermill":{"duration":1.057333,"end_time":"2025-03-10T09:42:20.710763","exception":false,"start_time":"2025-03-10T09:42:19.653430","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"c1d52dfe","cell_type":"code","source":"# Load channels 41-46 and ground truth from the test set\nchannel_names = [f\"channel_{x}\" for x in range(41,46 + 1)]\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:20.719068Z","iopub.status.busy":"2025-03-10T09:42:20.718541Z","iopub.status.idle":"2025-03-10T09:42:21.147743Z","shell.execute_reply":"2025-03-10T09:42:21.146727Z"},"papermill":{"duration":0.435544,"end_time":"2025-03-10T09:42:21.149731","exception":false,"start_time":"2025-03-10T09:42:20.714187","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"330809bb","cell_type":"code","source":"# Calculate mean and std for each channel\ntest_data = test_df.loc[:, channel_names].values\nmeans = np.mean(test_data, axis=0)\nstds = np.std(test_data, axis=0)\nmeans, stds","metadata":{"execution":{"iopub.execute_input":"2025-03-10T09:42:21.158258Z","iopub.status.busy":"2025-03-10T09:42:21.157787Z","iopub.status.idle":"2025-03-10T09:42:21.205891Z","shell.execute_reply":"2025-03-10T09:42:21.204580Z"},"papermill":{"duration":0.054633,"end_time":"2025-03-10T09:42:21.208037","exception":false,"start_time":"2025-03-10T09:42:21.153404","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"31529b66","cell_type":"code","source":"# Find test samples with values outside of (mean ± N * std) for any channel\nN = 3\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:21.221469Z","iopub.status.busy":"2025-03-10T09:42:21.221098Z","iopub.status.idle":"2025-03-10T09:42:21.248590Z","shell.execute_reply":"2025-03-10T09:42:21.247441Z"},"papermill":{"duration":0.036366,"end_time":"2025-03-10T09:42:21.250649","exception":false,"start_time":"2025-03-10T09:42:21.214283","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"d687f617","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/LocalSTD{N}-channels-41-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:21.258751Z","iopub.status.busy":"2025-03-10T09:42:21.258257Z","iopub.status.idle":"2025-03-10T09:42:21.768888Z","shell.execute_reply":"2025-03-10T09:42:21.767711Z"},"papermill":{"duration":0.517235,"end_time":"2025-03-10T09:42:21.771286","exception":false,"start_time":"2025-03-10T09:42:21.254051","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"32affcbe","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:21.784648Z","iopub.status.busy":"2025-03-10T09:42:21.784294Z","iopub.status.idle":"2025-03-10T09:42:24.695101Z","shell.execute_reply":"2025-03-10T09:42:24.694068Z"},"papermill":{"duration":2.919357,"end_time":"2025-03-10T09:42:24.696748","exception":false,"start_time":"2025-03-10T09:42:21.777391","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"104232dc","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/LocalSTD{N}-channels41-46.parquet\")","metadata":{"execution":{"iopub.execute_input":"2025-03-10T09:42:24.706387Z","iopub.status.busy":"2025-03-10T09:42:24.706015Z","iopub.status.idle":"2025-03-10T09:42:24.798335Z","shell.execute_reply":"2025-03-10T09:42:24.797103Z"},"papermill":{"duration":0.099405,"end_time":"2025-03-10T09:42:24.800432","exception":false,"start_time":"2025-03-10T09:42:24.701027","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"id":"65beb64b","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:24.810168Z","iopub.status.busy":"2025-03-10T09:42:24.809789Z","iopub.status.idle":"2025-03-10T09:42:27.318450Z","shell.execute_reply":"2025-03-10T09:42:27.317244Z"},"papermill":{"duration":2.515617,"end_time":"2025-03-10T09:42:27.320344","exception":false,"start_time":"2025-03-10T09:42:24.804727","status":"completed"},"tags":[]},"outputs":[],"execution_count":null}]}