{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":81933,"databundleVersionId":9643020,"sourceType":"competition"},{"sourceId":165404,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":140738,"modelId":163343}],"dockerImageVersionId":30804,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Initial","metadata":{}},{"cell_type":"code","source":"import re\nfrom sklearn.base import clone\nfrom sklearn.metrics import cohen_kappa_score\nfrom sklearn.model_selection import StratifiedKFold\nfrom scipy.optimize import minimize\nfrom concurrent.futures import ThreadPoolExecutor\nfrom tqdm import tqdm\nimport polars as pl\nimport polars.selectors as cs\nimport matplotlib.pyplot as plt\nfrom matplotlib.ticker import MaxNLocator, FormatStrFormatter, PercentFormatter\nimport seaborn as sns\nimport os\nfrom sklearn.preprocessing import StandardScaler\nimport matplotlib.pyplot as plt\nfrom keras.models import Model\nfrom keras.layers import Input, Dense\nfrom keras.optimizers import Adam\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nimport pandas as pd\nfrom colorama import Fore, Style\nfrom IPython.display import clear_output\nimport warnings\nfrom lightgbm import LGBMRegressor\nfrom xgboost import XGBRegressor\nfrom catboost import CatBoostRegressor\nfrom sklearn.ensemble import VotingRegressor, RandomForestRegressor, GradientBoostingRegressor\nfrom sklearn.impute import SimpleImputer, KNNImputer\nfrom sklearn.pipeline import Pipeline","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T16:36:08.450507Z","iopub.execute_input":"2024-12-19T16:36:08.450899Z","iopub.status.idle":"2024-12-19T16:36:29.039596Z","shell.execute_reply.started":"2024-12-19T16:36:08.450854Z","shell.execute_reply":"2024-12-19T16:36:29.038487Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip -q install /kaggle/input/tabnet/pytorch/v1/1/pytorch_tabnet-4.1.0-py3-none-any.whl","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T16:36:29.041325Z","iopub.execute_input":"2024-12-19T16:36:29.041998Z","iopub.status.idle":"2024-12-19T16:37:13.044961Z","shell.execute_reply.started":"2024-12-19T16:36:29.041963Z","shell.execute_reply":"2024-12-19T16:37:13.043211Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Process Data","metadata":{}},{"cell_type":"code","source":"from concurrent.futures import ThreadPoolExecutor\ndef process_file(filename, dirname):\n    df = pd.read_parquet(os.path.join(dirname, filename, 'part-0.parquet'))\n    df.drop('step', axis=1, inplace=True)\n    return df.describe().values.reshape(-1), filename.split('=')[1]\n\ndef load_data_parquet(dirname) -> pd.DataFrame:\n    ids = os.listdir(dirname)\n    with ThreadPoolExecutor() as executor:\n        results = list(tqdm(executor.map(lambda fname: process_file(fname, dirname), ids), total=len(ids)))\n    stats, indexes = zip(*results)\n    df = pd.DataFrame(stats, columns=[f\"stat_{i}\" for i in range(len(stats[0]))])\n    df['id'] = indexes\n    return df\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T16:37:13.047446Z","iopub.execute_input":"2024-12-19T16:37:13.047986Z","iopub.status.idle":"2024-12-19T16:37:13.059977Z","shell.execute_reply.started":"2024-12-19T16:37:13.047934Z","shell.execute_reply":"2024-12-19T16:37:13.058355Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import OneHotEncoder\ntrain = pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/train.csv')\ntest = pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/test.csv')\nsample = pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/sample_submission.csv')\n\nfeaturesCols = ['Basic_Demos-Enroll_Season', 'Basic_Demos-Age', 'Basic_Demos-Sex',\n                'CGAS-Season', 'CGAS-CGAS_Score', 'Physical-Season', 'Physical-BMI',\n                'Physical-Height', 'Physical-Weight', 'Physical-Waist_Circumference',\n                'Physical-Diastolic_BP', 'Physical-HeartRate', 'Physical-Systolic_BP',\n                'Fitness_Endurance-Season', 'Fitness_Endurance-Max_Stage',\n                'Fitness_Endurance-Time_Mins', 'Fitness_Endurance-Time_Sec',\n                'FGC-Season', 'FGC-FGC_CU', 'FGC-FGC_CU_Zone', 'FGC-FGC_GSND',\n                'FGC-FGC_GSND_Zone', 'FGC-FGC_GSD', 'FGC-FGC_GSD_Zone', 'FGC-FGC_PU',\n                'FGC-FGC_PU_Zone', 'FGC-FGC_SRL', 'FGC-FGC_SRL_Zone', 'FGC-FGC_SRR',\n                'FGC-FGC_SRR_Zone', 'FGC-FGC_TL', 'FGC-FGC_TL_Zone', 'BIA-Season',\n                'BIA-BIA_Activity_Level_num', 'BIA-BIA_BMC', 'BIA-BIA_BMI',\n                'BIA-BIA_BMR', 'BIA-BIA_DEE', 'BIA-BIA_ECW', 'BIA-BIA_FFM',\n                'BIA-BIA_FFMI', 'BIA-BIA_FMI', 'BIA-BIA_Fat', 'BIA-BIA_Frame_num',\n                'BIA-BIA_ICW', 'BIA-BIA_LDM', 'BIA-BIA_LST', 'BIA-BIA_SMM',\n                'BIA-BIA_TBW', 'PAQ_A-Season', 'PAQ_A-PAQ_A_Total', 'PAQ_C-Season',\n                'PAQ_C-PAQ_C_Total', 'SDS-Season', 'SDS-SDS_Total_Raw',\n                'SDS-SDS_Total_T', 'PreInt_EduHx-Season',\n                'PreInt_EduHx-computerinternet_hoursday', 'sii']\n\ncat_c = ['Basic_Demos-Enroll_Season', 'CGAS-Season', 'Physical-Season', \n          'Fitness_Endurance-Season', 'FGC-Season', 'BIA-Season', \n          'PAQ_A-Season', 'PAQ_C-Season', 'SDS-Season', 'PreInt_EduHx-Season']\n\ntrain_ts = load_data_parquet(\"/kaggle/input/child-mind-institute-problematic-internet-use/series_train.parquet\")\ntest_ts = load_data_parquet(\"/kaggle/input/child-mind-institute-problematic-internet-use/series_test.parquet\")\n\ntime_series_cols = train_ts.columns.tolist()\ntime_series_cols.remove(\"id\")\n\ntrain = pd.merge(train, train_ts, how=\"left\", on='id')\ntest = pd.merge(test, test_ts, how=\"left\", on='id')\n\ntrain = train.drop('id', axis=1)\ntest = test.drop('id', axis=1)\nfeaturesCols += time_series_cols\n\ntrain = train[featuresCols]\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T16:37:13.063261Z","iopub.execute_input":"2024-12-19T16:37:13.063835Z","iopub.status.idle":"2024-12-19T16:38:47.387691Z","shell.execute_reply.started":"2024-12-19T16:37:13.063778Z","shell.execute_reply":"2024-12-19T16:38:47.386160Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def fill_nan_values(data):\n    imputer = KNNImputer(n_neighbors=71)\n    numeric_cols = data.select_dtypes(include=['float64', 'int64']).columns\n    imputed_data = imputer.fit_transform(data[numeric_cols])\n    data_imputed = pd.DataFrame(imputed_data, columns=numeric_cols)\n    if 'sii' in data.columns:\n        data_imputed['sii'] = data_imputed['sii'].round().astype(int)\n    for col in data.columns:\n        if col not in numeric_cols:\n            data_imputed[col] = data[col]\n    return data_imputed\n\ndef handle_category_data(data):\n    # Define the fixed set of seasons\n    all_categories = ['Spring', 'Summer', 'Fall', 'Winter']\n\n    df_encoded = data.copy()\n    encoder = OneHotEncoder(categories=[all_categories],  # Đảm bảo 4 chiều\n                             sparse_output=False,         # Đầu ra dạng mảng dày\n                             handle_unknown='ignore',     # Bỏ qua giá trị ngoài danh mục\n                             dtype=int)                   # Đầu ra kiểu int\n\n    for column in data.columns:\n        if df_encoded[column].dtype == 'object' or df_encoded[column].dtype.name == 'category':\n            # Fit and transform the column\n            encoded_array = encoder.fit_transform(df_encoded[[column]])\n            \n            # Generate column names\n            encoded_columns = [f\"{column}_{season}\" for season in all_categories]\n            \n            # Add the encoded data to the DataFrame\n            encoded_df = pd.DataFrame(encoded_array, columns=encoded_columns, index=data.index)\n            df_encoded = pd.concat([df_encoded.drop(column, axis=1), encoded_df], axis=1)\n    \n    return df_encoded\ntrain = fill_nan_values(train)\ntrain = handle_category_data(train)\ntest = fill_nan_values(test)\ntest = handle_category_data(test)\ntrain_2 = train\ntest_2 = test\n\ntrain_3 = train\ntest_3 = test","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T16:38:47.389133Z","iopub.execute_input":"2024-12-19T16:38:47.389512Z","iopub.status.idle":"2024-12-19T16:39:01.863430Z","shell.execute_reply.started":"2024-12-19T16:38:47.389475Z","shell.execute_reply":"2024-12-19T16:39:01.862144Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Model S3 (Using normally with Voting Regressor)","metadata":{}},{"cell_type":"code","source":"train\ntrain = train.dropna(subset='sii')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T16:39:01.864993Z","iopub.execute_input":"2024-12-19T16:39:01.865438Z","iopub.status.idle":"2024-12-19T16:39:01.876135Z","shell.execute_reply.started":"2024-12-19T16:39:01.865392Z","shell.execute_reply":"2024-12-19T16:39:01.874687Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nif np.any(np.isinf(train)):\n    train = train.replace([np.inf, -np.inf], np.nan)\ndef quadratic_weighted_kappa(y_true, y_pred):\n    return cohen_kappa_score(y_true, y_pred, weights='quadratic')\n\ndef threshold_Rounder(oof_non_rounded, thresholds):\n    return np.where(oof_non_rounded < thresholds[0], 0,\n                    np.where(oof_non_rounded < thresholds[1], 1,\n                             np.where(oof_non_rounded < thresholds[2], 2, 3)))\n\ndef evaluate_predictions(thresholds, y_true, oof_non_rounded):\n    rounded_p = threshold_Rounder(oof_non_rounded, thresholds)\n    return -quadratic_weighted_kappa(y_true, rounded_p)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T16:39:01.877504Z","iopub.execute_input":"2024-12-19T16:39:01.877814Z","iopub.status.idle":"2024-12-19T16:39:01.903985Z","shell.execute_reply.started":"2024-12-19T16:39:01.877784Z","shell.execute_reply":"2024-12-19T16:39:01.902999Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\ndef TrainML(model_class, test_data):\n    X = train.drop(['sii'], axis=1)\n    y = train['sii']\n\n    SKF = StratifiedKFold(n_splits=n_splits, shuffle=True, random_state=SEED)\n    \n    train_S = []\n    test_S = []\n    \n    oof_non_rounded = np.zeros(len(y), dtype=float) \n    oof_rounded = np.zeros(len(y), dtype=int) \n    test_preds = np.zeros((len(test_data), n_splits))\n\n    for fold, (train_idx, test_idx) in enumerate(tqdm(SKF.split(X, y), desc=\"Training Folds\", total=n_splits)):\n        X_train, X_val = X.iloc[train_idx], X.iloc[test_idx]\n        y_train, y_val = y.iloc[train_idx], y.iloc[test_idx]\n\n        model = clone(model_class)\n        model.fit(X_train, y_train)\n\n        y_train_pred = model.predict(X_train)\n        y_val_pred = model.predict(X_val)\n\n        oof_non_rounded[test_idx] = y_val_pred\n        y_val_pred_rounded = y_val_pred.round(0).astype(int)\n        oof_rounded[test_idx] = y_val_pred_rounded\n\n        train_kappa = quadratic_weighted_kappa(y_train, y_train_pred.round(0).astype(int))\n        val_kappa = quadratic_weighted_kappa(y_val, y_val_pred_rounded)\n\n        train_S.append(train_kappa)\n        test_S.append(val_kappa)\n        \n        test_preds[:, fold] = model.predict(test_data)\n        \n        print(f\"Fold {fold+1} - Train QWK: {train_kappa:.4f}, Validation QWK: {val_kappa:.4f}\")\n        clear_output(wait=True)\n\n    print(f\"Mean Train QWK --> {np.mean(train_S):.4f}\")\n    print(f\"Mean Validation QWK ---> {np.mean(test_S):.4f}\")\n\n    KappaOPtimizer = minimize(evaluate_predictions,\n                              x0=[0.5, 1.5, 2.5], args=(y, oof_non_rounded), \n                              method='Nelder-Mead')\n    assert KappaOPtimizer.success, \"Optimization did not converge.\"\n    \n    oof_tuned = threshold_Rounder(oof_non_rounded, KappaOPtimizer.x)\n    tKappa = quadratic_weighted_kappa(y, oof_tuned)\n\n    print(f\"----> || Optimized QWK SCORE :: {Fore.CYAN}{Style.BRIGHT} {tKappa:.3f}{Style.RESET_ALL}\")\n\n    tpm = test_preds.mean(axis=1)\n    tpTuned = threshold_Rounder(tpm, KappaOPtimizer.x)\n    \n    submission = pd.DataFrame({\n        'id': sample['id'],\n        'sii': tpTuned\n    })\n\n    return submission","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T16:39:01.905858Z","iopub.execute_input":"2024-12-19T16:39:01.906543Z","iopub.status.idle":"2024-12-19T16:39:01.927893Z","shell.execute_reply.started":"2024-12-19T16:39:01.906499Z","shell.execute_reply":"2024-12-19T16:39:01.926568Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"SEED = 2004\nn_splits = 5\n# Model parameters for LightGBM\nParams = {\n    'learning_rate': 0.09970294901245966, \n    'max_depth': 3, \n    'subsample': 0.9651688449975022, \n    'colsample_bytree': 0.616732288405486, \n    'num_leaves': 34, \n    'min_data_in_leaf': 68, \n    'feature_fraction': 0.6476169754611282, \n    'bagging_fraction': 0.9184091064527949, \n    'bagging_freq': 10, \n    'reg_alpha': 0.015879148435808108, \n    'reg_lambda': 0.0036854044260839643\n}\n\n\n# XGBoost parameters\nXGB_Params = {\n    'learning_rate': 0.29682190417298865,\n    'max_depth': 4, 'n_estimators': 796,\n    'subsample': 0.7542484622989069,\n    'colsample_bytree': 0.886399359731497,\n    'reg_alpha': 0.014681067600657996,\n    'reg_lambda': 9.209859894025579,\n    'gamma': 0.06495942878096272, \n    'min_child_weight': 13,\n    'use_gpu': True\n}\n\nCatBoost_Params = {\n    'learning_rate': 0.026392650714515364, \n    'depth': 15, 'l2_leaf_reg': 0.0018692968691208557,\n    'iterations': 637, 'bagging_temperature': 0.45636037003578794,\n    'random_strength': 7.2357605130667455, 'border_count': 135, \n    'grow_policy': 'Lossguide'\n}\nLight = LGBMRegressor(**Params, random_state=SEED, verbose=-1, n_estimators=300)\nXGB_Model = XGBRegressor(**XGB_Params)\nCatBoost_Model = CatBoostRegressor(**CatBoost_Params)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T16:39:01.929564Z","iopub.execute_input":"2024-12-19T16:39:01.930038Z","iopub.status.idle":"2024-12-19T16:39:01.954405Z","shell.execute_reply.started":"2024-12-19T16:39:01.930000Z","shell.execute_reply":"2024-12-19T16:39:01.953080Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ensemble = VotingRegressor(estimators=[\n    ('lgb', Pipeline(steps=[ ('regressor', LGBMRegressor(random_state=SEED))])),\n    ('xgb', Pipeline(steps=[ ('regressor', XGBRegressor(random_state=SEED))])),\n    ('cat', Pipeline(steps=[ ('regressor', CatBoostRegressor(random_state=SEED, silent=True))])),\n    ('rf', Pipeline(steps=[('regressor', RandomForestRegressor(random_state=SEED))])),\n    ('gb', Pipeline(steps=[\n    ('regressor', GradientBoostingRegressor(random_state=SEED))\n]))\n])\n\nSubmission3 = TrainML(ensemble, test)\n# Submission3 = pd.DataFrame({\n#     'id': sample['id'],\n#     'sii': Submission3\n# })\nSubmission3","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T16:39:01.958007Z","iopub.execute_input":"2024-12-19T16:39:01.958483Z","iopub.status.idle":"2024-12-19T16:42:21.037144Z","shell.execute_reply.started":"2024-12-19T16:39:01.958429Z","shell.execute_reply":"2024-12-19T16:42:21.035481Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Model 2 ( Using Slack to combine the simple model which have small score like SVM, RandomForest, DescisionTree)","metadata":{}},{"cell_type":"code","source":"# New: TabNet\nfrom pytorch_tabnet.tab_model import TabNetRegressor\nfrom sklearn.base import BaseEstimator, RegressorMixin\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.model_selection import train_test_split\nfrom pytorch_tabnet.callbacks import Callback\nimport os\nimport torch\nfrom pytorch_tabnet.callbacks import Callback\n\nclass TabNetWrapper(BaseEstimator, RegressorMixin):\n    def __init__(self, **kwargs):\n        self.model = TabNetRegressor(**kwargs)\n        self.kwargs = kwargs\n        self.imputer = SimpleImputer(strategy='median')\n        self.best_model_path = 'best_tabnet_model.pt'\n        \n    def fit(self, X, y):\n        # Handle missing values\n        X_imputed = self.imputer.fit_transform(X)\n        \n        if hasattr(y, 'values'):\n            y = y.values\n            \n        # Create internal validation set\n        X_train, X_valid, y_train, y_valid = train_test_split(\n            X_imputed, \n            y, \n            test_size=0.2,\n            random_state=42\n        )\n        \n        # Train TabNet model\n        history = self.model.fit(\n            X_train=X_train,\n            y_train=y_train.reshape(-1, 1),\n            eval_set=[(X_valid, y_valid.reshape(-1, 1))],\n            eval_name=['valid'],\n            eval_metric=['mse'],\n            max_epochs=200,\n            patience=20,\n            batch_size=1024,\n            virtual_batch_size=128,\n            num_workers=0,\n            drop_last=False,\n            callbacks=[\n                TabNetPretrainedModelCheckpoint(\n                    filepath=self.best_model_path,\n                    monitor='valid_mse',\n                    mode='min',\n                    save_best_only=True,\n                    verbose=True\n                )\n            ]\n        )\n        \n        # Load the best model\n        if os.path.exists(self.best_model_path):\n            self.model.load_model(self.best_model_path)\n            os.remove(self.best_model_path)  # Remove temporary file\n        \n        return self\n    \n    def predict(self, X):\n        X_imputed = self.imputer.transform(X)\n        return self.model.predict(X_imputed).flatten()\n    \n    def __deepcopy__(self, memo):\n        # Add deepcopy support for scikit-learn\n        cls = self.__class__\n        result = cls.__new__(cls)\n        memo[id(self)] = result\n        for k, v in self.__dict__.items():\n            setattr(result, k, deepcopy(v, memo))\n        return result\n\n# TabNet hyperparameters\nTabNet_Params = {\n    'n_d': 47,              \n    'n_a': 56,              \n    'n_steps': 5,           \n    'gamma': 1.5,           \n    'n_independent': 2,     \n    'n_shared': 2,          \n    'lambda_sparse': 1e-4, \n    'optimizer_fn': torch.optim.Adam,\n    'optimizer_params': dict(lr=2e-2, weight_decay=1e-5),\n    'mask_type': 'entmax',\n    'scheduler_params': dict(mode=\"min\", patience=10, min_lr=1e-5, factor=0.5),\n    'scheduler_fn': torch.optim.lr_scheduler.ReduceLROnPlateau,\n    'verbose': -1,\n    'device_name': 'cuda' if torch.cuda.is_available() else 'cpu'\n}\n\nclass TabNetPretrainedModelCheckpoint(Callback):\n    def __init__(self, filepath, monitor='val_loss', mode='min', \n                 save_best_only=True, verbose=1):\n        super().__init__()  # Initialize parent class\n        self.filepath = filepath\n        self.monitor = monitor\n        self.mode = mode\n        self.save_best_only = save_best_only\n        self.verbose = verbose\n        self.best = float('inf') if mode == 'min' else -float('inf')\n        \n    def on_train_begin(self, logs=None):\n        self.model = self.trainer  # Use trainer itself as model\n        \n    def on_epoch_end(self, epoch, logs=None):\n        logs = logs or {}\n        current = logs.get(self.monitor)\n        if current is None:\n            return\n        \n        # Check if current metric is better than best\n        if (self.mode == 'min' and current < self.best) or \\\n           (self.mode == 'max' and current > self.best):\n            if self.verbose:\n                print(f'\\nEpoch {epoch}: {self.monitor} improved from {self.best:.4f} to {current:.4f}')\n            self.best = current\n            if self.save_best_only:\n                self.model.save_model(self.filepath)  # Save the entire model","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T16:46:52.074233Z","iopub.execute_input":"2024-12-19T16:46:52.075505Z","iopub.status.idle":"2024-12-19T16:46:52.095837Z","shell.execute_reply.started":"2024-12-19T16:46:52.075457Z","shell.execute_reply":"2024-12-19T16:46:52.094179Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Light = LGBMRegressor(**Params, random_state=SEED, verbose=-1, n_estimators=300)\nXGB_Model = XGBRegressor(**XGB_Params)\nCatBoost_Model = CatBoostRegressor(**CatBoost_Params)\nTabNet_Model = TabNetWrapper(**TabNet_Params) \nvoting_model = VotingRegressor(estimators=[\n    ('lightgbm', Light),\n    ('xgboost', XGB_Model),\n    ('catboost', CatBoost_Model),\n    ('tabnet', TabNet_Model)\n])\n\nSubmission2 = TrainML(voting_model, test)\n\nSubmission2","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T16:46:56.117306Z","iopub.execute_input":"2024-12-19T16:46:56.117835Z","iopub.status.idle":"2024-12-19T16:50:51.035725Z","shell.execute_reply.started":"2024-12-19T16:46:56.117786Z","shell.execute_reply":"2024-12-19T16:50:51.034471Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Submission2","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T16:42:52.434922Z","iopub.status.idle":"2024-12-19T16:42:52.435673Z","shell.execute_reply.started":"2024-12-19T16:42:52.435308Z","shell.execute_reply":"2024-12-19T16:42:52.435341Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Model 3 (Idea about using AutoML to detect the best model, in this case, using H2O)","metadata":{}},{"cell_type":"code","source":"def TrainML(best_model, test_data, train_data):\n    X = train_data.drop(['sii'], axis=1)\n    y = train_data['sii']\n\n    SKF = StratifiedKFold(n_splits=n_splits, shuffle=True, random_state=SEED)\n    \n    oof_preds = np.zeros(len(y))\n    oof_rounded = np.zeros(len(y), dtype=int)\n    test_preds = np.zeros((len(test_data), n_splits))\n\n    train_scores, val_scores = [], []\n\n    for fold, (train_idx, val_idx) in enumerate(tqdm(SKF.split(X, y), desc=\"Training Folds\")):\n        # Split data\n        X_train, X_val = X.iloc[train_idx], X.iloc[val_idx]\n        y_train, y_val = y.iloc[train_idx], y.iloc[val_idx]\n\n        # Convert to H2OFrame and predict\n        train_preds = best_model.predict(h2o.H2OFrame(X_train)).as_data_frame().values.flatten()\n        val_preds = best_model.predict(h2o.H2OFrame(X_val)).as_data_frame().values.flatten()\n\n        # Save predictions\n        oof_preds[val_idx] = val_preds\n        oof_rounded[val_idx] = val_preds.round().astype(int)\n\n        # Calculate scores\n        train_scores.append(quadratic_weighted_kappa(y_train, train_preds.round().astype(int)))\n        val_scores.append(quadratic_weighted_kappa(y_val, val_preds.round().astype(int)))\n\n        # Predict on test data\n        test_preds[:, fold] = best_model.predict(h2o.H2OFrame(test_data)).as_data_frame().values.flatten()\n\n    print(f\"Mean Train QWK: {np.mean(train_scores):.4f}\")\n    print(f\"Mean Validation QWK: {np.mean(val_scores):.4f}\")\n\n    # Optimize thresholds\n    KappaOptimizer = minimize(evaluate_predictions, x0=[0.5, 1.5, 2.5], args=(y, oof_preds), method='Nelder-Mead')\n    assert KappaOptimizer.success, \"Threshold optimization failed\"\n\n    # Apply optimized thresholds\n    oof_tuned = threshold_Rounder(oof_preds, KappaOptimizer.x)\n    print(f\"Optimized QWK Score: {quadratic_weighted_kappa(y, oof_tuned):.3f}\")\n\n    # Test predictions with optimized thresholds\n    final_test_preds = threshold_Rounder(test_preds.mean(axis=1), KappaOptimizer.x)\n\n    submission = pd.DataFrame({\n        'id': sample['id'],\n        'sii': final_test_preds\n    })\n\n    return submission, KappaOptimizer\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T16:42:52.438416Z","iopub.status.idle":"2024-12-19T16:42:52.439052Z","shell.execute_reply.started":"2024-12-19T16:42:52.438749Z","shell.execute_reply":"2024-12-19T16:42:52.438777Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import h2o\nfrom h2o.automl import H2OAutoML\nh2o.init()\ntrain_data = h2o.H2OFrame(train)\n\naml = H2OAutoML(max_runtime_secs=540,seed=5)\naml.train(y='sii', training_frame=train_data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T16:42:52.441451Z","iopub.status.idle":"2024-12-19T16:42:52.442138Z","shell.execute_reply.started":"2024-12-19T16:42:52.441816Z","shell.execute_reply":"2024-12-19T16:42:52.441849Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"best_model = aml.leader\nSubmission1,KappaOPtimizer = TrainML(best_model,test,train)\nprint(KappaOPtimizer.x)\nprint(Submission1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T16:42:52.444122Z","iopub.status.idle":"2024-12-19T16:42:52.444666Z","shell.execute_reply.started":"2024-12-19T16:42:52.444447Z","shell.execute_reply":"2024-12-19T16:42:52.444471Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sub1 = Submission1\nsub2 = Submission2\nsub3 = Submission3\n\nsub1 = sub1.sort_values(by='id').reset_index(drop=True)\nsub2 = sub2.sort_values(by='id').reset_index(drop=True)\nsub3 = sub3.sort_values(by='id').reset_index(drop=True)\n\ncombined = pd.DataFrame({\n    'id': sub1['id'],\n    'sii_1': sub1['sii'],\n    'sii_2': sub2['sii'],\n    'sii_3': sub3['sii']\n})\n\ndef majority_vote(row):\n    return row.mode()[0]\n\ncombined['final_sii'] = combined[['sii_1', 'sii_2', 'sii_3']].apply(majority_vote, axis=1)\n\nfinal_submission = combined[['id', 'final_sii']].rename(columns={'final_sii': 'sii'})\n\nfinal_submission.to_csv('submission.csv', index=False)\n\nprint(\"Majority voting completed and saved to 'Final_Submission.csv'\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T16:42:52.446248Z","iopub.status.idle":"2024-12-19T16:42:52.446647Z","shell.execute_reply.started":"2024-12-19T16:42:52.446469Z","shell.execute_reply":"2024-12-19T16:42:52.446488Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"final_submission","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-19T16:42:52.448981Z","iopub.status.idle":"2024-12-19T16:42:52.449652Z","shell.execute_reply.started":"2024-12-19T16:42:52.449314Z","shell.execute_reply":"2024-12-19T16:42:52.449346Z"}},"outputs":[],"execution_count":null}]}