{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.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":101849,"databundleVersionId":12846694,"sourceType":"competition"}],"dockerImageVersionId":31089,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-07-08T11:11:53.711809Z","iopub.execute_input":"2025-07-08T11:11:53.712112Z","iopub.status.idle":"2025-07-08T11:11:53.716216Z","shell.execute_reply.started":"2025-07-08T11:11:53.712087Z","shell.execute_reply":"2025-07-08T11:11:53.715345Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nimport warnings\nwarnings.filterwarnings('ignore')\nfrom sklearn.ensemble import IsolationForest\nfrom sklearn.cluster import KMeans\nfrom sklearn.decomposition import PCA","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-08T11:11:57.928876Z","iopub.execute_input":"2025-07-08T11:11:57.929198Z","iopub.status.idle":"2025-07-08T11:11:57.935697Z","shell.execute_reply.started":"2025-07-08T11:11:57.929167Z","shell.execute_reply":"2025-07-08T11:11:57.934708Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data=pd.read_csv('/kaggle/input/ariel-data-challenge-2025/train.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-08T11:12:01.476739Z","iopub.execute_input":"2025-07-08T11:12:01.477035Z","iopub.status.idle":"2025-07-08T11:12:01.58813Z","shell.execute_reply.started":"2025-07-08T11:12:01.477013Z","shell.execute_reply":"2025-07-08T11:12:01.587161Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.head(3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-08T11:12:02.778259Z","iopub.execute_input":"2025-07-08T11:12:02.779005Z","iopub.status.idle":"2025-07-08T11:12:02.797941Z","shell.execute_reply.started":"2025-07-08T11:12:02.778975Z","shell.execute_reply":"2025-07-08T11:12:02.796986Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-08T11:12:05.192139Z","iopub.execute_input":"2025-07-08T11:12:05.192453Z","iopub.status.idle":"2025-07-08T11:12:05.199447Z","shell.execute_reply.started":"2025-07-08T11:12:05.192426Z","shell.execute_reply":"2025-07-08T11:12:05.198589Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"mean_spectrum = data.drop(columns=[\"planet_id\"]).mean()\nplt.figure(figsize=(14, 6))\nplt.plot(range(1, len(mean_spectrum) + 1), mean_spectrum.values)\nplt.title(\"Mean Spectrum Across All Planets\")\nplt.xlabel(\"Wavelength Index (wl_1 to wl_283)\")\nplt.ylabel(\"Mean Reflectance\")\nplt.grid(True)\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-08T11:12:07.745051Z","iopub.execute_input":"2025-07-08T11:12:07.7454Z","iopub.status.idle":"2025-07-08T11:12:08.019156Z","shell.execute_reply.started":"2025-07-08T11:12:07.745376Z","shell.execute_reply":"2025-07-08T11:12:08.018178Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_df = data.drop(columns=[\"planet_id\"]).T\nplt.figure(figsize=(18,7))\n\nfor i in range(sample_df.shape[1]):\n    plt.plot(sample_df.index.str.extract('(\\d+)').astype(int)[0], sample_df.iloc[:, i], label=f'Sample {i+1}')\n\nplt.title(\"Spectra of Planets\")\nplt.xlabel(\"Wavelength Index\")\nplt.ylabel(\"Reflectance\")\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-08T11:12:10.849508Z","iopub.execute_input":"2025-07-08T11:12:10.849834Z","iopub.status.idle":"2025-07-08T11:12:13.436328Z","shell.execute_reply.started":"2025-07-08T11:12:10.84981Z","shell.execute_reply":"2025-07-08T11:12:13.435312Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = data.drop(columns=[\"planet_id\"])\niso_forest = IsolationForest(contamination=0.01, random_state=42)\noutlier_labels = iso_forest.fit_predict(X)\n\nnormal_spectra = X[outlier_labels == 1].T\noutlier_spectra = X[outlier_labels == -1].T\nwavelength_indices = normal_spectra.index.str.extract('(\\d+)').astype(int)[0]\n\nplt.figure(figsize=(18, 7))\nfor i in range(normal_spectra.shape[1]):\n    plt.plot(wavelength_indices, normal_spectra.iloc[:, i], alpha=0.03, color='darkblue')\n\nfor i in range(outlier_spectra.shape[1]):\n    plt.plot(wavelength_indices, outlier_spectra.iloc[:, i], alpha=0.8, color='red', linewidth=1.2)\n\nplt.title(\"Spectra of Planets with Outliers Highlighted in Red\")\nplt.xlabel(\"Wavelength Index\")\nplt.ylabel(\"Reflectance\")\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-08T11:12:18.121913Z","iopub.execute_input":"2025-07-08T11:12:18.12222Z","iopub.status.idle":"2025-07-08T11:12:20.193217Z","shell.execute_reply.started":"2025-07-08T11:12:18.122198Z","shell.execute_reply":"2025-07-08T11:12:20.192291Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_with_labels = data.copy()\ndf_with_labels[\"outlier\"] = outlier_labels\noutlier_planets = df_with_labels[df_with_labels[\"outlier\"] == -1]\noutlier_details = outlier_planets.drop(columns=[\"outlier\"])\noutlier_spectra = outlier_details.drop(columns=[\"planet_id\"]).T\nwavelength_indices = outlier_spectra.index.str.extract('(\\d+)').astype(int)[0]\n\nplt.figure(figsize=(18, 8))\nfor i in range(outlier_spectra.shape[1]):\n    plt.plot(wavelength_indices, outlier_spectra.iloc[:, i], label=f'Planet ID: {outlier_details.iloc[i][\"planet_id\"]}')\n\nplt.title(\"Spectral Signatures of Outlier Planets\")\nplt.xlabel(\"Wavelength Index\")\nplt.ylabel(\"Reflectance\")\nplt.legend()\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-08T11:12:24.210323Z","iopub.execute_input":"2025-07-08T11:12:24.210643Z","iopub.status.idle":"2025-07-08T11:12:24.671656Z","shell.execute_reply.started":"2025-07-08T11:12:24.210599Z","shell.execute_reply":"2025-07-08T11:12:24.670638Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = data.drop(columns=[\"planet_id\"])\npca = PCA(n_components=2)\npca_result = pca.fit_transform(X)\npca_df = pd.DataFrame(pca_result, columns=[\"PC1\", \"PC2\"])\n\nkmeans = KMeans(n_clusters=4, random_state=42)\npca_df[\"Cluster\"] = kmeans.fit_predict(pca_result)\n\nplt.figure(figsize=(10, 7))\nsns.scatterplot(data=pca_df, x=\"PC1\", y=\"PC2\", hue=\"Cluster\", palette=\"Set2\", alpha=0.8)\nplt.title(\"KMeans Clustering of Planet Spectra in PCA Space\")\nplt.xlabel(\"Principal Component 1\")\nplt.ylabel(\"Principal Component 2\")\nplt.legend(title=\"Cluster\")\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-08T11:12:28.626011Z","iopub.execute_input":"2025-07-08T11:12:28.626384Z","iopub.status.idle":"2025-07-08T11:12:29.224183Z","shell.execute_reply.started":"2025-07-08T11:12:28.626358Z","shell.execute_reply":"2025-07-08T11:12:29.222912Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}