{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceType":"competition","sourceId":131914,"databundleVersionId":15827034}],"dockerImageVersionId":31287,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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":{"iopub.status.busy":"2026-02-26T06:01:51.754477Z","iopub.execute_input":"2026-02-26T06:01:51.754799Z","iopub.status.idle":"2026-02-26T06:01:52.650942Z","shell.execute_reply.started":"2026-02-26T06:01:51.754768Z","shell.execute_reply":"2026-02-26T06:01:52.650165Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install anndata","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-26T06:03:10.801008Z","iopub.execute_input":"2026-02-26T06:03:10.801556Z","iopub.status.idle":"2026-02-26T06:03:16.926519Z","shell.execute_reply.started":"2026-02-26T06:03:10.801529Z","shell.execute_reply":"2026-02-26T06:03:16.925816Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import anndata as ad\nimport pandas as pd\nimport numpy as np\nfrom sklearn.decomposition import TruncatedSVD\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.metrics import cohen_kappa_score\nfrom sklearn.model_selection import train_test_split\nimport warnings\nwarnings.filterwarnings('ignore')\n\n# 1. Load data using your exact Kaggle paths\ntrain_path = '/kaggle/input/26-spring-cell-type-annotation/train.h5ad'\ntest_path = '/kaggle/input/26-spring-cell-type-annotation/test.h5ad'\n\nprint(\"Loading data...\")\n# Read the AnnData format (.h5ad)\nadata_train = ad.read_h5ad(train_path)\nadata_test = ad.read_h5ad(test_path)\n\n# Extract features (X) and labels (y)\nX = adata_train.X\ny = adata_train.obs['cell_type']\nX_test = adata_test.X\n\n# 2. Local Validation Split\n# We split the training data to calculate a local Cohen's Kappa score\nX_train_split, X_val_split, y_train_split, y_val_split = train_test_split(\n    X, y, test_size=0.2, random_state=42, stratify=y\n)\n\n# 3. Dimensionality Reduction (Truncated SVD)\n# We reduce the massive number of peaks down to 100 dense components\nprint(\"Performing Dimensionality Reduction...\")\nsvd = TruncatedSVD(n_components=100, random_state=42)\n\n# Fit SVD on the training split and transform both train and val\nX_train_svd = svd.fit_transform(X_train_split)\nX_val_svd = svd.transform(X_val_split)\n\n# 4. Train and Validate Baseline Model\nprint(\"Training Logistic Regression model...\")\nmodel = LogisticRegression(max_iter=1000, random_state=42, class_weight='balanced', n_jobs=-1)\nmodel.fit(X_train_svd, y_train_split)\n\n# Validate locally\nval_preds = model.predict(X_val_svd)\nkappa = cohen_kappa_score(y_val_split, val_preds)\nprint(f\"Local Validation Cohen's Kappa Score: {kappa:.4f}\")\n\n# 5. Train on Full Data and Predict for Submission\nprint(\"\\nRetraining on full training data for final submission...\")\n# Refit SVD on the entire training set\nX_full_svd = svd.fit_transform(X)\nX_test_svd = svd.transform(X_test)\n\nfinal_model = LogisticRegression(max_iter=1000, random_state=42, class_weight='balanced', n_jobs=-1)\nfinal_model.fit(X_full_svd, y)\n\nprint(\"Predicting test data...\")\ntest_preds = final_model.predict(X_test_svd)\n\n# 6. Create Submission File\nsubmission = pd.DataFrame({\n    'ID': adata_test.obs_names,\n    'TARGET': test_preds\n})\n\n# Save to Kaggle working directory\nsubmission.to_csv('submission.csv', index=False)\nprint(\"Submission successfully saved to 'submission.csv'\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-26T06:03:22.161836Z","iopub.execute_input":"2026-02-26T06:03:22.162517Z","iopub.status.idle":"2026-02-26T06:04:06.282239Z","shell.execute_reply.started":"2026-02-26T06:03:22.162486Z","shell.execute_reply":"2026-02-26T06:04:06.281500Z"}},"outputs":[],"execution_count":null}]}