{"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":205315638,"sourceType":"kernelVersion"}],"dockerImageVersionId":30761,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# <p style=\"background-color:#6A1B9A; font-family:'Dancing Script', cursive; color:#FFFFFF; font-size:150%; text-align:center; border: 3px solid #FFEB3B; border-radius:40px; padding: 10px;\">Child Mind Institute | SIngleLGBM</p>","metadata":{}},{"cell_type":"markdown","source":"This notebook is a fork of [abdmental01](https://www.kaggle.com/code/abdmental01/cmi-best-single-model) Thanks for your great work!\n\n\nSince the training dataset is so small, my CV score was varying around +-0.05 depending on the seed!!. Hence, I wanted to find a reliable way to get a stable CV for my model and check if there's a correlation between the CV and LB. Hence I decided to create a multi-seed ensemble where the final predictions are computed with VotingClassfier across different seed and the final cv is just the mean of cv across each seed. From my experiments, this method seems to be consistent and gives good correlation between CV and LB.\n\n\nExperiments: Baseline: CV=0.44967, LB=0.455\n1. Removing all the `Physical-*` columns from the list: CV=0.0.4601, LB=0.466\n2. Changing lambda1 of the LGBM model form 4.73->4.74: CV=0.0.4609, LB=0.467\n\n\nI also think that the public dataset is a bit easy and it might contain less samples of severe cases which are hard to predict, which could explain higher cv value, but most likely this will not be case with the private dataset so don't overfit!!","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport polars as pl\nimport pandas as pd\nfrom sklearn.base import clone\nfrom copy import deepcopy\n\n\nimport optuna\nfrom scipy.optimize import minimize\nimport os\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nimport re\nfrom colorama import Fore, Style\n\nfrom tqdm import tqdm\nfrom IPython.display import clear_output\nfrom concurrent.futures import ThreadPoolExecutor\n\nimport warnings\nwarnings.filterwarnings('ignore')\npd.options.display.max_columns = None\n\nimport lightgbm as lgb\nfrom catboost import CatBoostRegressor\nfrom xgboost import XGBRegressor\nfrom sklearn.ensemble import VotingRegressor,StackingRegressor\nfrom sklearn.model_selection import *\nfrom sklearn.metrics import *\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.svm import SVR\n\nn_splits = 5","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-11-06T05:28:43.770053Z","iopub.execute_input":"2024-11-06T05:28:43.770728Z","iopub.status.idle":"2024-11-06T05:28:43.779794Z","shell.execute_reply.started":"2024-11-06T05:28:43.770681Z","shell.execute_reply":"2024-11-06T05:28:43.778556Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color:#6A1B9A; font-family:'Dancing Script', cursive; color:#FFFFFF; font-size:120%; text-align:center; border: 3px solid #FFEB3B; border-radius:40px; padding: 10px;\">Basic Preprocess</p>","metadata":{}},{"cell_type":"code","source":"%%time\n\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_time_series(dirname) -> pd.DataFrame:\n    ids = os.listdir(dirname)\n    \n    with ThreadPoolExecutor() as executor:\n        results = list(tqdm(executor.map(lambda fname: process_file(fname, dirname), ids), total=len(ids)))\n    \n    stats, indexes = zip(*results)\n    \n    df = pd.DataFrame(stats, columns=[f\"Stat_{i}\" for i in range(len(stats[0]))])\n    df['id'] = indexes\n    \n    return df\n\nroot_dir = \"/kaggle/input/child-mind-institute-problematic-internet-use\"\ntrain = pd.read_csv(f'{root_dir}/train.csv')\ntest = pd.read_csv(f'{root_dir}/test.csv')\nsample = pd.read_csv(f'{root_dir}/sample_submission.csv')\n\ntrain_ts = load_time_series(f\"{root_dir}/series_train.parquet\")\ntest_ts = load_time_series(f\"{root_dir}/series_test.parquet\")\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)\n\nfeaturesCols = ['Basic_Demos-Enroll_Season', 'Basic_Demos-Age', 'Basic_Demos-Sex',\n                'CGAS-Season', 'CGAS-CGAS_Score', 'Physical-Season', \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']\n\nfeaturesCols += time_series_cols\n\ntrain = train[featuresCols + ['sii']]\ntrain = train.dropna(subset='sii')\ntest = test[featuresCols]\n\ncat_c = ['Basic_Demos-Enroll_Season', 'CGAS-Season', 'Physical-Season', 'Fitness_Endurance-Season', \n          'FGC-Season', 'BIA-Season', 'PAQ_A-Season', 'PAQ_C-Season', 'SDS-Season', 'PreInt_EduHx-Season']\n\ndef update(df):\n    for c in cat_c: \n        df[c] = df[c].fillna('Missing')\n        df[c] = df[c].astype('category')\n    return df\n        \ntrain = update(train)\ntest = update(test)\n\ndef create_mapping(column, dataset):\n    unique_values = dataset[column].unique()\n    return {value: idx for idx, value in enumerate(unique_values)}\n\n\n\"\"\"This Mapping Works Fine For me I also Check Each Values in Train and test Using Logic. There no Data Lekage.\"\"\"\n\nfor col in cat_c:\n    mapping_train = create_mapping(col, train)\n    mapping_test = create_mapping(col, test)\n    \n    train[col] = train[col].replace(mapping_train).astype(int)\n    test[col] = test[col].replace(mapping_test).astype(int)\n\nprint(f'Train Shape : {train.shape} || Test Shape : {test.shape}')\n","metadata":{"execution":{"iopub.status.busy":"2024-11-06T05:31:04.806155Z","iopub.execute_input":"2024-11-06T05:31:04.806636Z","iopub.status.idle":"2024-11-06T05:32:40.161891Z","shell.execute_reply.started":"2024-11-06T05:31:04.806598Z","shell.execute_reply":"2024-11-06T05:32:40.160537Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color:#6A1B9A; font-family:'Dancing Script', cursive; color:#FFFFFF; font-size:120%; text-align:center; border: 3px solid #FFEB3B; border-radius:40px; padding: 10px;\">Modeling | Single LGBM</p>","metadata":{}},{"cell_type":"code","source":"\n\nmedian_imputer = SimpleImputer(strategy=\"median\")\n\nX = train.drop(['sii'], axis=1).to_numpy()\ny = train['sii'].to_numpy()\n\nX = median_imputer.fit_transform(X)\ntest = median_imputer.transform(test)\n\n","metadata":{"execution":{"iopub.status.busy":"2024-11-06T05:32:45.924196Z","iopub.execute_input":"2024-11-06T05:32:45.924663Z","iopub.status.idle":"2024-11-06T05:32:45.986619Z","shell.execute_reply.started":"2024-11-06T05:32:45.924622Z","shell.execute_reply":"2024-11-06T05:32:45.985526Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\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)\n\n","metadata":{"execution":{"iopub.status.busy":"2024-11-06T05:32:48.194356Z","iopub.execute_input":"2024-11-06T05:32:48.194832Z","iopub.status.idle":"2024-11-06T05:32:48.202965Z","shell.execute_reply.started":"2024-11-06T05:32:48.194788Z","shell.execute_reply":"2024-11-06T05:32:48.201950Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\ndef TrainML(model_class, test_data, seed):\n    \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[train_idx], X[test_idx]\n        y_train, y_val = y[train_idx], y[test_idx]\n\n        model = clone(model_class)\n\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\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') # Nelder-Mead | # Powell\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 :: {tKappa:.3f}\")\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,oof_non_rounded , model , tKappa\n    return oof_non_rounded, tKappa, submission","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T05:32:52.568792Z","iopub.execute_input":"2024-11-06T05:32:52.569233Z","iopub.status.idle":"2024-11-06T05:32:52.582020Z","shell.execute_reply.started":"2024-11-06T05:32:52.569192Z","shell.execute_reply":"2024-11-06T05:32:52.580785Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lgbm_params = {'learning_rate': 0.03884249148676395, 'max_depth': 12, 'num_leaves': 413, 'min_data_in_leaf': 14,\n           'feature_fraction': 0.7987976913702801, 'bagging_fraction': 0.7602261703576205, 'bagging_freq': 2, \n           'lambda_l1': 4.745462555910575, 'lambda_l2': 4.735028557007343e-06, \"verbosity\": -1} # CV : 0.4094 | LB : 0.471\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T05:33:02.430654Z","iopub.execute_input":"2024-11-06T05:33:02.431093Z","iopub.status.idle":"2024-11-06T05:33:02.436923Z","shell.execute_reply.started":"2024-11-06T05:33:02.431054Z","shell.execute_reply":"2024-11-06T05:33:02.435761Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nscores = []\nsubmissions = []\noof_preds = []\n\nfor seed in [0,31,42,59, 61, 67, 71, 73]:\n\n    print(f\"creating split with seed: {seed}\")\n    oof_pred, final_score, submission = TrainML(lgb.LGBMRegressor(**lgbm_params),test, seed)\n    scores.append(final_score)\n    submissions.append(submission)\n    oof_preds.append(oof_pred)\n    \n\nprint(f\"final score: {np.mean(scores)}\")","metadata":{"execution":{"iopub.status.busy":"2024-11-06T05:33:09.111515Z","iopub.execute_input":"2024-11-06T05:33:09.112405Z","iopub.status.idle":"2024-11-06T05:33:48.342847Z","shell.execute_reply.started":"2024-11-06T05:33:09.112358Z","shell.execute_reply":"2024-11-06T05:33:48.341623Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nsii_pred = np.vstack([sub.sii.values for sub in submissions]).T\nprint(sii_pred.shape)\nfinal_sub = []\n\nfor i, row in tqdm(submissions[0].iterrows()):\n    final_sub.append((row[\"id\"], np.argmax(np.bincount(sii_pred[i]))))\n\n\nsub_df = pd.DataFrame(final_sub, columns=[\"id\", \"sii\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T05:34:05.046351Z","iopub.execute_input":"2024-11-06T05:34:05.046945Z","iopub.status.idle":"2024-11-06T05:34:05.065360Z","shell.execute_reply.started":"2024-11-06T05:34:05.046885Z","shell.execute_reply":"2024-11-06T05:34:05.063698Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <p style=\"background-color:#6A1B9A; font-family:'Dancing Script', cursive; color:#FFFFFF; font-size:120%; text-align:center; border: 3px solid #FFEB3B; border-radius:40px; padding: 10px;\">Submission</p>","metadata":{}},{"cell_type":"code","source":"%%time\n\nsub_df.to_csv('submission.csv', index=False)\nprint(sub_df['sii'].value_counts())","metadata":{"execution":{"iopub.status.busy":"2024-11-06T05:34:16.586714Z","iopub.execute_input":"2024-11-06T05:34:16.587717Z","iopub.status.idle":"2024-11-06T05:34:16.601290Z","shell.execute_reply.started":"2024-11-06T05:34:16.587665Z","shell.execute_reply":"2024-11-06T05:34:16.599990Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pd.read_csv(\"submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-11-06T05:34:20.353730Z","iopub.execute_input":"2024-11-06T05:34:20.354141Z","iopub.status.idle":"2024-11-06T05:34:20.375655Z","shell.execute_reply.started":"2024-11-06T05:34:20.354105Z","shell.execute_reply":"2024-11-06T05:34:20.374546Z"},"trusted":true},"outputs":[],"execution_count":null}]}