{"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"}],"dockerImageVersionId":30786,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-07T01:48:48.468287Z","iopub.execute_input":"2024-12-07T01:48:48.468690Z","iopub.status.idle":"2024-12-07T01:48:52.671415Z","shell.execute_reply.started":"2024-12-07T01:48:48.468654Z","shell.execute_reply":"2024-12-07T01:48:52.670337Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_train = pd.read_csv(\"/kaggle/input/child-mind-institute-problematic-internet-use/train.csv\", encoding='utf-8')\ndata_test = pd.read_csv(\"/kaggle/input/child-mind-institute-problematic-internet-use/test.csv\", encoding='utf-8')\ndata_dictionary = pd.read_csv(\"/kaggle/input/child-mind-institute-problematic-internet-use/data_dictionary.csv\", encoding='utf-8')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T01:48:52.673150Z","iopub.execute_input":"2024-12-07T01:48:52.673615Z","iopub.status.idle":"2024-12-07T01:48:52.754802Z","shell.execute_reply.started":"2024-12-07T01:48:52.673581Z","shell.execute_reply":"2024-12-07T01:48:52.753884Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T01:48:52.756202Z","iopub.execute_input":"2024-12-07T01:48:52.756987Z","iopub.status.idle":"2024-12-07T01:48:52.764936Z","shell.execute_reply.started":"2024-12-07T01:48:52.756927Z","shell.execute_reply":"2024-12-07T01:48:52.763862Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_train.columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T01:48:52.767928Z","iopub.execute_input":"2024-12-07T01:48:52.768263Z","iopub.status.idle":"2024-12-07T01:48:52.779036Z","shell.execute_reply.started":"2024-12-07T01:48:52.768232Z","shell.execute_reply":"2024-12-07T01:48:52.777983Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T01:48:52.780117Z","iopub.execute_input":"2024-12-07T01:48:52.780482Z","iopub.status.idle":"2024-12-07T01:48:52.824728Z","shell.execute_reply.started":"2024-12-07T01:48:52.780417Z","shell.execute_reply":"2024-12-07T01:48:52.823577Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_train.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T01:48:52.825889Z","iopub.execute_input":"2024-12-07T01:48:52.826169Z","iopub.status.idle":"2024-12-07T01:48:52.854749Z","shell.execute_reply.started":"2024-12-07T01:48:52.826141Z","shell.execute_reply":"2024-12-07T01:48:52.853667Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\nimport polars as pl\n\nfiltered_data = data_train[data_train['sii'].notna()]\nprint(filtered_data['sii'].count())\nprint(filtered_data)\n\nsns.countplot(x='sii', data=filtered_data)\nplt.title('Phân phối cột mục tiêu (sii)')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T01:48:52.856038Z","iopub.execute_input":"2024-12-07T01:48:52.856353Z","iopub.status.idle":"2024-12-07T01:48:54.295012Z","shell.execute_reply.started":"2024-12-07T01:48:52.856321Z","shell.execute_reply":"2024-12-07T01:48:54.293927Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print('Columns missing in test:')\nprint([f for f in data_train.columns if f not in data_test.columns])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T01:48:54.296545Z","iopub.execute_input":"2024-12-07T01:48:54.297178Z","iopub.status.idle":"2024-12-07T01:48:54.303907Z","shell.execute_reply.started":"2024-12-07T01:48:54.297128Z","shell.execute_reply":"2024-12-07T01:48:54.302605Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"missing_values = filtered_data.isnull().sum()\nprint(\"Số lượng giá trị thiếu:\\n\", missing_values[missing_values >= 0])\nmissing_percent = (missing_values / len(data_train)) * 100\n\nmissing_data = pd.DataFrame({\n    'Số lượng thiếu': missing_values,\n    'Phần trăm (%)': missing_percent\n})\n\nmissing_data = missing_data[missing_data['Số lượng thiếu'] >= 0]\n\nmissing_data_sort = missing_data.sort_values(by='Phần trăm (%)', ascending=True)\n\nif missing_data_sort.empty:\n    print(\"Không có cột nào có giá trị thiếu.\")\nelse:\n    plt.figure(figsize=(10, len(missing_data) * 0.2))\n    sns.barplot(\n        y=missing_data_sort.index, \n        x=missing_data_sort['Phần trăm (%)'], \n        palette=\"viridis\"\n    )\n    plt.xticks(rotation=45, ha='right')\n    plt.title(\"Tỷ lệ phần trăm giá trị thiếu theo cột\", fontsize=14)\n    plt.ylabel(\"Feature\", fontsize=12)\n    plt.xlabel(\"Phần trăm (%)\", fontsize=12)\n\n    plt.gca().yaxis.set_tick_params(labelsize=10, pad=5)\n    plt.tight_layout()\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T01:48:54.305581Z","iopub.execute_input":"2024-12-07T01:48:54.306345Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"columns_to_drop = missing_data[missing_data['Phần trăm (%)'] > 30].index\n\nprint(\"Các feature có tỷ lệ giá trị thiếu lớn hơn 30%:\")\nprint(columns_to_drop)\n\ndata_train_cleaned = data_train.drop(columns=columns_to_drop)\n\ndata_train_cleaned.info()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(data_train[['Basic_Demos-Age', 'Basic_Demos-Sex']].describe())\nsns.histplot(data_train['Basic_Demos-Age'], kde=True)\nplt.title('Age Distribution')\nplt.show()\nsns.countplot(x='Basic_Demos-Sex', data=data_train)\nplt.title('Sex Distribution')\nplt.show()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.impute import KNNImputer\n\nknn_imputer = KNNImputer(n_neighbors=5)\n\nnum_cols = data_train_cleaned.select_dtypes(include=['float64', 'int64']).columns\n\ndata_train_cleaned[num_cols] = knn_imputer.fit_transform(data_train_cleaned[num_cols])\n\nprint(data_train_cleaned.isnull().sum())","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"missing_values_train_after = data_train_cleaned.isnull().sum()\nprint(\"Missing values after imputation in train dataset:\\n\", missing_values_train_after)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_cols = ['Basic_Demos-Age', 'Physical-BMI', 'PCIAT-PCIAT_Total']\nfor col in num_cols:\n    sns.boxplot(x='sii', y=col, data=data_train_cleaned)\n    plt.title(f'Mối quan hệ giữa {col} và sii')\n    plt.show()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cat_cols = ['Basic_Demos-Sex', 'Basic_Demos-Enroll_Season', 'PCIAT-Season']\nfor col in cat_cols:\n    sns.countplot(x=col, hue='sii', data=data_train_cleaned)\n    plt.title(f'Mối quan hệ giữa {col} và sii')\n    plt.show()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"season_mapping = {'Spring': 1, 'Summer': 2, 'Fall': 3, 'Winter': 4}\nseason_cols = [col for col in data_train_cleaned.columns if 'Season' in col]\nfor col in season_cols:\n    data_train_cleaned[col] = data_train_cleaned[col].map(season_mapping)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_train_no_id = data_train_cleaned.drop(columns = ['id'], errors = 'ignore')\ndata_train_no_id.info()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pciat_columns = [col for col in data_train_cleaned.columns if 'PCIAT-PCIAT' in col and col != 'PCIAT-PCIAT_Total']\ncorr_with_total = data_train_cleaned[pciat_columns].corrwith(data_train_cleaned['PCIAT-PCIAT_Total'])\nprint(\"Mối tương quan với PCIAT_PCIAT_TOTAL:\")\nprint(corr_with_total)\n\ndata_train_no_id.drop(columns= pciat_columns, inplace=True)\ndata_train_cleaned.drop(columns= pciat_columns, inplace=True)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"corr_matrix = data_train_no_id.corr()\n\nplt.figure(figsize=(30, 30))\nsns.heatmap(corr_matrix, annot=True, cmap='coolwarm', linewidths=0.5, fmt='.2f', vmin=-1, vmax=1)\nplt.title('Heatmap of Correlation Matrix')\nplt.show()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"threshold = 0.8\n\nto_drop = set()\nfor i in range(len(corr_matrix.columns)):\n    for j in range(i):\n        if abs(corr_matrix.iloc[i, j]) > threshold:\n            colname = corr_matrix.columns[i]\n            to_drop.add(colname)\n\nto_drop.discard('sii')\n\ndata_train_cleaned = data_train_cleaned.drop(columns=to_drop)\n\nprint(f\"Những cột đã bị loại bỏ: {to_drop}\")\nprint(data_train_cleaned.shape)  \n\ndata_train_cleaned = data_train_cleaned.drop(columns=['PCIAT-Season', 'PCIAT-PCIAT_Total'])","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"actigraphy = pl.read_parquet('/kaggle/input/child-mind-institute-problematic-internet-use/series_train.parquet/id=0417c91e/part-0.parquet')\nactigraphy","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import random\nimport os\nimport torch\nfrom concurrent.futures import ThreadPoolExecutor\nfrom tqdm import tqdm\nfrom sklearn.preprocessing import StandardScaler\nimport torch.nn as nn\nimport torch.optim as optim\n\n# Seed for reproducibility\ndef seed_everything(seed):\n    random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.backends.cudnn.deterministic = True\n    torch.backends.cudnn.benchmark = True\nseed_everything(2024)\n\n# Function to load and process files\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\n# Optimized loading time series\ndef load_time_series(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    \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\n# AutoEncoder class\nclass AutoEncoder(nn.Module):\n    def __init__(self, input_dim, encoding_dim):\n        super(AutoEncoder, self).__init__()\n        self.encoder = nn.Sequential(\n            nn.Linear(input_dim, encoding_dim * 3), nn.ReLU(),\n            nn.Linear(encoding_dim * 3, encoding_dim * 2), nn.ReLU(),\n            nn.Linear(encoding_dim * 2, encoding_dim), nn.ReLU()\n        )\n        self.decoder = nn.Sequential(\n            nn.Linear(encoding_dim, input_dim * 2), nn.ReLU(),\n            nn.Linear(input_dim * 2, input_dim * 3), nn.ReLU(),\n            nn.Linear(input_dim * 3, input_dim), nn.Sigmoid()\n        )\n\n    def forward(self, x):\n        encoded = self.encoder(x)\n        decoded = self.decoder(encoded)\n        return decoded\n\n# Optimized Autoencoder Training Function\ndef perform_autoencoder(df, encoding_dim=50, epochs=50, batch_size=32):\n    scaler = StandardScaler()\n    df_scaled = scaler.fit_transform(df)\n    data_tensor = torch.FloatTensor(df_scaled)\n    \n    input_dim = data_tensor.shape[1]\n    autoencoder = AutoEncoder(input_dim, encoding_dim)\n    criterion = nn.MSELoss()\n    optimizer = optim.Adam(autoencoder.parameters())\n    \n    for epoch in range(epochs):\n        for i in range(0, len(data_tensor), batch_size):\n            batch = data_tensor[i:i + batch_size]\n            optimizer.zero_grad()\n            reconstructed = autoencoder(batch)\n            loss = criterion(reconstructed, batch)\n            loss.backward()\n            optimizer.step()\n            \n        if (epoch + 1) % 10 == 0:\n            print(f'Epoch [{epoch + 1}/{epochs}], Loss: {loss.item():.4f}')\n    \n    with torch.no_grad():\n        encoded_data = autoencoder.encoder(data_tensor).numpy()\n    df_encoded = pd.DataFrame(encoded_data, columns=[f'Enc_{i + 1}' for i in range(encoded_data.shape[1])])\n    return df_encoded\n\ndef feature_engineering(df):\n    df['BMI_Age'] = df['Physical-BMI'] * df['Basic_Demos-Age']\n    df['Internet_Hours_Age'] = df['PreInt_EduHx-computerinternet_hoursday'] * df['Basic_Demos-Age']\n    df['BMI_Internet_Hours'] = df['Physical-BMI'] * df['PreInt_EduHx-computerinternet_hoursday']\n    df['BFP_BMI'] = df['BIA-BIA_Fat'] / df['BIA-BIA_BMI']\n    df['FFMI_BFP'] = df['BIA-BIA_FFMI'] / df['BIA-BIA_Fat']\n    df['FMI_BFP'] = df['BIA-BIA_FMI'] / df['BIA-BIA_Fat']\n    df['LST_TBW'] = df['BIA-BIA_LST'] / df['BIA-BIA_TBW']\n    df['BFP_BMR'] = df['BIA-BIA_Fat'] * df['BIA-BIA_BMR']\n    df['BFP_DEE'] = df['BIA-BIA_Fat'] * df['BIA-BIA_DEE']\n    df['BMR_Weight'] = df['BIA-BIA_BMR'] / df['Physical-Weight']\n    df['DEE_Weight'] = df['BIA-BIA_DEE'] / df['Physical-Weight']\n    df['SMM_Height'] = df['BIA-BIA_SMM'] / df['Physical-Height']\n    df['Muscle_to_Fat'] = df['BIA-BIA_SMM'] / df['BIA-BIA_FMI']\n    df['Hydration_Status'] = df['BIA-BIA_TBW'] / df['Physical-Weight']\n    df['ICW_TBW'] = df['BIA-BIA_ICW'] / df['BIA-BIA_TBW']\n    df['BMI_PHR'] = df['Physical-BMI'] * df['Physical-HeartRate']\n    df['SDS_InternetHours'] = df['SDS-SDS_Total_T'] * df['PreInt_EduHx-computerinternet_hoursday']\n    return df\n\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\ntrain_ts = load_time_series(\"/kaggle/input/child-mind-institute-problematic-internet-use/series_train.parquet\")\ntest_ts = load_time_series(\"/kaggle/input/child-mind-institute-problematic-internet-use/series_test.parquet\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train = train_ts.drop('id', axis=1)\ndf_test = test_ts.drop('id', axis=1)\n\ntrain_ts_encoded = perform_autoencoder(df_train, encoding_dim=60, epochs=100, batch_size=32)\ntest_ts_encoded = perform_autoencoder(df_test, encoding_dim=60, epochs=100, batch_size=32)\n\ntrain_ts_encoded[\"id\"] = train_ts[\"id\"]\ntest_ts_encoded['id'] = test_ts[\"id\"]\n\ndata_cleaned = pd.merge(data_train_cleaned, train_ts_encoded, how=\"left\", on='id')\ntest = pd.merge(test, test_ts_encoded, how=\"left\", on='id')","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_cleaned","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn_imputer = KNNImputer(n_neighbors=5)\n\nnum_cols = data_cleaned.select_dtypes(include=['int32', 'int64', 'float64', 'int64']).columns\n\ndata_cleaned[num_cols] = knn_imputer.fit_transform(data_cleaned[num_cols])\n\ndata_cleaned = data_cleaned.dropna(thresh=10, axis=0)\n\nprint(data_cleaned.isnull().sum())","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_cleaned.drop('id', axis=1)\ndata_cleaned","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_cleaned.info()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def filter_test_data(train_data, test_data, target_column):\n    train_columns = [col for col in train_data.columns if col != target_column]\n    \n    test_data_filtered = test_data[train_columns]\n    \n    return test_data_filtered\n\ntest = filter_test_data (data_cleaned, test, 'sii')\nseason_mapping = {'Spring': 1, 'Summer': 2, 'Fall': 3, 'Winter': 4}\nseason_cols = [col for col in test.columns if 'Season' in col]\nfor col in season_cols:\n    test[col] = test[col].map(season_mapping)\ntest.info()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import cohen_kappa_score  # Đảm bảo bạn đã nhập khẩu hàm này\nfrom sklearn.ensemble import GradientBoostingRegressor\nfrom xgboost import XGBRegressor\nfrom catboost import CatBoostRegressor\nfrom sklearn.metrics import make_scorer\nfrom scipy.optimize import minimize\nfrom sklearn.model_selection import KFold\nimport numpy as np\nimport pandas as pd\nfrom tqdm import tqdm\n\ndef quadratic_weighted_kappa(y_true, y_pred):\n    rounded_y_true = threshold_Rounder(y_true, [0.5, 1.5, 2.5])\n    rounded_y_pred = threshold_Rounder(y_pred, [0.5, 1.5, 2.5]) \n\n    return cohen_kappa_score(rounded_y_true, rounded_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_y_true = threshold_Rounder(y_true, thresholds)\n    rounded_p = threshold_Rounder(oof_non_rounded, thresholds)\n    return -quadratic_weighted_kappa(rounded_y_true, rounded_p)\n\n\ndef TrainML(train_data, test_data, target_column, n_splits=5, random_state=42):\n    X = train_data.drop(columns=[target_column, 'id'])\n    y = train_data[target_column]\n    test_data_dropped = test_data.drop(columns=['id'])\n\n    test_ids = test_data['id']\n\n    from sklearn.impute import SimpleImputer  \n    imputer = SimpleImputer(strategy='mean')\n    X = imputer.fit_transform(X)\n    test_data_dropped = imputer.transform(test_data_dropped)\n\n    # Khởi tạo Cross Validation\n    SKF = KFold(n_splits=n_splits, shuffle=True, random_state=random_state)\n\n    # Lưu kết quả\n    oof_non_rounded = np.zeros(len(y), dtype=float)\n    test_preds = np.zeros((len(test_data), n_splits))\n\n    # Model\n    models = {\n        'xgboost': XGBRegressor(\n            learning_rate=0.05, max_depth=6, n_estimators=200, subsample=0.8,\n            colsample_bytree=0.8, reg_alpha=1, reg_lambda=5, random_state=random_state\n        ),\n        'gboost': GradientBoostingRegressor(\n            learning_rate=0.05, max_depth=6, n_estimators=200, random_state=random_state\n        ),\n        'catboost': CatBoostRegressor(\n            learning_rate=0.05, depth=6, iterations=200, random_state=random_state, verbose=0  # Cấu hình CatBoost\n        )\n    }\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.iloc[train_idx], y.iloc[test_idx]\n\n        # Huấn luyện từng model\n        for model_name, model in models.items():\n            model.fit(X_train, y_train)\n            y_val_pred = model.predict(X_val)\n            oof_non_rounded[test_idx] += y_val_pred / len(models)\n\n            # Dự đoán trên tập test\n            test_preds[:, fold] += model.predict(test_data_dropped) / len(models)\n\n    # Tối ưu threshold\n    KappaOptimizer = minimize(\n        evaluate_predictions, x0=[0.5, 1.5, 2.5],\n        args=(y, oof_non_rounded), method='Nelder-Mead'\n    )\n    assert KappaOptimizer.success, \"Optimization did not converge.\"\n\n    # Áp dụng threshold tối ưu\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    # Dự đoán tập test\n    tpm = test_preds.mean(axis=1)\n    tpTuned = threshold_Rounder(tpm, KappaOptimizer.x)\n\n    # Tạo submission với id\n    submission = pd.DataFrame({\n        'id': test_ids,  # Sử dụng lại cột 'id' đã lưu\n        target_column: tpTuned\n    })\n\n    return submission","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = TrainML(data_cleaned, test, target_column=\"sii\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}