{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":77585,"databundleVersionId":8490368,"sourceType":"competition"}],"dockerImageVersionId":30627,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Benchmarking GBDT for tabular data\n\nTabular data benchmark: ensemble of 5 un-tuned catboost.\nCatboost has been choosen for: best performance out of the box and easy handling of categorical data.","metadata":{}},{"cell_type":"code","source":"import numpy as np, pandas as pd\nimport os\nimport json\nfrom catboost import CatBoostRegressor, CatBoostClassifier\nfrom sklearn.metrics import log_loss, mean_squared_error","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-01-24T11:02:08.358149Z","iopub.execute_input":"2024-01-24T11:02:08.358462Z","iopub.status.idle":"2024-01-24T11:02:11.607695Z","shell.execute_reply.started":"2024-01-24T11:02:08.358433Z","shell.execute_reply":"2024-01-24T11:02:11.606684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '/kaggle/input/bench-tab-v2/'\n\ntasks = json.load(open(f'{path}train/dict_task.json'))\nsuits = json.load(open(f'{path}train/dict_suit.json'))\n\nDEBUG = False\nsuits = {'334': [361110, 361111], '335': [361093]} if DEBUG else suits\nn_seeds = 2 if DEBUG else 5\n\ndef prepare_sub(data, identifier):\n    data = pd.DataFrame(data)\n    data.columns = ['target']\n    data['ID'] =  identifier + '_' + data.index.astype(str)\n    columns = ['ID','target']\n    return data[columns]\n\nres_gbdt = {}\nsub = []\n\nparams = {}\n    #'iterations':50\n\nfor t in suits:\n    print(t)\n    for suit in suits[t]:\n        \n        identifier = f'{t}_{suit}'\n        task_type = tasks[t]\n        var_type = task_type['features']\n        target_type = task_type['target']\n\n        X = pd.read_parquet(f'{path}train/{identifier}/data_train.parquet')\n        X_test = pd.read_parquet(f'{path}train/{identifier}/data_test.parquet')\n        y = pd.read_parquet(f'{path}train/{identifier}/target_train.parquet')\n        \n        names = json.load(open(f'{path}train/{identifier}/attribute_names.json'))\n        cat_indicator = json.load(open(f'{path}train/{identifier}/categorical_indicator.json'))\n        \n        preds = []\n        \n        for seed in range(n_seeds):\n        \n            gbdt_model = CatBoostRegressor(random_seed=seed, **params, task_type='GPU') if target_type=='num' else CatBoostClassifier(random_seed=seed, **params, task_type='GPU')  \n            gbdt_model = gbdt_model.fit(X, y.values, cat_features=X.columns[cat_indicator].values, silent = True)\n            \n            if target_type=='num':\n                preds.append(gbdt_model.predict(X_test))\n            else:\n                preds.append(gbdt_model.predict_proba(X_test)[:,1])\n            \n        preds = np.mean(np.array(preds),axis=0)\n\n        preds = pd.DataFrame(preds)\n        preds.index = X_test.index\n        \n        # appends empty tests / solutions\n        sub.append(prepare_sub(preds, identifier))\n        \n        print('')\n        print(f'{identifier=} {var_type=} {target_type=}')","metadata":{"execution":{"iopub.status.busy":"2024-01-24T11:02:11.609652Z","iopub.execute_input":"2024-01-24T11:02:11.610106Z","iopub.status.idle":"2024-01-24T11:02:18.821568Z","shell.execute_reply.started":"2024-01-24T11:02:11.61007Z","shell.execute_reply":"2024-01-24T11:02:18.820621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.concat(sub).reset_index(drop=True)\ndisplay(sub.head())\nsub.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-01-24T11:02:18.823021Z","iopub.execute_input":"2024-01-24T11:02:18.823579Z","iopub.status.idle":"2024-01-24T11:02:18.992996Z","shell.execute_reply.started":"2024-01-24T11:02:18.823542Z","shell.execute_reply":"2024-01-24T11:02:18.991977Z"},"trusted":true},"execution_count":null,"outputs":[]}]}