{"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":"none","dataSources":[{"sourceId":67223,"databundleVersionId":7463838,"sourceType":"competition"}],"dockerImageVersionId":30635,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install HROCH","metadata":{"execution":{"iopub.status.busy":"2024-01-20T19:54:05.268014Z","iopub.execute_input":"2024-01-20T19:54:05.268442Z","iopub.status.idle":"2024-01-20T19:54:19.611297Z","shell.execute_reply.started":"2024-01-20T19:54:05.268409Z","shell.execute_reply":"2024-01-20T19:54:19.609485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np, pandas as pd, sympy as sp\nimport os, json, warnings, time\n\nwarnings.filterwarnings(\"ignore\")\n\nfrom sklearn.metrics import log_loss, mean_squared_error\nfrom sklearn.preprocessing import OneHotEncoder\nfrom HROCH import SymbolicRegressor, NonlinearLogisticRegressor ","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-01-20T19:54:19.615312Z","iopub.execute_input":"2024-01-20T19:54:19.616161Z","iopub.status.idle":"2024-01-20T19:54:19.624450Z","shell.execute_reply.started":"2024-01-20T19:54:19.616109Z","shell.execute_reply":"2024-01-20T19:54:19.623317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '/kaggle/input/bench-tab-v1/'\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\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\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        idx_test = X_test.index\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        if var_type == 'num+cat':\n            enc = OneHotEncoder(handle_unknown='ignore')\n            X_cat = enc.fit_transform(X.loc[:,cat_indicator].copy())\n            X_test_cat = enc.transform(X_test.loc[:,cat_indicator].copy())            \n            X = pd.concat([X.loc[:,[not c for c in cat_indicator]].reset_index(drop=True).copy(), pd.DataFrame(X_cat.toarray())],axis=1)\n            X_test = pd.concat([X_test.loc[:,[not c for c in cat_indicator]].reset_index(drop=True).copy(), pd.DataFrame(X_test_cat.toarray())],axis=1)\n            X.columns = X.columns.astype(str)\n            X_test.columns = X_test.columns.astype(str)\n        \n        print(X.shape, X_test.shape)\n        \n        params={'num_threads':4,'time_limit':0, 'iter_limit':100000, 'random_state':42}\n        base_model = SymbolicRegressor(**params) if target_type=='num' else NonlinearLogisticRegressor(**params)\n        start = time.time()\n        base_model = base_model.fit(X, y)\n        fit_time = time.time() - start\n        \n        if target_type=='num':\n            preds = base_model.predict(X_test)\n        else:\n            preds = base_model.predict_proba(X_test)[:,1]\n        \n        preds = pd.DataFrame(preds)\n        preds.index = idx_test\n        \n        # appends empty tests / solutions\n        sub.append(prepare_sub(preds, identifier))\n        \n        print('')\n        print(f'{identifier=} {var_type=} {target_type=}')\n        eq = sp.parse_expr(base_model.sexpr_)\n        print('time: ', fit_time)\n        print('equation: ', eq)","metadata":{"execution":{"iopub.status.busy":"2024-01-20T19:54:19.626023Z","iopub.execute_input":"2024-01-20T19:54:19.627185Z","iopub.status.idle":"2024-01-20T20:05:34.361263Z","shell.execute_reply.started":"2024-01-20T19:54:19.627147Z","shell.execute_reply":"2024-01-20T20:05:34.359633Z"},"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-20T20:05:34.362654Z","iopub.status.idle":"2024-01-20T20:05:34.363329Z","shell.execute_reply.started":"2024-01-20T20:05:34.363092Z","shell.execute_reply":"2024-01-20T20:05:34.363119Z"},"trusted":true},"execution_count":null,"outputs":[]}]}