{ "cells": [ { "cell_type": "markdown", "id": "082fc435", "metadata": { "papermill": { "duration": 0.005415, "end_time": "2024-12-22T02:22:33.724559", "exception": false, "start_time": "2024-12-22T02:22:33.719144", "status": "completed" }, "tags": [] }, "source": [ "# Extract:\n", "Nhóm mình sử dụng Voting Regressor để voting các model chính: LightGBM, XGBoost và CatBoost.\n", "\n", "LightGBM, XGBoost và CatBoost là các mô hình dạng Gradient Boosting. Nói đại khái là sử dụng nhiều mô hình nhỏ học lần lượt. Mô hình sau sẽ cải tiến điểm yếu của mô hình trước. Và cuối cùng vẫn cho Voting các Model yếu để hoàn thiện mô hình một cách tối ưu." ] }, { "cell_type": "markdown", "id": "2616f6de", "metadata": { "papermill": { "duration": 0.004335, "end_time": "2024-12-22T02:22:33.733527", "exception": false, "start_time": "2024-12-22T02:22:33.729192", "status": "completed" }, "tags": [] }, "source": [ "# Thêm các thư viện cần thiết" ] }, { "cell_type": "code", "execution_count": 1, "id": "d22f42fc", "metadata": { "_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19", "_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5", "execution": { "iopub.execute_input": "2024-12-22T02:22:33.743698Z", "iopub.status.busy": "2024-12-22T02:22:33.743312Z", "iopub.status.idle": "2024-12-22T02:22:49.437492Z", "shell.execute_reply": "2024-12-22T02:22:49.436542Z" }, "papermill": { "duration": 15.701408, "end_time": "2024-12-22T02:22:49.439287", "exception": false, "start_time": "2024-12-22T02:22:33.737879", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import os\n", "import re\n", "from sklearn.base import clone\n", "from sklearn.metrics import cohen_kappa_score\n", "from sklearn.model_selection import StratifiedKFold\n", "from scipy.optimize import minimize\n", "from concurrent.futures import ThreadPoolExecutor\n", "from tqdm import tqdm\n", "import polars as pl\n", "import polars.selectors as cs\n", "import matplotlib.pyplot as plt\n", "from matplotlib.ticker import MaxNLocator, FormatStrFormatter, PercentFormatter\n", "import seaborn as sns\n", "\n", "from sklearn.preprocessing import StandardScaler\n", "import matplotlib.pyplot as plt\n", "from keras.models import Model\n", "from keras.layers import Input, Dense\n", "from keras.optimizers import Adam\n", "import torch\n", "import torch.nn as nn\n", "import torch.optim as optim\n", "\n", "from colorama import Fore, Style\n", "from IPython.display import clear_output\n", "import warnings\n", "from lightgbm import LGBMRegressor\n", "from xgboost import XGBRegressor\n", "from catboost import CatBoostRegressor\n", "from sklearn.ensemble import VotingRegressor, RandomForestRegressor, GradientBoostingRegressor\n", "from sklearn.impute import SimpleImputer, KNNImputer\n", "from sklearn.pipeline import Pipeline\n", "warnings.filterwarnings('ignore')\n", "pd.options.display.max_columns = None" ] }, { "cell_type": "markdown", "id": "721e9104", "metadata": { "papermill": { "duration": 0.004393, "end_time": "2024-12-22T02:22:49.451298", "exception": false, "start_time": "2024-12-22T02:22:49.446905", "status": "completed" }, "tags": [] }, "source": [ "# Xử lý dữ liệu\n", "\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "c27d5918", "metadata": { "execution": { "iopub.execute_input": "2024-12-22T02:22:49.461518Z", "iopub.status.busy": "2024-12-22T02:22:49.460838Z", "iopub.status.idle": "2024-12-22T02:22:49.466795Z", "shell.execute_reply": "2024-12-22T02:22:49.466144Z" }, "papermill": { "duration": 0.012195, "end_time": "2024-12-22T02:22:49.468011", "exception": false, "start_time": "2024-12-22T02:22:49.455816", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "# Tiền xử lý dữ liệu\n", "def data_preprocessing(data):\n", " \n", " # Loại bỏ các cột chứa Season\n", " season_cols = [col for col in data.columns if 'Season' in col]\n", " data = data.drop(season_cols, axis=1)\n", " \n", " # Tạo một số feature mới hữu dụng\n", " data['BMI_Age'] = data['Physical-BMI'] * data['Basic_Demos-Age']\n", " data['Internet_Hours_Age'] = data['PreInt_EduHx-computerinternet_hoursday'] * data['Basic_Demos-Age']\n", " data['BMI_Internet_Hours'] = data['Physical-BMI'] * data['PreInt_EduHx-computerinternet_hoursday']\n", " data['BFP_BMI'] = data['BIA-BIA_Fat'] / data['BIA-BIA_BMI']\n", " data['FFMI_BFP'] = data['BIA-BIA_FFMI'] / data['BIA-BIA_Fat']\n", " data['FMI_BFP'] = data['BIA-BIA_FMI'] / data['BIA-BIA_Fat']\n", " data['LST_TBW'] = data['BIA-BIA_LST'] / data['BIA-BIA_TBW']\n", " data['BFP_BMR'] = data['BIA-BIA_Fat'] * data['BIA-BIA_BMR']\n", " data['BFP_DEE'] = data['BIA-BIA_Fat'] * data['BIA-BIA_DEE']\n", " data['BMR_Weight'] = data['BIA-BIA_BMR'] / data['Physical-Weight']\n", " data['DEE_Weight'] = data['BIA-BIA_DEE'] / data['Physical-Weight']\n", " data['SMM_Height'] = data['BIA-BIA_SMM'] / data['Physical-Height']\n", " data['Muscle_to_Fat'] = data['BIA-BIA_SMM'] / data['BIA-BIA_FMI']\n", " data['Hydration_Status'] = data['BIA-BIA_TBW'] / data['Physical-Weight']\n", " data['ICW_TBW'] = data['BIA-BIA_ICW'] / data['BIA-BIA_TBW']\n", " \n", " return data" ] }, { "cell_type": "code", "execution_count": 3, "id": "a3e9e099", "metadata": { "execution": { "iopub.execute_input": "2024-12-22T02:22:49.477745Z", "iopub.status.busy": "2024-12-22T02:22:49.477500Z", "iopub.status.idle": "2024-12-22T02:22:49.482339Z", "shell.execute_reply": "2024-12-22T02:22:49.481716Z" }, "papermill": { "duration": 0.011158, "end_time": "2024-12-22T02:22:49.483650", "exception": false, "start_time": "2024-12-22T02:22:49.472492", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "# Đọc và xử lý dữ liệu parquet\n", "def process_parquet_file(file_name, file_path):\n", " df = pd.read_parquet(os.path.join(file_path, file_name, 'part-0.parquet'))\n", " df.drop('step', axis=1, inplace=True)\n", " return df.describe().values.reshape(-1), file_name.split('=')[1]\n", "\n", "def load_parquet_file(file_path) -> pd.DataFrame:\n", " # Liệt kê các tệp\n", " file_list = os.listdir(file_path)\n", " \n", " # ThreadPool hỗ trợ xử lý đa luồng\n", " with ThreadPoolExecutor() as executor:\n", " results = list(tqdm(executor.map(lambda fname: process_parquet_file(fname, file_path), file_list), total=len(file_list)))\n", " \n", " # Trả về thống kê và các chỉ số\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", " return df" ] }, { "cell_type": "code", "execution_count": 4, "id": "f9609c25", "metadata": { "execution": { "iopub.execute_input": "2024-12-22T02:22:49.494124Z", "iopub.status.busy": "2024-12-22T02:22:49.493853Z", "iopub.status.idle": "2024-12-22T02:22:49.502960Z", "shell.execute_reply": "2024-12-22T02:22:49.502215Z" }, "papermill": { "duration": 0.016439, "end_time": "2024-12-22T02:22:49.504500", "exception": false, "start_time": "2024-12-22T02:22:49.488061", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "# Mã hóa dữ liệu sử dụng AutoEncoder\n", "class AutoEncoder(nn.Module):\n", " def __init__(self, input_dimen, encode_dimen):\n", " super(AutoEncoder, self).__init__()\n", " self.encoder = nn.Sequential(\n", " nn.Linear(input_dimen, encode_dimen*3),\n", " nn.ReLU(),\n", " nn.Linear(encode_dimen*3, encode_dimen*2),\n", " nn.ReLU(),\n", " nn.Linear(encode_dimen*2, encode_dimen),\n", " nn.ReLU()\n", " )\n", " self.decoder = nn.Sequential(\n", " nn.Linear(encode_dimen, input_dimen*2),\n", " nn.ReLU(),\n", " nn.Linear(input_dimen*2, input_dimen*3),\n", " nn.ReLU(),\n", " nn.Linear(input_dimen*3, input_dimen),\n", " nn.Sigmoid()\n", " )\n", " \n", " def forward(self, x):\n", " encoded = self.encoder(x)\n", " decoded = self.decoder(encoded)\n", " return decoded\n", "\n", "# Mã hóa dữ liệu về 50 chiều\n", "def perform_autoencoder(df, encoding_dim=50, epochs=50, batch_size=32):\n", " # Chuẩn hóa dữ liệu: đưa về z (trung bình = 0, phương sai = 1)\n", " scaler = StandardScaler()\n", " df_scaled = scaler.fit_transform(df)\n", " \n", " # Chuyển dữ liệu đã chuẩn hóa sang dạng tensor để sử dụng trong mô hình NN\n", " data_tensor = torch.FloatTensor(df_scaled)\n", " \n", " # Khởi tạo AutoEncoder\n", " input_dim = data_tensor.shape[1]\n", " autoencoder = AutoEncoder(input_dim, encoding_dim)\n", " \n", " # Cài đặt hàm mất mát và tối ưu\n", " criterion = nn.MSELoss()\n", " optimizer = optim.Adam(autoencoder.parameters())\n", " \n", " # Huấn luyện mô hình Encoder\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", " # Sau mỗi 10 epoch, in ra Loss để theo dõi\n", " if (epoch + 1) % 10 == 0:\n", " print(f'Epoch thứ [{epoch + 1}/{epochs}], Loss = {loss.item():.4f}]')\n", " # Lấy dữ liệu đã được mã hóa & chuyển thành dataframe \n", " with torch.no_grad():\n", " encoded_data = autoencoder.encoder(data_tensor).numpy()\n", " \n", " df_encoded = pd.DataFrame(encoded_data, columns=[f'Enc_{i + 1}' for i in range(encoded_data.shape[1])])\n", " \n", " return df_encoded" ] }, { "cell_type": "markdown", "id": "6f9e4c6c", "metadata": { "papermill": { "duration": 0.00949, "end_time": "2024-12-22T02:22:49.520476", "exception": false, "start_time": "2024-12-22T02:22:49.510986", "status": "completed" }, "tags": [] }, "source": [ "# HÀM MÔ HÌNH HUẤN LUYỆN" ] }, { "cell_type": "code", "execution_count": 5, "id": "fca86cc4", "metadata": { "execution": { "iopub.execute_input": "2024-12-22T02:22:49.535637Z", "iopub.status.busy": "2024-12-22T02:22:49.535246Z", "iopub.status.idle": "2024-12-22T02:22:49.540231Z", "shell.execute_reply": "2024-12-22T02:22:49.538983Z" }, "papermill": { "duration": 0.015813, "end_time": "2024-12-22T02:22:49.542828", "exception": false, "start_time": "2024-12-22T02:22:49.527015", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "SEED = 42\n", "n_splits = 5" ] }, { "cell_type": "code", "execution_count": 6, "id": "9c1b3948", "metadata": { "execution": { "iopub.execute_input": "2024-12-22T02:22:49.559292Z", "iopub.status.busy": "2024-12-22T02:22:49.559028Z", "iopub.status.idle": "2024-12-22T02:22:49.564024Z", "shell.execute_reply": "2024-12-22T02:22:49.563175Z" }, "papermill": { "duration": 0.013845, "end_time": "2024-12-22T02:22:49.565786", "exception": false, "start_time": "2024-12-22T02:22:49.551941", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "# Khởi tạo hàm và tính điểm kappa\n", "def quadratic_weighted_kappa(y_true, y_pred):\n", " return cohen_kappa_score(y_true, y_pred, weights='quadratic')\n", "def 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", "# Làm tròn giá trị dự đoán\n", "def 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)))" ] }, { "cell_type": "markdown", "id": "1850886e", "metadata": { "papermill": { "duration": 0.009357, "end_time": "2024-12-22T02:22:49.582352", "exception": false, "start_time": "2024-12-22T02:22:49.572995", "status": "completed" }, "tags": [] }, "source": [ "Thực hiện huấn luyện và đánh giá mô hình. Trọng tâm hàm là tính toán điểm số Quadratic Weighted Kappa (QWK) và tối ưu hóa bằng Neler-Mead" ] }, { "cell_type": "code", "execution_count": 7, "id": "74880719", "metadata": { "execution": { "iopub.execute_input": "2024-12-22T02:22:49.595047Z", "iopub.status.busy": "2024-12-22T02:22:49.594782Z", "iopub.status.idle": "2024-12-22T02:22:49.602816Z", "shell.execute_reply": "2024-12-22T02:22:49.602128Z" }, "papermill": { "duration": 0.014679, "end_time": "2024-12-22T02:22:49.604069", "exception": false, "start_time": "2024-12-22T02:22:49.589390", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "def TrainingModel(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}, Test QWK: {val_kappa:.4f}\")\n", " clear_output(wait=True)\n", "\n", " print(f\"QWK TB train --> {np.mean(train_S):.4f}\")\n", " print(f\"QWK TB test ---> {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, \"Tối ưu không hội tụ.\"\n", " \n", " oof_tuned = threshold_Rounder(oof_non_rounded, KappaOPtimizer.x)\n", " tKappa = quadratic_weighted_kappa(y, oof_tuned)\n", "\n", " print(f\"----> || Điểm QWK đã tối ưu :: {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" ] }, { "cell_type": "markdown", "id": "72251ff2", "metadata": { "papermill": { "duration": 0.004411, "end_time": "2024-12-22T02:22:49.613290", "exception": false, "start_time": "2024-12-22T02:22:49.608879", "status": "completed" }, "tags": [] }, "source": [ "Hiệu chỉnh tham số cho các mô hình sử dụng" ] }, { "cell_type": "code", "execution_count": 8, "id": "79977149", "metadata": { "execution": { "iopub.execute_input": "2024-12-22T02:22:49.624145Z", "iopub.status.busy": "2024-12-22T02:22:49.623908Z", "iopub.status.idle": "2024-12-22T02:22:49.628867Z", "shell.execute_reply": "2024-12-22T02:22:49.628127Z" }, "papermill": { "duration": 0.011969, "end_time": "2024-12-22T02:22:49.630218", "exception": false, "start_time": "2024-12-22T02:22:49.618249", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "# LightGBM\n", "Params = {\n", " 'learning_rate': 0.046,\n", " 'max_depth': 12,\n", " 'num_leaves': 478,\n", " 'min_data_in_leaf': 13,\n", " 'feature_fraction': 0.893,\n", " 'bagging_fraction': 0.784,\n", " 'bagging_freq': 4,\n", " 'lambda_l1': 10, \n", " 'lambda_l2': 0.01, \n", " 'random_state': SEED,\n", " 'verbose': -1,\n", " 'n_estimator': 300,\n", " 'device': 'gpu'\n", "\n", "}\n", "\n", "\n", "# XGBoost \n", "XGB_Params = {\n", " 'learning_rate': 0.05,\n", " 'max_depth': 6,\n", " 'n_estimators': 200,\n", " 'subsample': 0.8,\n", " 'colsample_bytree': 0.8,\n", " 'reg_alpha': 1, \n", " 'reg_lambda': 5, \n", " 'random_state': SEED,\n", " 'tree_method': 'gpu_hist',\n", "\n", "}\n", "\n", "# CatBoost\n", "CatBoost_Params = {\n", " 'learning_rate': 0.05,\n", " 'depth': 6,\n", " 'iterations': 200,\n", " 'random_seed': SEED,\n", " 'verbose': 0,\n", " 'l2_leaf_reg': 10, \n", " 'task_type': 'GPU'\n", "\n", "}" ] }, { "cell_type": "code", "execution_count": 9, "id": "dd66629e", "metadata": { "execution": { "iopub.execute_input": "2024-12-22T02:22:49.640960Z", "iopub.status.busy": "2024-12-22T02:22:49.640751Z", "iopub.status.idle": "2024-12-22T02:22:49.647155Z", "shell.execute_reply": "2024-12-22T02:22:49.646494Z" }, "papermill": { "duration": 0.01302, "end_time": "2024-12-22T02:22:49.648386", "exception": false, "start_time": "2024-12-22T02:22:49.635366", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "Light = LGBMRegressor(**Params)\n", "XGB_Model = XGBRegressor(**XGB_Params)\n", "CatBoost_Model = CatBoostRegressor(**CatBoost_Params)\n", "\n", "voting_model = VotingRegressor(estimators=[\n", " ('lightgbm', Light),\n", " ('xgboost', XGB_Model),\n", " ('catboost', CatBoost_Model)\n", "])" ] }, { "cell_type": "markdown", "id": "b7c14723", "metadata": { "papermill": { "duration": 0.004132, "end_time": "2024-12-22T02:22:49.656866", "exception": false, "start_time": "2024-12-22T02:22:49.652734", "status": "completed" }, "tags": [] }, "source": [ "# Submission 1" ] }, { "cell_type": "code", "execution_count": 10, "id": "db5f5da4", "metadata": { "execution": { "iopub.execute_input": "2024-12-22T02:22:49.666728Z", "iopub.status.busy": "2024-12-22T02:22:49.666480Z", "iopub.status.idle": "2024-12-22T02:24:18.483837Z", "shell.execute_reply": "2024-12-22T02:24:18.483067Z" }, "papermill": { "duration": 88.824157, "end_time": "2024-12-22T02:24:18.485372", "exception": false, "start_time": "2024-12-22T02:22:49.661215", "status": "completed" }, "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "100%|██████████| 996/996 [01:09<00:00, 14.38it/s]\n", "100%|██████████| 2/2 [00:00<00:00, 9.94it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Epoch thứ [10/100], Loss = 1.6273]\n", "Epoch thứ [20/100], Loss = 1.5442]\n", "Epoch thứ [30/100], Loss = 1.5088]\n", "Epoch thứ [40/100], Loss = 1.5025]\n", "Epoch thứ [50/100], Loss = 1.5003]\n", "Epoch thứ [60/100], Loss = 1.4989]\n", "Epoch thứ [70/100], Loss = 1.3855]\n", "Epoch thứ [80/100], Loss = 1.3827]\n", "Epoch thứ [90/100], Loss = 1.3842]\n", "Epoch thứ [100/100], Loss = 1.3826]\n", "Epoch thứ [10/100], Loss = 1.0255]\n", "Epoch thứ [20/100], Loss = 0.6005]\n", "Epoch thứ [30/100], Loss = 0.4271]\n", "Epoch thứ [40/100], Loss = 0.4271]\n", "Epoch thứ [50/100], Loss = 0.4271]\n", "Epoch thứ [60/100], Loss = 0.4271]\n", "Epoch thứ [70/100], Loss = 0.4271]\n", "Epoch thứ [80/100], Loss = 0.4271]\n", "Epoch thứ [90/100], Loss = 0.4271]\n", "Epoch thứ [100/100], Loss = 0.4271]\n" ] } ], "source": [ "# Đọc các bảng dữ liệu\n", "train = pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/train.csv')\n", "test = pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/test.csv')\n", "sample = pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/sample_submission.csv')\n", "\n", "train_pq = load_parquet_file(\"/kaggle/input/child-mind-institute-problematic-internet-use/series_train.parquet\")\n", "test_pq = load_parquet_file(\"/kaggle/input/child-mind-institute-problematic-internet-use/series_test.parquet\")\n", "\n", "# Xóa cột id\n", "df_train = train_pq.drop('id', axis=1)\n", "df_test = test_pq.drop('id', axis=1)\n", "\n", "# Encode tập parquet\n", "train_pq_encoded = perform_autoencoder(df_train, encoding_dim=60, epochs=100, batch_size=32)\n", "test_pq_encoded = perform_autoencoder(df_test, encoding_dim=60, epochs=100, batch_size=32)\n", "\n", "# Danh sách các cột parquet đã mã hóa\n", "parquet_cols = train_pq_encoded.columns.tolist()\n", "\n", "# Gán id vào dữ liệu đã encode\n", "train_pq_encoded[\"id\"]=train_pq[\"id\"]\n", "test_pq_encoded['id']=test_pq[\"id\"]\n", "\n", "# Kết hợp dữ liệu đã mã hóa vào tập huấn luyện\n", "train = pd.merge(train, train_pq_encoded, how=\"left\", on='id')\n", "test = pd.merge(test, test_pq_encoded, how=\"left\", on='id')\n", "# Dùng K-Nearest Neighbors điền các giá trị thiếu\n", "imputer = KNNImputer(n_neighbors=5)\n", "numeric_cols = train.select_dtypes(include=['float64', 'int64']).columns\n", "imputed_data = imputer.fit_transform(train[numeric_cols])\n", "train_imputed = pd.DataFrame(imputed_data, columns=numeric_cols)\n", "train_imputed['sii'] = train_imputed['sii'].round().astype(int)\n", "for col in train.columns:\n", " if col not in numeric_cols:\n", " train_imputed[col] = train[col]\n", " \n", "train = train_imputed\n", "\n", "# Tiến hành tiền xử lý dữ liệu cho tập train và test\n", "train = data_preprocessing(train)\n", "test = data_preprocessing(test)\n", "\n", "# Hàng nào ít hơn 10 giá trị hợp lệ thì bỏ \n", "train = train.dropna(thresh=10, axis=0)\n", "\n", "# Xóa cột id\n", "train = train.drop('id', axis=1)\n", "test = test .drop('id', axis=1) \n", "\n", "# Xác định các cột đặc trưng cho tập train và tập test\n", "trainingCols = ['Basic_Demos-Age', 'Basic_Demos-Sex',\n", " 'CGAS-CGAS_Score', 'Physical-BMI',\n", " 'Physical-Height', 'Physical-Weight', 'Physical-Waist_Circumference',\n", " 'Physical-Diastolic_BP', 'Physical-HeartRate', 'Physical-Systolic_BP',\n", " 'Fitness_Endurance-Max_Stage',\n", " 'Fitness_Endurance-Time_Mins', 'Fitness_Endurance-Time_Sec',\n", " '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',\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-PAQ_A_Total',\n", " 'PAQ_C-PAQ_C_Total', 'SDS-SDS_Total_Raw',\n", " 'SDS-SDS_Total_T',\n", " 'PreInt_EduHx-computerinternet_hoursday', 'sii', 'BMI_Age','Internet_Hours_Age','BMI_Internet_Hours',\n", " 'BFP_BMI', 'FFMI_BFP', 'FMI_BFP', 'LST_TBW', 'BFP_BMR', 'BFP_DEE', 'BMR_Weight', 'DEE_Weight',\n", " 'SMM_Height', 'Muscle_to_Fat', 'Hydration_Status', 'ICW_TBW']\n", "testingCols = ['Basic_Demos-Age', 'Basic_Demos-Sex',\n", " 'CGAS-CGAS_Score', 'Physical-BMI',\n", " 'Physical-Height', 'Physical-Weight', 'Physical-Waist_Circumference',\n", " 'Physical-Diastolic_BP', 'Physical-HeartRate', 'Physical-Systolic_BP',\n", " 'Fitness_Endurance-Max_Stage',\n", " 'Fitness_Endurance-Time_Mins', 'Fitness_Endurance-Time_Sec',\n", " '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',\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-PAQ_A_Total',\n", " 'PAQ_C-PAQ_C_Total', 'SDS-SDS_Total_Raw',\n", " 'SDS-SDS_Total_T',\n", " 'PreInt_EduHx-computerinternet_hoursday', 'BMI_Age','Internet_Hours_Age','BMI_Internet_Hours',\n", " 'BFP_BMI', 'FFMI_BFP', 'FMI_BFP', 'LST_TBW', 'BFP_BMR', 'BFP_DEE', 'BMR_Weight', 'DEE_Weight',\n", " 'SMM_Height', 'Muscle_to_Fat', 'Hydration_Status', 'ICW_TBW']\n", "# Thêm các đặc trưng lấy từ parquet\n", "trainingCols += parquet_cols\n", "testingCols += parquet_cols\n", "\n", "# Cập nhật lại tập dữ liệu\n", "train = train[trainingCols]\n", "test = test[testingCols]\n", "\n", "# Xóa các cột sii bị rỗng\n", "train = train.dropna(subset='sii')\n", "\n", "# Xử lý giá trị vô cùng\n", "if np.any(np.isinf(train)):\n", " train = train.replace([np.inf, -np.inf], np.nan)" ] }, { "cell_type": "code", "execution_count": 11, "id": "67b75666", "metadata": { "execution": { "iopub.execute_input": "2024-12-22T02:24:18.524362Z", "iopub.status.busy": "2024-12-22T02:24:18.523758Z", "iopub.status.idle": "2024-12-22T02:24:52.697201Z", "shell.execute_reply": "2024-12-22T02:24:52.696347Z" }, "papermill": { "duration": 34.194455, "end_time": "2024-12-22T02:24:52.698746", "exception": false, "start_time": "2024-12-22T02:24:18.504291", "status": "completed" }, "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Training Folds: 100%|██████████| 5/5 [00:34<00:00, 6.81s/it]" ] }, { "name": "stdout", "output_type": "stream", "text": [ "QWK TB train --> 0.7698\n", "QWK TB test ---> 0.4876\n", "----> || Điểm QWK đã tối ưu :: \u001b[36m\u001b[1m 0.537\u001b[0m\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idsii
000008ff91
1000fd4600
2001052581
300115b9f0
40016bb221
5001f33791
60038ba981
70068a4850
80069fbed1
90083e3970
100087dd650
1100abe6550
1200ae59c91
1300af63871
1400bd43591
1500c0cd711
1600d56d4b0
1700d9913d1
1800e6167c0
1900ebc35d1
\n", "
" ], "text/plain": [ " id sii\n", "0 00008ff9 1\n", "1 000fd460 0\n", "2 00105258 1\n", "3 00115b9f 0\n", "4 0016bb22 1\n", "5 001f3379 1\n", "6 0038ba98 1\n", "7 0068a485 0\n", "8 0069fbed 1\n", "9 0083e397 0\n", "10 0087dd65 0\n", "11 00abe655 0\n", "12 00ae59c9 1\n", "13 00af6387 1\n", "14 00bd4359 1\n", "15 00c0cd71 1\n", "16 00d56d4b 0\n", "17 00d9913d 1\n", "18 00e6167c 0\n", "19 00ebc35d 1" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Submission1 = TrainingModel(voting_model, test)\n", "\n", "Submission1" ] }, { "cell_type": "markdown", "id": "20076c4d", "metadata": { "papermill": { "duration": 0.018445, "end_time": "2024-12-22T02:24:52.736989", "exception": false, "start_time": "2024-12-22T02:24:52.718544", "status": "completed" }, "tags": [] }, "source": [ "# Submission 2" ] }, { "cell_type": "code", "execution_count": 12, "id": "6e8a8410", "metadata": { "execution": { "iopub.execute_input": "2024-12-22T02:24:52.774560Z", "iopub.status.busy": "2024-12-22T02:24:52.774283Z", "iopub.status.idle": "2024-12-22T02:26:00.818617Z", "shell.execute_reply": "2024-12-22T02:26:00.817729Z" }, "papermill": { "duration": 68.064588, "end_time": "2024-12-22T02:26:00.819911", "exception": false, "start_time": "2024-12-22T02:24:52.755323", "status": "completed" }, "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "100%|██████████| 996/996 [01:07<00:00, 14.70it/s]\n", "100%|██████████| 2/2 [00:00<00:00, 12.87it/s]\n" ] } ], "source": [ "train = pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/train.csv')\n", "test = pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/test.csv')\n", "sample = pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/sample_submission.csv')\n", "train_pq = load_parquet_file(\"/kaggle/input/child-mind-institute-problematic-internet-use/series_train.parquet\")\n", "test_pq = load_parquet_file(\"/kaggle/input/child-mind-institute-problematic-internet-use/series_test.parquet\")\n", "\n", "pq_cols = train_pq.columns.tolist()\n", "pq_cols.remove(\"id\")\n", "\n", "train = pd.merge(train, train_pq, how=\"left\", on='id')\n", "test = pd.merge(test, test_pq, how=\"left\", on='id')\n", "\n", "train = train.drop('id', axis=1)\n", "test = test.drop('id', axis=1) " ] }, { "cell_type": "code", "execution_count": 13, "id": "5d148efe", "metadata": { "execution": { "iopub.execute_input": "2024-12-22T02:26:00.887973Z", "iopub.status.busy": "2024-12-22T02:26:00.887683Z", "iopub.status.idle": "2024-12-22T02:26:00.896872Z", "shell.execute_reply": "2024-12-22T02:26:00.896194Z" }, "papermill": { "duration": 0.044616, "end_time": "2024-12-22T02:26:00.898182", "exception": false, "start_time": "2024-12-22T02:26:00.853566", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "trainingCols = ['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", "\n", "trainingCols += pq_cols\n", "train = train[trainingCols]\n", "train = train.dropna(subset='sii')" ] }, { "cell_type": "markdown", "id": "e4f4cffd", "metadata": { "papermill": { "duration": 0.032505, "end_time": "2024-12-22T02:26:00.964002", "exception": false, "start_time": "2024-12-22T02:26:00.931497", "status": "completed" }, "tags": [] }, "source": [ "Xử lý các cột phân loại" ] }, { "cell_type": "code", "execution_count": 14, "id": "3f4acdf8", "metadata": { "execution": { "iopub.execute_input": "2024-12-22T02:26:01.030711Z", "iopub.status.busy": "2024-12-22T02:26:01.030343Z", "iopub.status.idle": "2024-12-22T02:26:01.084012Z", "shell.execute_reply": "2024-12-22T02:26:01.083264Z" }, "papermill": { "duration": 0.088597, "end_time": "2024-12-22T02:26:01.085367", "exception": false, "start_time": "2024-12-22T02:26:00.996770", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "categoryFeatures = ['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", "\n", "def update(df):\n", " global categoryFeatures\n", " for c in categoryFeatures: \n", " df[c] = df[c].fillna('Missing')\n", " df[c] = df[c].astype('category')\n", " return df\n", " \n", "train = update(train)\n", "test = update(test)\n", "\n", "# Hàm ánh xạ sang dạng enum\n", "def create_mapping(column, dataset):\n", " unique_values = dataset[column].unique()\n", " return {value: idx for idx, value in enumerate(unique_values)}\n", "\n", "for col in categoryFeatures:\n", " mapping = create_mapping(col, train)\n", " mappingTe = create_mapping(col, test)\n", " \n", " train[col] = train[col].replace(mapping).astype(int)\n", " test[col] = test[col].replace(mappingTe).astype(int)" ] }, { "cell_type": "code", "execution_count": 15, "id": "10b08a36", "metadata": { "execution": { "iopub.execute_input": "2024-12-22T02:26:01.151607Z", "iopub.status.busy": "2024-12-22T02:26:01.151297Z", "iopub.status.idle": "2024-12-22T02:26:14.923765Z", "shell.execute_reply": "2024-12-22T02:26:14.922695Z" }, "papermill": { "duration": 13.807665, "end_time": "2024-12-22T02:26:14.925912", "exception": false, "start_time": "2024-12-22T02:26:01.118247", "status": "completed" }, "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Training Folds: 100%|██████████| 5/5 [00:13<00:00, 2.70s/it]" ] }, { "name": "stdout", "output_type": "stream", "text": [ "QWK TB train --> 0.7259\n", "QWK TB test ---> 0.3804\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "----> || Điểm QWK đã tối ưu :: \u001b[36m\u001b[1m 0.464\u001b[0m\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idsii
000008ff91
1000fd4600
2001052580
300115b9f0
40016bb220
5001f33791
60038ba980
70068a4850
80069fbed1
90083e3970
100087dd650
1100abe6550
1200ae59c91
1300af63871
1400bd43591
1500c0cd711
1600d56d4b0
1700d9913d0
1800e6167c0
1900ebc35d0
\n", "
" ], "text/plain": [ " id sii\n", "0 00008ff9 1\n", "1 000fd460 0\n", "2 00105258 0\n", "3 00115b9f 0\n", "4 0016bb22 0\n", "5 001f3379 1\n", "6 0038ba98 0\n", "7 0068a485 0\n", "8 0069fbed 1\n", "9 0083e397 0\n", "10 0087dd65 0\n", "11 00abe655 0\n", "12 00ae59c9 1\n", "13 00af6387 1\n", "14 00bd4359 1\n", "15 00c0cd71 1\n", "16 00d56d4b 0\n", "17 00d9913d 0\n", "18 00e6167c 0\n", "19 00ebc35d 0" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Submission2 = TrainingModel(voting_model, test)\n", "\n", "Submission2" ] }, { "cell_type": "markdown", "id": "f34b237c", "metadata": { "papermill": { "duration": 0.033669, "end_time": "2024-12-22T02:26:15.009998", "exception": false, "start_time": "2024-12-22T02:26:14.976329", "status": "completed" }, "tags": [] }, "source": [ "# Submission 3" ] }, { "cell_type": "code", "execution_count": 16, "id": "a57cc9b5", "metadata": { "execution": { "iopub.execute_input": "2024-12-22T02:26:15.078216Z", "iopub.status.busy": "2024-12-22T02:26:15.077888Z", "iopub.status.idle": "2024-12-22T02:28:14.547425Z", "shell.execute_reply": "2024-12-22T02:28:14.546584Z" }, "papermill": { "duration": 119.505453, "end_time": "2024-12-22T02:28:14.548971", "exception": false, "start_time": "2024-12-22T02:26:15.043518", "status": "completed" }, "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Training Folds: 100%|██████████| 5/5 [01:59<00:00, 23.85s/it]" ] }, { "name": "stdout", "output_type": "stream", "text": [ "QWK TB train --> 0.9175\n", "QWK TB test ---> 0.3803\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "----> || Điểm QWK đã tối ưu :: \u001b[36m\u001b[1m 0.450\u001b[0m\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idsii
000008ff92
1000fd4600
2001052580
300115b9f0
40016bb221
5001f33791
60038ba980
70068a4850
80069fbed2
90083e3970
100087dd651
1100abe6550
1200ae59c92
1300af63871
1400bd43592
1500c0cd712
1600d56d4b0
1700d9913d0
1800e6167c0
1900ebc35d1
\n", "
" ], "text/plain": [ " id sii\n", "0 00008ff9 2\n", "1 000fd460 0\n", "2 00105258 0\n", "3 00115b9f 0\n", "4 0016bb22 1\n", "5 001f3379 1\n", "6 0038ba98 0\n", "7 0068a485 0\n", "8 0069fbed 2\n", "9 0083e397 0\n", "10 0087dd65 1\n", "11 00abe655 0\n", "12 00ae59c9 2\n", "13 00af6387 1\n", "14 00bd4359 2\n", "15 00c0cd71 2\n", "16 00d56d4b 0\n", "17 00d9913d 0\n", "18 00e6167c 0\n", "19 00ebc35d 1" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "imputer = SimpleImputer(strategy='median')\n", "\n", "ensemble = VotingRegressor(estimators=[\n", " ('lgb', Pipeline(steps=[('imputer', imputer), ('regressor', LGBMRegressor(random_state=SEED))])),\n", " ('xgb', Pipeline(steps=[('imputer', imputer), ('regressor', XGBRegressor(random_state=SEED))])),\n", " ('cat', Pipeline(steps=[('imputer', imputer), ('regressor', CatBoostRegressor(random_state=SEED, silent=True))])),\n", " ('rf', Pipeline(steps=[('imputer', imputer), ('regressor', RandomForestRegressor(random_state=SEED))])),\n", " ('gb', Pipeline(steps=[('imputer', imputer), ('regressor', GradientBoostingRegressor(random_state=SEED))]))\n", "])\n", "\n", "Submission3 = TrainingModel(ensemble, test)\n", "\n", "Submission3" ] }, { "cell_type": "code", "execution_count": 17, "id": "03321cf9", "metadata": { "execution": { "iopub.execute_input": "2024-12-22T02:28:14.620213Z", "iopub.status.busy": "2024-12-22T02:28:14.619956Z", "iopub.status.idle": "2024-12-22T02:28:14.636832Z", "shell.execute_reply": "2024-12-22T02:28:14.635901Z" }, "papermill": { "duration": 0.052844, "end_time": "2024-12-22T02:28:14.638109", "exception": false, "start_time": "2024-12-22T02:28:14.585265", "status": "completed" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Majority voting completed and saved to 'Final_Submission.csv'\n" ] } ], "source": [ "sub1 = Submission1\n", "sub2 = Submission2\n", "sub3 = Submission3\n", "\n", "sub1 = sub1.sort_values(by='id').reset_index(drop=True)\n", "sub2 = sub2.sort_values(by='id').reset_index(drop=True)\n", "sub3 = sub3.sort_values(by='id').reset_index(drop=True)\n", "\n", "combined = pd.DataFrame({\n", " 'id': sub1['id'],\n", " 'sii_1': sub1['sii'],\n", " 'sii_2': sub2['sii'],\n", " 'sii_3': sub3['sii']\n", "})\n", "\n", "def majority_vote(row):\n", " return row.mode()[0]\n", "\n", "combined['final_sii'] = combined[['sii_1', 'sii_2', 'sii_3']].apply(majority_vote, axis=1)\n", "\n", "final_submission = combined[['id', 'final_sii']].rename(columns={'final_sii': 'sii'})\n", "\n", "final_submission.to_csv('submission.csv', index=False)\n", "\n", "print(\"Majority voting completed and saved to 'Final_Submission.csv'\")" ] }, { "cell_type": "code", "execution_count": 18, "id": "d403b134", "metadata": { "execution": { "iopub.execute_input": "2024-12-22T02:28:14.707888Z", "iopub.status.busy": "2024-12-22T02:28:14.707573Z", "iopub.status.idle": "2024-12-22T02:28:14.715425Z", "shell.execute_reply": "2024-12-22T02:28:14.714480Z" }, "papermill": { "duration": 0.043866, "end_time": "2024-12-22T02:28:14.716800", "exception": false, "start_time": "2024-12-22T02:28:14.672934", "status": "completed" }, "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idsii
000008ff91
1000fd4600
2001052580
300115b9f0
40016bb221
5001f33791
60038ba980
70068a4850
80069fbed1
90083e3970
100087dd650
1100abe6550
1200ae59c91
1300af63871
1400bd43591
1500c0cd711
1600d56d4b0
1700d9913d0
1800e6167c0
1900ebc35d1
\n", "
" ], "text/plain": [ " id sii\n", "0 00008ff9 1\n", "1 000fd460 0\n", "2 00105258 0\n", "3 00115b9f 0\n", "4 0016bb22 1\n", "5 001f3379 1\n", "6 0038ba98 0\n", "7 0068a485 0\n", "8 0069fbed 1\n", "9 0083e397 0\n", "10 0087dd65 0\n", "11 00abe655 0\n", "12 00ae59c9 1\n", "13 00af6387 1\n", "14 00bd4359 1\n", "15 00c0cd71 1\n", "16 00d56d4b 0\n", "17 00d9913d 0\n", "18 00e6167c 0\n", "19 00ebc35d 1" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "final_submission" ] } ], "metadata": { "kaggle": { "accelerator": "gpu", "dataSources": [ { "databundleVersionId": 9643020, "sourceId": 81933, "sourceType": "competition" } ], "dockerImageVersionId": 30823, "isGpuEnabled": true, "isInternetEnabled": false, "language": "python", "sourceType": "notebook" }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.12" }, "papermill": { "default_parameters": {}, "duration": 346.01461, "end_time": "2024-12-22T02:28:17.567852", "environment_variables": {}, "exception": null, "input_path": "__notebook__.ipynb", "output_path": "__notebook__.ipynb", "parameters": {}, "start_time": "2024-12-22T02:22:31.553242", "version": "2.6.0" } }, "nbformat": 4, "nbformat_minor": 5 }