{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":94147,"databundleVersionId":11390004,"sourceType":"competition"}],"dockerImageVersionId":31012,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Libraries & Config","metadata":{}},{"cell_type":"code","source":"!pip install -qq scikit-learn==1.6.1","metadata":{"trusted":true,"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2025-05-07T22:16:36.903484Z","iopub.execute_input":"2025-05-07T22:16:36.904369Z","iopub.status.idle":"2025-05-07T22:16:45.626334Z","shell.execute_reply.started":"2025-05-07T22:16:36.904334Z","shell.execute_reply":"2025-05-07T22:16:45.625496Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#\n# Libraries\n#\n\n# General\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os, string, re, random, gc, pickle, math,warnings\nimport json\nfrom itertools import *\nfrom datetime import date\nfrom tqdm.keras import TqdmCallback\nfrom tqdm import tqdm\nimport h5py\nfrom catboost import CatBoostRegressor\nimport optuna\n\n# Sklearn\nfrom sklearn.model_selection import *\nfrom sklearn.feature_extraction import *\nfrom sklearn.metrics import *\nfrom sklearn.metrics import pairwise\nfrom sklearn.preprocessing import *\nfrom sklearn.utils import *\nfrom sklearn.pipeline import *\nfrom sklearn.compose import *\n\n# Stats\nimport scipy\nfrom scipy.stats import *\nfrom scipy.sparse import csr_matrix\n\n# Setting\npd.set_option('max_colwidth',None)\nseed = 505\nwarnings.simplefilter('ignore')\n\ndata_path = None\n\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        if filename.endswith('h5'):\n            data_path = os.path.join(dirname, filename)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-05-07T22:16:45.627857Z","iopub.execute_input":"2025-05-07T22:16:45.628282Z","iopub.status.idle":"2025-05-07T22:17:02.764708Z","shell.execute_reply.started":"2025-05-07T22:16:45.628258Z","shell.execute_reply":"2025-05-07T22:17:02.764053Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Data","metadata":{}},{"cell_type":"code","source":"#\n# Load data\n#\n\n# train data\nwith h5py.File(data_path,'r') as f:\n    train_spots = f['spots/Train']\n    train_spot_tables = {slide_name: pd.DataFrame(np.array(train_spots[slide_name])) for slide_name in train_spots.keys()}\n\ntrain_ds = pd.concat(train_spot_tables.values(), ignore_index=True)\n\n# test data\nwith h5py.File(data_path,'r') as f:\n    test_spots = f['spots/Test']\n    test_spot_tables = {slide_name: pd.DataFrame(np.array(test_spots[slide_name])) for slide_name in test_spots.keys()}\n\ntest_ds = pd.concat(test_spot_tables.values(), ignore_index=True)\n\n\n# view\ntrain_ds.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-07T22:17:02.765624Z","iopub.execute_input":"2025-05-07T22:17:02.766176Z","iopub.status.idle":"2025-05-07T22:17:02.907357Z","shell.execute_reply.started":"2025-05-07T22:17:02.766148Z","shell.execute_reply":"2025-05-07T22:17:02.906530Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Preprocessing","metadata":{}},{"cell_type":"code","source":"#\n# Preprocessing\n#\n\n# feature engineering\nx = train_ds[['x', 'y']]  # Use spatial coordinates\ny = train_ds.iloc[:, 2:]  # All other columns are cell-type labels\n\n# train-test split\nx_train, x_valid, y_train, y_valid = train_test_split(x, y, test_size=0.02, random_state=seed)\n\n# scale \nscaler = MinMaxScaler()\nscaled_val = scaler.fit_transform(x_train)\nx_train_sc = pd.DataFrame(scaled_val,columns=x_train.columns)\n\nscaled_val = scaler.fit_transform(x_valid)\nx_valid_sc = pd.DataFrame(scaled_val,columns=x_valid.columns)\n\n# shape info\nprint(f\"  Training shape: {x_train_sc.shape} | Validation shape: {x_valid.shape}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-07T22:17:02.909184Z","iopub.execute_input":"2025-05-07T22:17:02.909806Z","iopub.status.idle":"2025-05-07T22:17:02.943412Z","shell.execute_reply.started":"2025-05-07T22:17:02.909774Z","shell.execute_reply":"2025-05-07T22:17:02.942519Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Model Training, Prediction & Evaluation","metadata":{}},{"cell_type":"code","source":"#\n# Training, Prediction & Evaluation - Baseline\n#\ncatbr = CatBoostRegressor(verbose=False,objective='MultiRMSE')\ncatbr.fit(x_train_sc, y_train,early_stopping_rounds=100)\n\n# prediction\npred = catbr.predict(x_valid)\n\n# rmse\nrmse = root_mean_squared_error(y_valid,pred)\n\nprint('RMSE: {:.5}'.format(rmse))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-07T22:17:02.944252Z","iopub.execute_input":"2025-05-07T22:17:02.944548Z","iopub.status.idle":"2025-05-07T22:17:22.567864Z","shell.execute_reply.started":"2025-05-07T22:17:02.944522Z","shell.execute_reply":"2025-05-07T22:17:22.567178Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Optimisation","metadata":{}},{"cell_type":"code","source":"#\n# Optuna \n#\n\ndef objective(trial):\n    param = {}\n    param['learning_rate'] = trial.suggest_discrete_uniform(\"learning_rate\", 0.01, 0.03, 0.01)\n    # param['iterations'] = trial.suggest_int('iterations', 1000, 2000,1000)\n    param['depth'] = trial.suggest_int('depth', 3, 5)\n    param['l2_leaf_reg'] = trial.suggest_discrete_uniform('l2_leaf_reg', 1.0, 5.5, 0.5)\n    param['min_child_samples'] = trial.suggest_categorical('min_child_samples', [1, 4, 8, 16, 32])\n    param['grow_policy'] = 'Depthwise'\n    param['iterations'] = 1000\n    param['objective'] = 'MultiRMSE'\n    # param['od_type'] = 'iter'\n    param['od_wait'] = 20\n    param['random_state'] = seed\n    param['logging_level'] = 'Silent'\n    #param['task_type'] ='GPU'\n    \n    regressor = CatBoostRegressor(**param)\n    regressor.fit(x_train_sc, y_train,early_stopping_rounds=100)\n    #loss = mean_squared_error(y_valid, regressor.predict(x_valid_sc))\n    rmse = root_mean_squared_error(y_valid, regressor.predict(x_valid_sc))\n    \n    return rmse\n\nstudy = optuna.create_study(study_name=f'catboost-seed{seed}')\nstudy.optimize(objective, n_trials=20, n_jobs=-1, timeout=24000)\n\ntrial = study.best_trial\n\nprint(\"  Params: \")\nfor key, value in trial.params.items():\n    print(\"    {}: {}\".format(key, value))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-07T22:17:22.568803Z","iopub.execute_input":"2025-05-07T22:17:22.569188Z","iopub.status.idle":"2025-05-07T22:20:51.799668Z","shell.execute_reply.started":"2025-05-07T22:17:22.569160Z","shell.execute_reply":"2025-05-07T22:20:51.798649Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Prediction & Submission","metadata":{}},{"cell_type":"code","source":"# \n# Prediction with best params\n#\n\n# best params & adding new items\nbest_param = study.best_params\nbest_param['logging_level'] = 'Silent'\nbest_param['objective'] = 'MultiRMSE'\n\n# test data\nx_tst = test_ds[['x', 'y']]  \n\n# scale \nscaler = MinMaxScaler()\nscaled_val = scaler.fit_transform(x_tst)\nx_tst_sc = pd.DataFrame(scaled_val,columns=x_tst.columns)\n\n# training\ncatbr_bst = CatBoostRegressor(**best_param)\ncatbr_bst.fit(x_train_sc, y_train,early_stopping_rounds=100)\n\n# prediction\npred_tst = catbr_bst.predict(x_tst_sc)\n\n# prepare submission file\nsub_df = pd.DataFrame(pred_tst, columns=y.columns)\nsub_df.insert(0, 'ID', test_ds.index)\nsub_df.to_csv(\"/kaggle/working/submission.csv\", index=False)\nprint(\"Submission file 'submission.csv' created!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-07T22:20:51.800538Z","iopub.execute_input":"2025-05-07T22:20:51.800835Z","iopub.status.idle":"2025-05-07T22:21:05.603441Z","shell.execute_reply.started":"2025-05-07T22:20:51.800807Z","shell.execute_reply":"2025-05-07T22:21:05.602637Z"}},"outputs":[],"execution_count":null}]}