{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 365
    },
    "executionInfo": {
     "elapsed": 668,
     "status": "ok",
     "timestamp": 1735311271540,
     "user": {
      "displayName": "kelompok bersama",
      "userId": "01911350349879401396"
     },
     "user_tz": -420
    },
    "id": "A9JXeRGCC1Fg",
    "outputId": "fe6008e2-e5e3-4083-8899-7fe5b6bc4067"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>employee_id</th>\n",
       "      <th>domisili</th>\n",
       "      <th>jenis_kelamin</th>\n",
       "      <th>date_of_birth</th>\n",
       "      <th>join_date</th>\n",
       "      <th>resign_date</th>\n",
       "      <th>marriage_stat</th>\n",
       "      <th>dependant</th>\n",
       "      <th>education</th>\n",
       "      <th>absent_90D</th>\n",
       "      <th>...</th>\n",
       "      <th>active_work_category</th>\n",
       "      <th>work_stability_score</th>\n",
       "      <th>married_dependent_ratio</th>\n",
       "      <th>position_score</th>\n",
       "      <th>job_income_position_score</th>\n",
       "      <th>education_score</th>\n",
       "      <th>education_income_ratio</th>\n",
       "      <th>weighted_satisfaction_performance</th>\n",
       "      <th>resign_risk_indicator</th>\n",
       "      <th>adjusted_work_time</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>EM0001</td>\n",
       "      <td>Kabupaten Bogor</td>\n",
       "      <td>Laki-laki</td>\n",
       "      <td>1970-09-10</td>\n",
       "      <td>2024-01-04</td>\n",
       "      <td>2024-10-31</td>\n",
       "      <td>Married</td>\n",
       "      <td>2</td>\n",
       "      <td>S1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>Short-term</td>\n",
       "      <td>5.00</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>2599023.0</td>\n",
       "      <td>5</td>\n",
       "      <td>1.039609e+06</td>\n",
       "      <td>2.0</td>\n",
       "      <td>Medium</td>\n",
       "      <td>9.329634</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>EM0002</td>\n",
       "      <td>Kota Jakarta Selatan</td>\n",
       "      <td>Laki-laki</td>\n",
       "      <td>1980-12-09</td>\n",
       "      <td>2021-01-05</td>\n",
       "      <td>2023-04-22</td>\n",
       "      <td>Married</td>\n",
       "      <td>3</td>\n",
       "      <td>SLTA</td>\n",
       "      <td>11.0</td>\n",
       "      <td>...</td>\n",
       "      <td>Mid-term</td>\n",
       "      <td>2.25</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>1281761.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.281761e+06</td>\n",
       "      <td>1.4</td>\n",
       "      <td>Medium</td>\n",
       "      <td>9.815385</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>EM0003</td>\n",
       "      <td>Tangerang</td>\n",
       "      <td>Laki-laki</td>\n",
       "      <td>1987-04-25</td>\n",
       "      <td>2022-01-17</td>\n",
       "      <td>2024-01-31</td>\n",
       "      <td>Single</td>\n",
       "      <td>0</td>\n",
       "      <td>D2</td>\n",
       "      <td>3.0</td>\n",
       "      <td>...</td>\n",
       "      <td>Mid-term</td>\n",
       "      <td>6.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>4902208.0</td>\n",
       "      <td>3</td>\n",
       "      <td>1.634069e+06</td>\n",
       "      <td>1.8</td>\n",
       "      <td>Medium</td>\n",
       "      <td>9.646590</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>EM0004</td>\n",
       "      <td>Kepulauan Seribu</td>\n",
       "      <td>Laki-laki</td>\n",
       "      <td>1975-12-24</td>\n",
       "      <td>2022-01-26</td>\n",
       "      <td>2024-10-31</td>\n",
       "      <td>Married</td>\n",
       "      <td>1</td>\n",
       "      <td>S1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>Mid-term</td>\n",
       "      <td>16.50</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>3205246.0</td>\n",
       "      <td>5</td>\n",
       "      <td>1.282098e+06</td>\n",
       "      <td>1.6</td>\n",
       "      <td>Medium</td>\n",
       "      <td>9.536789</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>EM0005</td>\n",
       "      <td>Kota Jakarta Utara</td>\n",
       "      <td>Laki-laki</td>\n",
       "      <td>1987-06-15</td>\n",
       "      <td>2022-01-31</td>\n",
       "      <td>2023-02-21</td>\n",
       "      <td>Single</td>\n",
       "      <td>0</td>\n",
       "      <td>SLTA</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>Mid-term</td>\n",
       "      <td>6.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1208627.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.208627e+06</td>\n",
       "      <td>2.0</td>\n",
       "      <td>Medium</td>\n",
       "      <td>9.131545</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 37 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "  employee_id              domisili jenis_kelamin date_of_birth   join_date  \\\n",
       "0      EM0001       Kabupaten Bogor     Laki-laki    1970-09-10  2024-01-04   \n",
       "1      EM0002  Kota Jakarta Selatan     Laki-laki    1980-12-09  2021-01-05   \n",
       "2      EM0003             Tangerang     Laki-laki    1987-04-25  2022-01-17   \n",
       "3      EM0004      Kepulauan Seribu     Laki-laki    1975-12-24  2022-01-26   \n",
       "4      EM0005    Kota Jakarta Utara     Laki-laki    1987-06-15  2022-01-31   \n",
       "\n",
       "  resign_date marriage_stat  dependant education  absent_90D  ...  \\\n",
       "0  2024-10-31       Married          2        S1         1.0  ...   \n",
       "1  2023-04-22       Married          3      SLTA        11.0  ...   \n",
       "2  2024-01-31        Single          0        D2         3.0  ...   \n",
       "3  2024-10-31       Married          1        S1         1.0  ...   \n",
       "4  2023-02-21        Single          0      SLTA         1.0  ...   \n",
       "\n",
       "   active_work_category work_stability_score married_dependent_ratio  \\\n",
       "0            Short-term                 5.00                       3   \n",
       "1              Mid-term                 2.25                       4   \n",
       "2              Mid-term                 6.00                       1   \n",
       "3              Mid-term                16.50                       2   \n",
       "4              Mid-term                 6.00                       1   \n",
       "\n",
       "   position_score  job_income_position_score education_score  \\\n",
       "0               2                  2599023.0               5   \n",
       "1               1                  1281761.0               1   \n",
       "2               1                  4902208.0               3   \n",
       "3               2                  3205246.0               5   \n",
       "4               1                  1208627.0               1   \n",
       "\n",
       "  education_income_ratio  weighted_satisfaction_performance  \\\n",
       "0           1.039609e+06                                2.0   \n",
       "1           1.281761e+06                                1.4   \n",
       "2           1.634069e+06                                1.8   \n",
       "3           1.282098e+06                                1.6   \n",
       "4           1.208627e+06                                2.0   \n",
       "\n",
       "   resign_risk_indicator  adjusted_work_time  \n",
       "0                 Medium            9.329634  \n",
       "1                 Medium            9.815385  \n",
       "2                 Medium            9.646590  \n",
       "3                 Medium            9.536789  \n",
       "4                 Medium            9.131545  \n",
       "\n",
       "[5 rows x 37 columns]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "df = pd.read_csv('D:\\Tugas Akhir\\Codingan\\Development\\Data\\preprocessed_data_train.csv')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Join Date - Min: 2020-01-02 00:00:00\n",
      "Join Date - Max: 2024-10-30 00:00:00\n",
      "Resign Date - Min: 2020-05-05 00:00:00\n",
      "Resign Date - Max: 2024-10-31 00:00:00\n"
     ]
    }
   ],
   "source": [
    "df['join_date'] = pd.to_datetime(df['join_date'])\n",
    "df['resign_date'] = pd.to_datetime(df['resign_date'])\n",
    "\n",
    "min_join_date = df['join_date'].min()\n",
    "max_join_date = df['join_date'].max()\n",
    "\n",
    "min_resign_date = df['resign_date'].min()\n",
    "max_resign_date = df['resign_date'].max()\n",
    "\n",
    "print(\"Join Date - Min:\", min_join_date)\n",
    "print(\"Join Date - Max:\", max_join_date)\n",
    "print(\"Resign Date - Min:\", min_resign_date)\n",
    "print(\"Resign Date - Max:\", max_resign_date)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = df.dropna(subset=['marriage_stat'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "id": "qT7W_7qbUmtZ"
   },
   "outputs": [],
   "source": [
    "# from sklearn.model_selection import train_test_split\n",
    "\n",
    "# cat_feature = ['departemen', 'position', 'domisili', 'marriage_stat', 'job_satisfaction', 'performance_rating',\n",
    "#                'education', 'active_work_category', 'resign_risk_indicator', 'jenis_kelamin']\n",
    "\n",
    "X = df.drop(columns=['active_work_months','churn_status', 'employee_id', 'date_of_birth', 'join_date', 'resign_date'])\n",
    "y = df['active_work_months']\n",
    "\n",
    "# X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
    "\n",
    "df['join_date'] = pd.to_datetime(df['join_date'])\n",
    "\n",
    "train_data = df[df['join_date'] < '2023-01-01']\n",
    "valid_data = df[df['join_date'] >= '2023-01-01']\n",
    "\n",
    "X_train = train_data.drop(columns=['active_work_months', 'churn_status', 'employee_id', 'date_of_birth', 'join_date', 'resign_date'])\n",
    "y_train = train_data['active_work_months']\n",
    "\n",
    "X_valid = valid_data.drop(columns=['active_work_months', 'churn_status', 'employee_id', 'date_of_birth', 'join_date', 'resign_date'])\n",
    "y_valid = valid_data['active_work_months']\n",
    "\n",
    "cat_feature = ['departemen', 'position', 'domisili', 'marriage_stat', 'job_satisfaction', 'performance_rating',\n",
    "               'education', 'active_work_category', 'resign_risk_indicator', 'jenis_kelamin']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 29830,
     "status": "ok",
     "timestamp": 1735311423993,
     "user": {
      "displayName": "kelompok bersama",
      "userId": "01911350349879401396"
     },
     "user_tz": -420
    },
    "id": "Q8deDWqJY1oC",
    "outputId": "e3fd20f2-2385-4514-c1c6-058f047b7221"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0:\tlearn: 13.5633497\ttest: 22.5835323\tbest: 22.5835323 (0)\ttotal: 258ms\tremaining: 4m 17s\n",
      "200:\tlearn: 2.2131170\ttest: 4.9022408\tbest: 4.9022408 (200)\ttotal: 8.86s\tremaining: 35.2s\n",
      "400:\tlearn: 0.5838665\ttest: 2.2138070\tbest: 2.2138070 (400)\ttotal: 17.6s\tremaining: 26.2s\n",
      "600:\tlearn: 0.3577892\ttest: 1.7168738\tbest: 1.7168738 (600)\ttotal: 26.6s\tremaining: 17.7s\n",
      "800:\tlearn: 0.3073068\ttest: 1.5536284\tbest: 1.5536284 (800)\ttotal: 35.5s\tremaining: 8.82s\n",
      "999:\tlearn: 0.2891487\ttest: 1.4757698\tbest: 1.4757678 (998)\ttotal: 44.5s\tremaining: 0us\n",
      "\n",
      "bestTest = 1.475767819\n",
      "bestIteration = 998\n",
      "\n",
      "Shrink model to first 999 iterations.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<catboost.core.CatBoostRegressor at 0x22f08be3e50>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from catboost import CatBoostRegressor\n",
    "\n",
    "model = CatBoostRegressor(\n",
    "    iterations=1000,\n",
    "    learning_rate=0.01,\n",
    "    depth=6,\n",
    "    cat_features=cat_feature,\n",
    "    loss_function='RMSE',  # Fungsi kerugian regresi, seperti RMSE atau MAE\n",
    "    eval_metric='RMSE',    # Metrik evaluasi regresi\n",
    "    verbose=200\n",
    ")\n",
    "\n",
    "model.fit(X_train, y_train, eval_set=(X_valid, y_valid), use_best_model=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 175
    },
    "executionInfo": {
     "elapsed": 321,
     "status": "ok",
     "timestamp": 1735311559697,
     "user": {
      "displayName": "kelompok bersama",
      "userId": "01911350349879401396"
     },
     "user_tz": -420
    },
    "id": "0AN-WzVOZrtG",
    "outputId": "0aed7f15-c856-4524-c9ec-89119a498d39"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>MSE</th>\n",
       "      <td>2.177891</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>MAE</th>\n",
       "      <td>0.834776</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>RMSE</th>\n",
       "      <td>1.475768</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>R2 Score</th>\n",
       "      <td>0.930862</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             Score\n",
       "MSE       2.177891\n",
       "MAE       0.834776\n",
       "RMSE      1.475768\n",
       "R2 Score  0.930862"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score\n",
    "import numpy as np\n",
    "\n",
    "# Prediksi pada data valid\n",
    "y_pred = model.predict(X_valid)\n",
    "\n",
    "# Menghitung metrik regresi\n",
    "mse = mean_squared_error(y_valid, y_pred)\n",
    "mae = mean_absolute_error(y_valid, y_pred)\n",
    "rmse = np.sqrt(mse)\n",
    "r2 = r2_score(y_valid, y_pred)\n",
    "\n",
    "# Membuat dataframe hasil metrik\n",
    "metrics = {\n",
    "    \"MSE\": mse,\n",
    "    \"MAE\": mae,\n",
    "    \"RMSE\": rmse,\n",
    "    \"R2 Score\": r2\n",
    "}\n",
    "\n",
    "metrics_df = pd.DataFrame.from_dict(metrics, orient='index', columns=['Score'])\n",
    "metrics_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 49260 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 105328 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# Membuat DataFrame untuk mempermudah visualisasi\n",
    "comparison_df = pd.DataFrame({'Actual': y_valid, 'Predicted': y_pred})\n",
    "\n",
    "# Membatasi hanya pada 20 indeks pertama\n",
    "comparison_df_subset = comparison_df.iloc[:20]\n",
    "\n",
    "# Scatter plot untuk membandingkan prediksi dan nilai asli\n",
    "plt.figure(figsize=(10, 6))\n",
    "plt.scatter(range(len(comparison_df_subset)), comparison_df_subset['Actual'], label='Actual Values', alpha=0.7, color='blue')\n",
    "plt.scatter(range(len(comparison_df_subset)), comparison_df_subset['Predicted'], label='Predicted Values', alpha=0.7, color='orange')\n",
    "plt.title('Comparison of Actual vs Predicted Active Work Months (First 20)')\n",
    "plt.xlabel('Index')\n",
    "plt.ylabel('Active Work Months')\n",
    "plt.legend()\n",
    "plt.grid(True)\n",
    "plt.show()\n",
    "\n",
    "# Line plot untuk membandingkan prediksi dan nilai asli\n",
    "plt.figure(figsize=(10, 6))\n",
    "plt.plot(range(len(comparison_df_subset)), comparison_df_subset['Actual'], label='Actual Values', marker='o', linestyle='-', color='blue')\n",
    "plt.plot(range(len(comparison_df_subset)), comparison_df_subset['Predicted'], label='Predicted Values', marker='x', linestyle='-', color='orange')\n",
    "plt.title('Actual vs Predicted Active Work Months (First 20 - Line Plot)')\n",
    "plt.xlabel('Index')\n",
    "plt.ylabel('Active Work Months')\n",
    "plt.legend()\n",
    "plt.grid(True)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[I 2025-01-15 20:13:29,609] A new study created in memory with name: no-name-08bf7bda-2a69-4819-aee4-c38c972943cd\n",
      "[I 2025-01-15 20:14:05,596] Trial 0 finished with value: 2.7842129590357327 and parameters: {'iterations': 947, 'learning_rate': 0.021474274017260056, 'depth': 6, 'subsample': 0.6597772630375474, 'colsample_bylevel': 0.6725654633870543, 'l2_leaf_reg': 11.904064563391351, 'random_strength': 6.636852794260673}. Best is trial 0 with value: 2.7842129590357327.\n",
      "[I 2025-01-15 20:14:18,537] Trial 1 finished with value: 3.3604966459792873 and parameters: {'iterations': 532, 'learning_rate': 0.014997699392015939, 'depth': 4, 'subsample': 0.5066064744995207, 'colsample_bylevel': 0.6769577723016567, 'l2_leaf_reg': 18.54766425803946, 'random_strength': 7.313362982904383}. Best is trial 0 with value: 2.7842129590357327.\n",
      "[I 2025-01-15 20:14:35,244] Trial 2 finished with value: 10.731547937509777 and parameters: {'iterations': 745, 'learning_rate': 0.0018382281881892761, 'depth': 4, 'subsample': 0.6467320760058648, 'colsample_bylevel': 0.557736500080584, 'l2_leaf_reg': 14.828021101711611, 'random_strength': 9.019716501749155}. Best is trial 0 with value: 2.7842129590357327.\n",
      "[I 2025-01-15 20:14:49,463] Trial 3 finished with value: 1.70690993706933 and parameters: {'iterations': 567, 'learning_rate': 0.03781343917057186, 'depth': 4, 'subsample': 0.6039387723621907, 'colsample_bylevel': 0.6808598591491113, 'l2_leaf_reg': 11.477573214087545, 'random_strength': 9.74704206793275}. Best is trial 3 with value: 1.70690993706933.\n",
      "[I 2025-01-15 20:15:13,235] Trial 4 finished with value: 6.377846207599941 and parameters: {'iterations': 682, 'learning_rate': 0.003970846204103649, 'depth': 6, 'subsample': 0.782742848821905, 'colsample_bylevel': 0.6991305457084517, 'l2_leaf_reg': 11.814370178020795, 'random_strength': 7.978490486571175}. Best is trial 3 with value: 1.70690993706933.\n",
      "[I 2025-01-15 20:15:34,758] Trial 5 finished with value: 3.8532653988417978 and parameters: {'iterations': 703, 'learning_rate': 0.009627989777082258, 'depth': 5, 'subsample': 0.6758350820650804, 'colsample_bylevel': 0.6845137435168039, 'l2_leaf_reg': 14.54643516179267, 'random_strength': 7.928157458442849}. Best is trial 3 with value: 1.70690993706933.\n",
      "[I 2025-01-15 20:15:56,121] Trial 6 finished with value: 7.911327625120516 and parameters: {'iterations': 617, 'learning_rate': 0.003161788308676907, 'depth': 6, 'subsample': 0.655849596099901, 'colsample_bylevel': 0.5907470643588891, 'l2_leaf_reg': 15.953005158515333, 'random_strength': 6.33160838409998}. Best is trial 3 with value: 1.70690993706933.\n",
      "[I 2025-01-15 20:16:20,651] Trial 7 finished with value: 2.6472143447255028 and parameters: {'iterations': 762, 'learning_rate': 0.0651233495346411, 'depth': 5, 'subsample': 0.6045406434832912, 'colsample_bylevel': 0.6408137062270973, 'l2_leaf_reg': 7.3636217948679885, 'random_strength': 5.056590959434105}. Best is trial 3 with value: 1.70690993706933.\n",
      "[I 2025-01-15 20:16:40,416] Trial 8 finished with value: 3.904345034752064 and parameters: {'iterations': 836, 'learning_rate': 0.0063081178897640665, 'depth': 4, 'subsample': 0.6209534648047793, 'colsample_bylevel': 0.6735288310668713, 'l2_leaf_reg': 14.619073756706687, 'random_strength': 7.447109529616001}. Best is trial 3 with value: 1.70690993706933.\n",
      "[I 2025-01-15 20:17:12,248] Trial 9 finished with value: 2.675917946189127 and parameters: {'iterations': 995, 'learning_rate': 0.029430371458377378, 'depth': 5, 'subsample': 0.7838327288912854, 'colsample_bylevel': 0.6968730335788775, 'l2_leaf_reg': 6.590606976608566, 'random_strength': 7.1462545755187366}. Best is trial 3 with value: 1.70690993706933.\n",
      "[I 2025-01-15 20:17:25,774] Trial 10 finished with value: 2.4922635454584756 and parameters: {'iterations': 503, 'learning_rate': 0.08959558982916915, 'depth': 4, 'subsample': 0.5117214866989651, 'colsample_bylevel': 0.7761028884115426, 'l2_leaf_reg': 9.546619424302936, 'random_strength': 9.468866323105104}. Best is trial 3 with value: 1.70690993706933.\n",
      "[I 2025-01-15 20:17:38,867] Trial 11 finished with value: 1.744493954106988 and parameters: {'iterations': 504, 'learning_rate': 0.09492600457554262, 'depth': 4, 'subsample': 0.5004298952554207, 'colsample_bylevel': 0.7922138691597381, 'l2_leaf_reg': 9.734927799742529, 'random_strength': 9.936390327287459}. Best is trial 3 with value: 1.70690993706933.\n",
      "[I 2025-01-15 20:17:53,899] Trial 12 finished with value: 2.151305735187685 and parameters: {'iterations': 590, 'learning_rate': 0.04527434848399387, 'depth': 4, 'subsample': 0.5613306996433728, 'colsample_bylevel': 0.7844782145581627, 'l2_leaf_reg': 8.71724115650851, 'random_strength': 9.767586689208404}. Best is trial 3 with value: 1.70690993706933.\n",
      "[I 2025-01-15 20:18:08,769] Trial 13 finished with value: 2.20265785283312 and parameters: {'iterations': 590, 'learning_rate': 0.041259515389968866, 'depth': 4, 'subsample': 0.7140194720599207, 'colsample_bylevel': 0.7444231994330105, 'l2_leaf_reg': 10.07294144120628, 'random_strength': 8.771995918460187}. Best is trial 3 with value: 1.70690993706933.\n",
      "[I 2025-01-15 20:18:23,736] Trial 14 finished with value: 2.6079691048763585 and parameters: {'iterations': 502, 'learning_rate': 0.08460466557013521, 'depth': 5, 'subsample': 0.5587677216450273, 'colsample_bylevel': 0.516048785445198, 'l2_leaf_reg': 11.017167930786023, 'random_strength': 9.982359815218986}. Best is trial 3 with value: 1.70690993706933.\n",
      "[I 2025-01-15 20:18:39,599] Trial 15 finished with value: 2.132731119368835 and parameters: {'iterations': 624, 'learning_rate': 0.046020548752808504, 'depth': 4, 'subsample': 0.5628765445757447, 'colsample_bylevel': 0.7389480544787516, 'l2_leaf_reg': 5.290599965354091, 'random_strength': 8.656602115357634}. Best is trial 3 with value: 1.70690993706933.\n",
      "[I 2025-01-15 20:18:56,192] Trial 16 finished with value: 3.5482068496788455 and parameters: {'iterations': 560, 'learning_rate': 0.020510851473925385, 'depth': 5, 'subsample': 0.7195393184364242, 'colsample_bylevel': 0.6243422046941179, 'l2_leaf_reg': 13.064507268703021, 'random_strength': 9.245627139927496}. Best is trial 3 with value: 1.70690993706933.\n",
      "[I 2025-01-15 20:19:12,951] Trial 17 finished with value: 3.3575114872171645 and parameters: {'iterations': 667, 'learning_rate': 0.012523349283214951, 'depth': 4, 'subsample': 0.5344273642074554, 'colsample_bylevel': 0.7448170199649261, 'l2_leaf_reg': 7.893350620289716, 'random_strength': 8.369812485816734}. Best is trial 3 with value: 1.70690993706933.\n",
      "[I 2025-01-15 20:19:40,464] Trial 18 finished with value: 1.8672095214450317 and parameters: {'iterations': 844, 'learning_rate': 0.09708642998838826, 'depth': 5, 'subsample': 0.6022210392087399, 'colsample_bylevel': 0.7962309080793653, 'l2_leaf_reg': 19.69295037962641, 'random_strength': 9.505062844821552}. Best is trial 3 with value: 1.70690993706933.\n",
      "[I 2025-01-15 20:19:53,762] Trial 19 finished with value: 2.499504916116985 and parameters: {'iterations': 551, 'learning_rate': 0.03006684196464984, 'depth': 4, 'subsample': 0.5855833584969841, 'colsample_bylevel': 0.6087741643186144, 'l2_leaf_reg': 16.820613373939256, 'random_strength': 9.989894548577027}. Best is trial 3 with value: 1.70690993706933.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Best trial:\n",
      "  RMSE: 1.70690993706933\n",
      "  Params: {'iterations': 567, 'learning_rate': 0.03781343917057186, 'depth': 4, 'subsample': 0.6039387723621907, 'colsample_bylevel': 0.6808598591491113, 'l2_leaf_reg': 11.477573214087545, 'random_strength': 9.74704206793275}\n"
     ]
    }
   ],
   "source": [
    "import optuna\n",
    "from catboost import CatBoostRegressor\n",
    "from sklearn.metrics import mean_squared_error\n",
    "\n",
    "# Fungsi objective untuk Optuna\n",
    "def objective(trial):\n",
    "    # Definisikan parameter yang akan dioptimasi\n",
    "    params = {\n",
    "        'iterations': trial.suggest_int('iterations', 500, 1000),\n",
    "        'learning_rate': trial.suggest_float('learning_rate', 0.001, 0.1, log=True),\n",
    "        'depth': trial.suggest_int('depth', 4, 6),\n",
    "        'subsample': trial.suggest_float('subsample', 0.5, 0.8),\n",
    "        'colsample_bylevel': trial.suggest_float('colsample_bylevel', 0.5, 0.8),\n",
    "        'l2_leaf_reg': trial.suggest_float('l2_leaf_reg', 5, 20),\n",
    "        'random_strength': trial.suggest_float('random_strength', 5, 10),\n",
    "        'cat_features': cat_feature,\n",
    "        'loss_function': 'RMSE',  # Fungsi kerugian untuk regresi\n",
    "        'random_state': 42,\n",
    "        'verbose': 0\n",
    "    }\n",
    "\n",
    "    # Inisialisasi model dengan parameter yang dioptimasi\n",
    "    model = CatBoostRegressor(**params)\n",
    "\n",
    "    # Melatih model dengan validasi\n",
    "    model.fit(X_train, y_train, eval_set=(X_valid, y_valid), use_best_model=True)\n",
    "\n",
    "    # Prediksi nilai target\n",
    "    y_pred = model.predict(X_valid)\n",
    "\n",
    "    # Hitung RMSE\n",
    "    rmse = mean_squared_error(y_valid, y_pred, squared=False)\n",
    "\n",
    "    return rmse  # Mengembalikan RMSE sebagai skor yang ingin diminimalkan\n",
    "\n",
    "# Membuat studi Optuna\n",
    "study = optuna.create_study(direction=\"minimize\")  # Minimalkan RMSE\n",
    "study.optimize(objective, n_trials=20)\n",
    "\n",
    "# Menampilkan hasil terbaik\n",
    "print(\"Best trial:\")\n",
    "print(f\"  RMSE: {study.best_value}\")\n",
    "print(f\"  Params: {study.best_params}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0:\tlearn: 13.3189474\ttest: 22.4201913\tbest: 22.4201913 (0)\ttotal: 30.4ms\tremaining: 17.2s\n",
      "200:\tlearn: 1.3383498\ttest: 3.1531773\tbest: 3.1531773 (200)\ttotal: 4.53s\tremaining: 8.25s\n",
      "400:\tlearn: 0.5436172\ttest: 1.9572286\tbest: 1.9572286 (400)\ttotal: 9.49s\tremaining: 3.93s\n",
      "566:\tlearn: 0.4106783\ttest: 1.7069099\tbest: 1.7069099 (566)\ttotal: 13.6s\tremaining: 0us\n",
      "\n",
      "bestTest = 1.706909937\n",
      "bestIteration = 566\n",
      "\n",
      "Final RMSE: 1.70690993706933\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import mean_squared_error\n",
    "\n",
    "# Ambil parameter terbaik dari Optuna\n",
    "best_params = study.best_trial.params\n",
    "\n",
    "# Tambahkan parameter tetap (yang tidak dioptimasi)\n",
    "best_params.update({\n",
    "    'loss_function': 'RMSE',  # Gunakan RMSE sebagai loss function\n",
    "    'cat_features': cat_feature,\n",
    "    'random_state': 42,\n",
    "    'verbose': 200,  # Aktifkan output verbose\n",
    "    'od_type': 'Iter',\n",
    "    'od_wait': 50\n",
    "})\n",
    "\n",
    "# Latih model dengan parameter terbaik\n",
    "final_model = CatBoostRegressor(**best_params)\n",
    "final_model.fit(X_train, y_train, eval_set=(X_valid, y_valid), use_best_model=True)\n",
    "\n",
    "# Evaluasi model final\n",
    "y_pred = final_model.predict(X_valid)\n",
    "final_rmse = mean_squared_error(y_valid, y_pred, squared=False)  # Hitung RMSE\n",
    "print(f\"Final RMSE: {final_rmse}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 48864 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 103588 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# Membuat DataFrame untuk mempermudah visualisasi\n",
    "comparison_df = pd.DataFrame({'Actual': y_valid, 'Predicted': y_pred})\n",
    "\n",
    "# Membatasi hanya pada 20 indeks pertama\n",
    "comparison_df_subset = comparison_df.iloc[:20]\n",
    "\n",
    "# Scatter plot untuk membandingkan prediksi dan nilai asli\n",
    "plt.figure(figsize=(10, 6))\n",
    "plt.scatter(range(len(comparison_df_subset)), comparison_df_subset['Actual'], label='Actual Values', alpha=0.7, color='blue')\n",
    "plt.scatter(range(len(comparison_df_subset)), comparison_df_subset['Predicted'], label='Predicted Values', alpha=0.7, color='orange')\n",
    "plt.title('Comparison of Actual vs Predicted Active Work Months (First 20)')\n",
    "plt.xlabel('Index')\n",
    "plt.ylabel('Active Work Months')\n",
    "plt.legend()\n",
    "plt.grid(True)\n",
    "plt.show()\n",
    "\n",
    "# Line plot untuk membandingkan prediksi dan nilai asli\n",
    "plt.figure(figsize=(10, 6))\n",
    "plt.plot(range(len(comparison_df_subset)), comparison_df_subset['Actual'], label='Actual Values', marker='o', linestyle='-', color='blue')\n",
    "plt.plot(range(len(comparison_df_subset)), comparison_df_subset['Predicted'], label='Predicted Values', marker='x', linestyle='-', color='orange')\n",
    "plt.title('Actual vs Predicted Active Work Months (First 20 - Line Plot)')\n",
    "plt.xlabel('Index')\n",
    "plt.ylabel('Active Work Months')\n",
    "plt.legend()\n",
    "plt.grid(True)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Final Training RMSE: 0.2891486734682186\n",
      "Final Validation RMSE: 1.4757698154285221\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 40844 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Ambil hasil evaluasi dari model\n",
    "evals_result = model.get_evals_result()\n",
    "\n",
    "# Menampilkan skor terakhir\n",
    "train_score = evals_result['learn']['RMSE'][-1]\n",
    "val_score = evals_result['validation']['RMSE'][-1]\n",
    "\n",
    "print(f\"Final Training RMSE: {train_score}\")\n",
    "print(f\"Final Validation RMSE: {val_score}\")\n",
    "\n",
    "# Import library untuk visualisasi\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# Ambil skor training dan validation dari evals_result\n",
    "train_rmse = evals_result['learn']['RMSE']\n",
    "val_rmse = evals_result['validation']['RMSE']\n",
    "\n",
    "# Plot learning curve\n",
    "plt.figure(figsize=(10, 6))\n",
    "plt.plot(train_rmse, label='Training RMSE', color='blue')\n",
    "plt.plot(val_rmse, label='Validation RMSE', color='orange')\n",
    "plt.xlabel('Iteration')\n",
    "plt.ylabel('RMSE')\n",
    "plt.title('Learning Curve')\n",
    "plt.legend()\n",
    "plt.grid()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Final Training RMSE: 0.4106782777280611\n",
      "Final Validation RMSE: 1.706909937069329\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 41108 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Ambil hasil evaluasi dari model\n",
    "evals_result = final_model.get_evals_result()\n",
    "\n",
    "# Menampilkan skor terakhir\n",
    "train_score = evals_result['learn']['RMSE'][-1]\n",
    "val_score = evals_result['validation']['RMSE'][-1]\n",
    "\n",
    "print(f\"Final Training RMSE: {train_score}\")\n",
    "print(f\"Final Validation RMSE: {val_score}\")\n",
    "\n",
    "# Import library untuk visualisasi\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# Ambil skor training dan validation dari evals_result\n",
    "train_rmse = evals_result['learn']['RMSE']\n",
    "val_rmse = evals_result['validation']['RMSE']\n",
    "\n",
    "# Plot learning curve\n",
    "plt.figure(figsize=(10, 6))\n",
    "plt.plot(train_rmse, label='Training RMSE', color='blue')\n",
    "plt.plot(val_rmse, label='Validation RMSE', color='orange')\n",
    "plt.xlabel('Iteration')\n",
    "plt.ylabel('RMSE')\n",
    "plt.title('Learning Curve')\n",
    "plt.legend()\n",
    "plt.grid()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>MSE</th>\n",
       "      <td>2.913542</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>MAE</th>\n",
       "      <td>0.893629</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>RMSE</th>\n",
       "      <td>1.706910</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>R2 Score</th>\n",
       "      <td>0.907509</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             Score\n",
       "MSE       2.913542\n",
       "MAE       0.893629\n",
       "RMSE      1.706910\n",
       "R2 Score  0.907509"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score\n",
    "import numpy as np\n",
    "\n",
    "# Prediksi pada data valid\n",
    "y_pred = final_model.predict(X_valid)\n",
    "\n",
    "# Menghitung metrik regresi\n",
    "mse = mean_squared_error(y_valid, y_pred)\n",
    "mae = mean_absolute_error(y_valid, y_pred)\n",
    "rmse = np.sqrt(mse)\n",
    "r2 = r2_score(y_valid, y_pred)\n",
    "\n",
    "# Membuat dataframe hasil metrik\n",
    "metrics = {\n",
    "    \"MSE\": mse,\n",
    "    \"MAE\": mae,\n",
    "    \"RMSE\": rmse,\n",
    "    \"R2 Score\": r2\n",
    "}\n",
    "\n",
    "metrics_df = pd.DataFrame.from_dict(metrics, orient='index', columns=['Score'])\n",
    "metrics_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CatBoost Regression model saved to 'regression_model.sav'\n"
     ]
    }
   ],
   "source": [
    "import pickle\n",
    "\n",
    "with open('regression_model.sav', 'wb') as f:\n",
    "    pickle.dump(model, f)\n",
    "print(\"CatBoost Regression model saved to 'regression_model.sav'\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CatBoost Regression model saved to 'regression_model.sav'\n"
     ]
    }
   ],
   "source": [
    "import pickle\n",
    "\n",
    "with open('regression_model_final.sav', 'wb') as f:\n",
    "    pickle.dump(final_model, f)\n",
    "print(\"CatBoost Regression model saved to 'regression_model.sav'\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Mengurutkan data berdasarkan waktu (join_date)\n",
    "df = df.sort_values('join_date')\n",
    "X = df.drop(columns=['active_work_months', 'churn_status', 'employee_id', 'date_of_birth', 'join_date', 'resign_date'])\n",
    "y = df['active_work_months']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0:\tlearn: 9.1927263\ttotal: 27.2ms\tremaining: 27.2s\n",
      "200:\tlearn: 1.8023540\ttotal: 5.96s\tremaining: 23.7s\n",
      "400:\tlearn: 0.6209110\ttotal: 15.7s\tremaining: 23.5s\n",
      "600:\tlearn: 0.4269301\ttotal: 27.9s\tremaining: 18.5s\n",
      "800:\tlearn: 0.3636209\ttotal: 34.1s\tremaining: 8.46s\n",
      "999:\tlearn: 0.3239213\ttotal: 45.2s\tremaining: 0us\n",
      "0:\tlearn: 10.1498267\ttotal: 54.1ms\tremaining: 54.1s\n",
      "200:\tlearn: 1.8413201\ttotal: 12.1s\tremaining: 48.1s\n",
      "400:\tlearn: 0.5739607\ttotal: 18.5s\tremaining: 27.6s\n",
      "600:\tlearn: 0.3855343\ttotal: 28.4s\tremaining: 18.8s\n",
      "800:\tlearn: 0.3321234\ttotal: 40.9s\tremaining: 10.2s\n",
      "999:\tlearn: 0.3056851\ttotal: 47.8s\tremaining: 0us\n",
      "0:\tlearn: 12.0823549\ttotal: 31ms\tremaining: 31s\n",
      "200:\tlearn: 2.0424293\ttotal: 16.1s\tremaining: 1m 4s\n",
      "400:\tlearn: 0.5614638\ttotal: 27.2s\tremaining: 40.7s\n",
      "600:\tlearn: 0.3540443\ttotal: 37.5s\tremaining: 24.9s\n",
      "800:\tlearn: 0.3048528\ttotal: 53s\tremaining: 13.2s\n",
      "999:\tlearn: 0.2854924\ttotal: 1m 3s\tremaining: 0us\n",
      "0:\tlearn: 13.6327729\ttotal: 32.6ms\tremaining: 32.6s\n",
      "200:\tlearn: 2.2277348\ttotal: 16s\tremaining: 1m 3s\n",
      "400:\tlearn: 0.5878309\ttotal: 32.2s\tremaining: 48s\n",
      "600:\tlearn: 0.3565726\ttotal: 42.3s\tremaining: 28.1s\n",
      "800:\tlearn: 0.3067813\ttotal: 58.2s\tremaining: 14.5s\n",
      "999:\tlearn: 0.2882435\ttotal: 1m 9s\tremaining: 0us\n",
      "0:\tlearn: 14.5619725\ttotal: 36ms\tremaining: 36s\n",
      "200:\tlearn: 2.3283745\ttotal: 12s\tremaining: 47.7s\n",
      "400:\tlearn: 0.6073001\ttotal: 27.8s\tremaining: 41.6s\n",
      "600:\tlearn: 0.3486209\ttotal: 37.7s\tremaining: 25s\n",
      "800:\tlearn: 0.2916105\ttotal: 54.1s\tremaining: 13.5s\n",
      "999:\tlearn: 0.2717463\ttotal: 1m 10s\tremaining: 0us\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 46532 characters]",
      "text/plain": [
       "<Figure size 800x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "R² Scores for each fold: [0.99040942 0.98790673 0.99653286 0.99102248 0.88548406]\n",
      "Mean R²: 0.97\n",
      "Standard Deviation: 0.04\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import TimeSeriesSplit, cross_val_score\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# Fungsi untuk menghitung skor cross-validation dengan TimeSeriesSplit\n",
    "def time_series_cross_validate_and_visualize_r2(model, X, y, n_splits=5):\n",
    "    # TimeSeriesSplit untuk data terkait waktu\n",
    "    tscv = TimeSeriesSplit(n_splits=n_splits)\n",
    "\n",
    "    # Hitung skor cross-validation dengan metrik R²\n",
    "    scores = cross_val_score(model, X, y, scoring='r2', cv=tscv)\n",
    "\n",
    "    # Rata-rata dan standar deviasi\n",
    "    mean_score = np.mean(scores)\n",
    "    std_score = np.std(scores)\n",
    "\n",
    "    # Visualisasi hasil cross-validation\n",
    "    plt.figure(figsize=(8, 5))\n",
    "    plt.plot(range(1, n_splits + 1), scores, marker='o', linestyle='-', color='b', label='Fold Score')\n",
    "    plt.axhline(y=mean_score, color='r', linestyle='--', label=f'Mean R²: {mean_score:.2f}')\n",
    "    plt.fill_between(range(1, n_splits + 1), mean_score - std_score, mean_score + std_score, color='r', alpha=0.2, label='±1 Std Dev')\n",
    "    plt.title('Time-Based Cross-Validation Scores (R²)')\n",
    "    plt.xlabel('Fold')\n",
    "    plt.ylabel('R² Score')\n",
    "    plt.legend()\n",
    "    plt.grid()\n",
    "    plt.show()\n",
    "\n",
    "    # Cetak hasil skor\n",
    "    print(f'R² Scores for each fold: {scores}')\n",
    "    print(f'Mean R²: {mean_score:.2f}')\n",
    "    print(f'Standard Deviation: {std_score:.2f}')\n",
    "\n",
    "# Contoh penggunaan\n",
    "time_series_cross_validate_and_visualize_r2(model, X, y, n_splits=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0:\tlearn: 8.7841225\ttotal: 17.9ms\tremaining: 12.3s\n",
      "200:\tlearn: 0.6414325\ttotal: 4.79s\tremaining: 11.6s\n",
      "400:\tlearn: 0.4070658\ttotal: 9.53s\tremaining: 6.82s\n",
      "600:\tlearn: 0.3136415\ttotal: 14.6s\tremaining: 2.11s\n",
      "687:\tlearn: 0.2871957\ttotal: 18.3s\tremaining: 0us\n",
      "0:\tlearn: 9.6341897\ttotal: 38.6ms\tremaining: 26.5s\n",
      "200:\tlearn: 0.5292219\ttotal: 8.49s\tremaining: 20.6s\n",
      "400:\tlearn: 0.3513814\ttotal: 16.8s\tremaining: 12s\n",
      "600:\tlearn: 0.2840241\ttotal: 21.7s\tremaining: 3.13s\n",
      "687:\tlearn: 0.2635121\ttotal: 24.4s\tremaining: 0us\n",
      "0:\tlearn: 11.3736677\ttotal: 29.5ms\tremaining: 20.3s\n",
      "200:\tlearn: 0.5146587\ttotal: 5.86s\tremaining: 14.2s\n",
      "400:\tlearn: 0.3822767\ttotal: 10.9s\tremaining: 7.81s\n",
      "600:\tlearn: 0.3077429\ttotal: 16.1s\tremaining: 2.33s\n",
      "687:\tlearn: 0.2894420\ttotal: 18.5s\tremaining: 0us\n",
      "0:\tlearn: 12.7272643\ttotal: 26.4ms\tremaining: 18.1s\n",
      "200:\tlearn: 0.5280327\ttotal: 6.21s\tremaining: 15s\n",
      "400:\tlearn: 0.3907724\ttotal: 13.1s\tremaining: 9.38s\n",
      "600:\tlearn: 0.3223581\ttotal: 22.6s\tremaining: 3.27s\n",
      "687:\tlearn: 0.3042779\ttotal: 27.1s\tremaining: 0us\n",
      "0:\tlearn: 13.4616767\ttotal: 61.1ms\tremaining: 42s\n",
      "200:\tlearn: 0.5121746\ttotal: 6.71s\tremaining: 16.3s\n",
      "400:\tlearn: 0.3754235\ttotal: 13s\tremaining: 9.29s\n",
      "600:\tlearn: 0.3125012\ttotal: 24.6s\tremaining: 3.56s\n",
      "687:\tlearn: 0.2947761\ttotal: 29.9s\tremaining: 0us\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 44544 characters]",
      "text/plain": [
       "<Figure size 800x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "R² Scores for each fold: [0.97547565 0.97730142 0.99540966 0.98816158 0.30451961]\n",
      "Mean R²: 0.85\n",
      "Standard Deviation: 0.27\n"
     ]
    }
   ],
   "source": [
    "time_series_cross_validate_and_visualize_r2(final_model, X, y, n_splits=5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "YHxMZUUgarGa"
   },
   "source": [
    "# Testing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 348
    },
    "executionInfo": {
     "elapsed": 772,
     "status": "ok",
     "timestamp": 1735311729780,
     "user": {
      "displayName": "kelompok bersama",
      "userId": "01911350349879401396"
     },
     "user_tz": -420
    },
    "id": "hwyHyRX_ap0R",
    "outputId": "f98c2683-1c1e-437b-fc49-9f61f5dbba4f"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>employee_id</th>\n",
       "      <th>domisili</th>\n",
       "      <th>jenis_kelamin</th>\n",
       "      <th>date_of_birth</th>\n",
       "      <th>join_date</th>\n",
       "      <th>resign_date</th>\n",
       "      <th>marriage_stat</th>\n",
       "      <th>dependant</th>\n",
       "      <th>education</th>\n",
       "      <th>absent_90D</th>\n",
       "      <th>...</th>\n",
       "      <th>active_work_category</th>\n",
       "      <th>work_stability_score</th>\n",
       "      <th>married_dependent_ratio</th>\n",
       "      <th>position_score</th>\n",
       "      <th>job_income_position_score</th>\n",
       "      <th>education_score</th>\n",
       "      <th>education_income_ratio</th>\n",
       "      <th>weighted_satisfaction_performance</th>\n",
       "      <th>resign_risk_indicator</th>\n",
       "      <th>adjusted_work_time</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>EM9660</td>\n",
       "      <td>Kota Jakarta Timur</td>\n",
       "      <td>Perempuan</td>\n",
       "      <td>1970-07-27</td>\n",
       "      <td>2023-10-09</td>\n",
       "      <td>2024-10-31</td>\n",
       "      <td>Single</td>\n",
       "      <td>0</td>\n",
       "      <td>D3</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>Mid-term</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>4843236.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1210809.00</td>\n",
       "      <td>2.0</td>\n",
       "      <td>Medium</td>\n",
       "      <td>9.182979</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>EM12614</td>\n",
       "      <td>Tangerang</td>\n",
       "      <td>Laki-laki</td>\n",
       "      <td>1972-03-13</td>\n",
       "      <td>2023-12-19</td>\n",
       "      <td>2024-10-31</td>\n",
       "      <td>Married</td>\n",
       "      <td>1</td>\n",
       "      <td>D1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>Short-term</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3491432.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1745716.00</td>\n",
       "      <td>2.6</td>\n",
       "      <td>Medium</td>\n",
       "      <td>9.529412</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>EM2150</td>\n",
       "      <td>Kota Jakarta Timur</td>\n",
       "      <td>Laki-laki</td>\n",
       "      <td>1986-12-27</td>\n",
       "      <td>2022-05-22</td>\n",
       "      <td>2023-07-05</td>\n",
       "      <td>Married</td>\n",
       "      <td>1</td>\n",
       "      <td>SLTA</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>Mid-term</td>\n",
       "      <td>2.600000</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1727468.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1727468.00</td>\n",
       "      <td>3.0</td>\n",
       "      <td>Medium</td>\n",
       "      <td>9.288164</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>EM6733</td>\n",
       "      <td>Kabupaten Bogor</td>\n",
       "      <td>Laki-laki</td>\n",
       "      <td>1978-07-12</td>\n",
       "      <td>2022-12-07</td>\n",
       "      <td>2024-10-31</td>\n",
       "      <td>Married</td>\n",
       "      <td>4</td>\n",
       "      <td>D3</td>\n",
       "      <td>6.0</td>\n",
       "      <td>...</td>\n",
       "      <td>Mid-term</td>\n",
       "      <td>3.285714</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>3745375.0</td>\n",
       "      <td>4</td>\n",
       "      <td>936343.75</td>\n",
       "      <td>1.6</td>\n",
       "      <td>Medium</td>\n",
       "      <td>9.053694</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>EM9133</td>\n",
       "      <td>Kabupaten Bekasi</td>\n",
       "      <td>Perempuan</td>\n",
       "      <td>1994-06-03</td>\n",
       "      <td>2023-05-29</td>\n",
       "      <td>2023-10-20</td>\n",
       "      <td>Married</td>\n",
       "      <td>0</td>\n",
       "      <td>SLTA</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>Short-term</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2658503.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2658503.00</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Medium</td>\n",
       "      <td>9.540000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 37 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "  employee_id            domisili jenis_kelamin date_of_birth   join_date  \\\n",
       "0      EM9660  Kota Jakarta Timur     Perempuan    1970-07-27  2023-10-09   \n",
       "1     EM12614           Tangerang     Laki-laki    1972-03-13  2023-12-19   \n",
       "2      EM2150  Kota Jakarta Timur     Laki-laki    1986-12-27  2022-05-22   \n",
       "3      EM6733     Kabupaten Bogor     Laki-laki    1978-07-12  2022-12-07   \n",
       "4      EM9133    Kabupaten Bekasi     Perempuan    1994-06-03  2023-05-29   \n",
       "\n",
       "  resign_date marriage_stat  dependant education  absent_90D  ...  \\\n",
       "0  2024-10-31        Single          0        D3         2.0  ...   \n",
       "1  2024-10-31       Married          1        D1         1.0  ...   \n",
       "2  2023-07-05       Married          1      SLTA         4.0  ...   \n",
       "3  2024-10-31       Married          4        D3         6.0  ...   \n",
       "4  2023-10-20       Married          0      SLTA         0.0  ...   \n",
       "\n",
       "   active_work_category work_stability_score married_dependent_ratio  \\\n",
       "0              Mid-term             4.000000                       1   \n",
       "1            Short-term             5.000000                       2   \n",
       "2              Mid-term             2.600000                       2   \n",
       "3              Mid-term             3.285714                       5   \n",
       "4            Short-term             4.000000                       1   \n",
       "\n",
       "   position_score  job_income_position_score education_score  \\\n",
       "0               1                  4843236.0               4   \n",
       "1               1                  3491432.0               2   \n",
       "2               1                  1727468.0               1   \n",
       "3               1                  3745375.0               4   \n",
       "4               1                  2658503.0               1   \n",
       "\n",
       "  education_income_ratio  weighted_satisfaction_performance  \\\n",
       "0             1210809.00                                2.0   \n",
       "1             1745716.00                                2.6   \n",
       "2             1727468.00                                3.0   \n",
       "3              936343.75                                1.6   \n",
       "4             2658503.00                                1.0   \n",
       "\n",
       "   resign_risk_indicator  adjusted_work_time  \n",
       "0                 Medium            9.182979  \n",
       "1                 Medium            9.529412  \n",
       "2                 Medium            9.288164  \n",
       "3                 Medium            9.053694  \n",
       "4                 Medium            9.540000  \n",
       "\n",
       "[5 rows x 37 columns]"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_test = pd.read_csv('D:\\Tugas Akhir\\Codingan\\Development\\Data\\data_testing.csv')\n",
    "df_test.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 561,
     "status": "ok",
     "timestamp": 1735309156053,
     "user": {
      "displayName": "kelompok bersama",
      "userId": "01911350349879401396"
     },
     "user_tz": -420
    },
    "id": "VX58wjd8bSfB",
    "outputId": "6d7dd536-8981-4528-8fb5-4fb3e0fec7f4"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "R² Score: 0.9951390052323061\n",
      "Mean Absolute Error (MAE): 0.44852516212478744\n",
      "Mean Squared Error (MSE): 1.198342479918863\n",
      "Root Mean Squared Error (RMSE): 1.094688302631787\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import r2_score, mean_absolute_error, mean_squared_error\n",
    "import numpy as np\n",
    "\n",
    "# Menghapus kolom yang tidak diperlukan untuk prediksi\n",
    "X_test = df_test.drop(columns=['active_work_months','churn_status', 'employee_id', 'date_of_birth', 'join_date', 'resign_date'])\n",
    "\n",
    "# Lakukan prediksi menggunakan model final\n",
    "y_pred = final_model.predict(X_test)\n",
    "\n",
    "# Tambahkan prediksi ke DataFrame\n",
    "X_test['predicted_active_work'] = y_pred\n",
    "\n",
    "# Hitung metrik evaluasi\n",
    "r2 = r2_score(df_test['active_work_months'], y_pred)\n",
    "mae = mean_absolute_error(df_test['active_work_months'], y_pred)\n",
    "mse = mean_squared_error(df_test['active_work_months'], y_pred)\n",
    "rmse = np.sqrt(mse)\n",
    "\n",
    "# Cetak hasil\n",
    "print(\"R² Score:\", r2)\n",
    "print(\"Mean Absolute Error (MAE):\", mae)\n",
    "print(\"Mean Squared Error (MSE):\", mse)\n",
    "print(\"Root Mean Squared Error (RMSE):\", rmse)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>employee_id</th>\n",
       "      <th>domisili</th>\n",
       "      <th>jenis_kelamin</th>\n",
       "      <th>date_of_birth</th>\n",
       "      <th>join_date</th>\n",
       "      <th>resign_date</th>\n",
       "      <th>marriage_stat</th>\n",
       "      <th>dependant</th>\n",
       "      <th>education</th>\n",
       "      <th>absent_90D</th>\n",
       "      <th>...</th>\n",
       "      <th>active_work_category</th>\n",
       "      <th>work_stability_score</th>\n",
       "      <th>married_dependent_ratio</th>\n",
       "      <th>position_score</th>\n",
       "      <th>job_income_position_score</th>\n",
       "      <th>education_score</th>\n",
       "      <th>education_income_ratio</th>\n",
       "      <th>weighted_satisfaction_performance</th>\n",
       "      <th>resign_risk_indicator</th>\n",
       "      <th>adjusted_work_time</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>EM13260</td>\n",
       "      <td>Kota Jakarta Barat</td>\n",
       "      <td>Perempuan</td>\n",
       "      <td>1976-12-02</td>\n",
       "      <td>2020-10-25</td>\n",
       "      <td>2023-02-16</td>\n",
       "      <td>Married</td>\n",
       "      <td>2</td>\n",
       "      <td>SLTA</td>\n",
       "      <td>8.0</td>\n",
       "      <td>...</td>\n",
       "      <td>Mid-term</td>\n",
       "      <td>3.111111</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2252793.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2.252793e+06</td>\n",
       "      <td>2.0</td>\n",
       "      <td>Medium</td>\n",
       "      <td>9.280456</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>EM0533</td>\n",
       "      <td>Tangerang</td>\n",
       "      <td>Laki-laki</td>\n",
       "      <td>1970-10-08</td>\n",
       "      <td>2022-10-05</td>\n",
       "      <td>2024-03-25</td>\n",
       "      <td>Married</td>\n",
       "      <td>3</td>\n",
       "      <td>SLTA</td>\n",
       "      <td>14.0</td>\n",
       "      <td>...</td>\n",
       "      <td>Mid-term</td>\n",
       "      <td>1.133333</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>1784520.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.784520e+06</td>\n",
       "      <td>1.8</td>\n",
       "      <td>Medium</td>\n",
       "      <td>9.809471</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>EM7296</td>\n",
       "      <td>Kota Depok</td>\n",
       "      <td>Perempuan</td>\n",
       "      <td>1980-05-10</td>\n",
       "      <td>2022-07-21</td>\n",
       "      <td>2023-09-01</td>\n",
       "      <td>Married</td>\n",
       "      <td>2</td>\n",
       "      <td>SLTA</td>\n",
       "      <td>14.0</td>\n",
       "      <td>...</td>\n",
       "      <td>Mid-term</td>\n",
       "      <td>0.866667</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1291410.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.291410e+06</td>\n",
       "      <td>1.6</td>\n",
       "      <td>Medium</td>\n",
       "      <td>9.534629</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>EM9032</td>\n",
       "      <td>Kota Depok</td>\n",
       "      <td>Perempuan</td>\n",
       "      <td>1993-10-24</td>\n",
       "      <td>2022-07-05</td>\n",
       "      <td>2024-01-25</td>\n",
       "      <td>Married</td>\n",
       "      <td>2</td>\n",
       "      <td>D2</td>\n",
       "      <td>7.0</td>\n",
       "      <td>...</td>\n",
       "      <td>Mid-term</td>\n",
       "      <td>2.250000</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3909283.0</td>\n",
       "      <td>3</td>\n",
       "      <td>1.303094e+06</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Medium</td>\n",
       "      <td>9.468933</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>EM11615</td>\n",
       "      <td>Tangerang</td>\n",
       "      <td>Laki-laki</td>\n",
       "      <td>1987-02-02</td>\n",
       "      <td>2022-09-14</td>\n",
       "      <td>2023-11-03</td>\n",
       "      <td>Divorce</td>\n",
       "      <td>0</td>\n",
       "      <td>SLTA</td>\n",
       "      <td>8.0</td>\n",
       "      <td>...</td>\n",
       "      <td>Mid-term</td>\n",
       "      <td>1.444444</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2615265.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2.615265e+06</td>\n",
       "      <td>2.2</td>\n",
       "      <td>Medium</td>\n",
       "      <td>8.968309</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 37 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "  employee_id            domisili jenis_kelamin date_of_birth   join_date  \\\n",
       "0     EM13260  Kota Jakarta Barat     Perempuan    1976-12-02  2020-10-25   \n",
       "1      EM0533           Tangerang     Laki-laki    1970-10-08  2022-10-05   \n",
       "2      EM7296          Kota Depok     Perempuan    1980-05-10  2022-07-21   \n",
       "3      EM9032          Kota Depok     Perempuan    1993-10-24  2022-07-05   \n",
       "4     EM11615           Tangerang     Laki-laki    1987-02-02  2022-09-14   \n",
       "\n",
       "  resign_date marriage_stat  dependant education  absent_90D  ...  \\\n",
       "0  2023-02-16       Married          2      SLTA         8.0  ...   \n",
       "1  2024-03-25       Married          3      SLTA        14.0  ...   \n",
       "2  2023-09-01       Married          2      SLTA        14.0  ...   \n",
       "3  2024-01-25       Married          2        D2         7.0  ...   \n",
       "4  2023-11-03       Divorce          0      SLTA         8.0  ...   \n",
       "\n",
       "   active_work_category work_stability_score married_dependent_ratio  \\\n",
       "0              Mid-term             3.111111                       3   \n",
       "1              Mid-term             1.133333                       4   \n",
       "2              Mid-term             0.866667                       3   \n",
       "3              Mid-term             2.250000                       3   \n",
       "4              Mid-term             1.444444                       1   \n",
       "\n",
       "   position_score  job_income_position_score education_score  \\\n",
       "0               1                  2252793.0               1   \n",
       "1               1                  1784520.0               1   \n",
       "2               1                  1291410.0               1   \n",
       "3               1                  3909283.0               3   \n",
       "4               1                  2615265.0               1   \n",
       "\n",
       "  education_income_ratio  weighted_satisfaction_performance  \\\n",
       "0           2.252793e+06                                2.0   \n",
       "1           1.784520e+06                                1.8   \n",
       "2           1.291410e+06                                1.6   \n",
       "3           1.303094e+06                                1.0   \n",
       "4           2.615265e+06                                2.2   \n",
       "\n",
       "   resign_risk_indicator  adjusted_work_time  \n",
       "0                 Medium            9.280456  \n",
       "1                 Medium            9.809471  \n",
       "2                 Medium            9.534629  \n",
       "3                 Medium            9.468933  \n",
       "4                 Medium            8.968309  \n",
       "\n",
       "[5 rows x 37 columns]"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_test = pd.read_csv('D:\\Tugas Akhir\\Codingan\\Development\\Data\\data_testing_resign.csv')\n",
    "df_test.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jumlah baris sebelum filter: 809\n",
      "Jumlah baris setelah filter: 809\n"
     ]
    }
   ],
   "source": [
    "# Memilih hanya kolom numerik\n",
    "numerical_columns = df_test.select_dtypes(include=['int64', 'float64']).columns\n",
    "\n",
    "# Filter: Hanya menyimpan baris yang tidak memiliki nilai negatif\n",
    "df_test_filtered = df_test[(df_test[numerical_columns] >= 0).all(axis=1)]\n",
    "\n",
    "# Menampilkan hasil\n",
    "print(\"Jumlah baris sebelum filter:\", df_test.shape[0])\n",
    "print(\"Jumlah baris setelah filter:\", df_test_filtered.shape[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "R² Score: 0.9982375650520496\n",
      "Mean Absolute Error (MAE): 0.2113292077085458\n",
      "Mean Squared Error (MSE): 0.08052087321650396\n",
      "Root Mean Squared Error (RMSE): 0.2837620010087749\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import r2_score, mean_absolute_error, mean_squared_error\n",
    "import numpy as np\n",
    "\n",
    "# Menghapus kolom yang tidak diperlukan untuk prediksi\n",
    "X_test = df_test.drop(columns=['active_work_months','churn_status', 'employee_id', 'date_of_birth', 'join_date', 'resign_date'])\n",
    "\n",
    "# Lakukan prediksi menggunakan model final\n",
    "y_pred = final_model.predict(X_test)\n",
    "\n",
    "# Tambahkan prediksi ke DataFrame\n",
    "X_test['predicted_active_work'] = y_pred\n",
    "\n",
    "# Hitung metrik evaluasi\n",
    "r2 = r2_score(df_test['active_work_months'], y_pred)\n",
    "mae = mean_absolute_error(df_test['active_work_months'], y_pred)\n",
    "mse = mean_squared_error(df_test['active_work_months'], y_pred)\n",
    "rmse = np.sqrt(mse)\n",
    "\n",
    "# Cetak hasil\n",
    "print(\"R² Score:\", r2)\n",
    "print(\"Mean Absolute Error (MAE):\", mae)\n",
    "print(\"Mean Squared Error (MSE):\", mse)\n",
    "print(\"Root Mean Squared Error (RMSE):\", rmse)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>employee_id</th>\n",
       "      <th>domisili</th>\n",
       "      <th>jenis_kelamin</th>\n",
       "      <th>date_of_birth</th>\n",
       "      <th>join_date</th>\n",
       "      <th>resign_date</th>\n",
       "      <th>marriage_stat</th>\n",
       "      <th>dependant</th>\n",
       "      <th>education</th>\n",
       "      <th>absent_90D</th>\n",
       "      <th>...</th>\n",
       "      <th>active_work_category</th>\n",
       "      <th>work_stability_score</th>\n",
       "      <th>married_dependent_ratio</th>\n",
       "      <th>position_score</th>\n",
       "      <th>job_income_position_score</th>\n",
       "      <th>education_score</th>\n",
       "      <th>education_income_ratio</th>\n",
       "      <th>weighted_satisfaction_performance</th>\n",
       "      <th>resign_risk_indicator</th>\n",
       "      <th>adjusted_work_time</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>EM0012</td>\n",
       "      <td>Tangerang</td>\n",
       "      <td>Laki-laki</td>\n",
       "      <td>1970-12-21</td>\n",
       "      <td>2023-02-23</td>\n",
       "      <td>2024-08-07</td>\n",
       "      <td>Married</td>\n",
       "      <td>3</td>\n",
       "      <td>D3</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>Mid-term</td>\n",
       "      <td>5.666667</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>4708861.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1.177215e+06</td>\n",
       "      <td>1.4</td>\n",
       "      <td>Medium</td>\n",
       "      <td>9.857106</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>EM0026</td>\n",
       "      <td>Kota Depok</td>\n",
       "      <td>Laki-laki</td>\n",
       "      <td>1986-11-14</td>\n",
       "      <td>2022-04-17</td>\n",
       "      <td>2024-08-04</td>\n",
       "      <td>Married</td>\n",
       "      <td>2</td>\n",
       "      <td>SLTA</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>Mid-term</td>\n",
       "      <td>5.600000</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1430853.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.430853e+06</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Medium</td>\n",
       "      <td>9.694593</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>EM0041</td>\n",
       "      <td>Kota Jakarta Barat</td>\n",
       "      <td>Laki-laki</td>\n",
       "      <td>1983-03-16</td>\n",
       "      <td>2023-06-15</td>\n",
       "      <td>2024-09-06</td>\n",
       "      <td>Divorce</td>\n",
       "      <td>3</td>\n",
       "      <td>SLTA</td>\n",
       "      <td>7.0</td>\n",
       "      <td>...</td>\n",
       "      <td>Mid-term</td>\n",
       "      <td>1.750000</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1379381.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.379381e+06</td>\n",
       "      <td>2.4</td>\n",
       "      <td>Medium</td>\n",
       "      <td>9.059429</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>EM0053</td>\n",
       "      <td>Kota Jakarta Timur</td>\n",
       "      <td>Laki-laki</td>\n",
       "      <td>1979-07-13</td>\n",
       "      <td>2023-07-11</td>\n",
       "      <td>2024-09-21</td>\n",
       "      <td>Single</td>\n",
       "      <td>0</td>\n",
       "      <td>SLTA</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>Mid-term</td>\n",
       "      <td>7.000000</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1911583.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.911583e+06</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Medium</td>\n",
       "      <td>9.842189</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>EM0057</td>\n",
       "      <td>Kota Jakarta Barat</td>\n",
       "      <td>Perempuan</td>\n",
       "      <td>2000-03-13</td>\n",
       "      <td>2022-07-14</td>\n",
       "      <td>2024-08-29</td>\n",
       "      <td>Single</td>\n",
       "      <td>0</td>\n",
       "      <td>D2</td>\n",
       "      <td>8.0</td>\n",
       "      <td>...</td>\n",
       "      <td>Mid-term</td>\n",
       "      <td>2.777778</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>3724157.0</td>\n",
       "      <td>3</td>\n",
       "      <td>1.241386e+06</td>\n",
       "      <td>2.0</td>\n",
       "      <td>Medium</td>\n",
       "      <td>9.047730</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 37 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "  employee_id            domisili jenis_kelamin date_of_birth   join_date  \\\n",
       "0      EM0012           Tangerang     Laki-laki    1970-12-21  2023-02-23   \n",
       "1      EM0026          Kota Depok     Laki-laki    1986-11-14  2022-04-17   \n",
       "2      EM0041  Kota Jakarta Barat     Laki-laki    1983-03-16  2023-06-15   \n",
       "3      EM0053  Kota Jakarta Timur     Laki-laki    1979-07-13  2023-07-11   \n",
       "4      EM0057  Kota Jakarta Barat     Perempuan    2000-03-13  2022-07-14   \n",
       "\n",
       "  resign_date marriage_stat  dependant education  absent_90D  ...  \\\n",
       "0  2024-08-07       Married          3        D3         2.0  ...   \n",
       "1  2024-08-04       Married          2      SLTA         4.0  ...   \n",
       "2  2024-09-06       Divorce          3      SLTA         7.0  ...   \n",
       "3  2024-09-21        Single          0      SLTA         1.0  ...   \n",
       "4  2024-08-29        Single          0        D2         8.0  ...   \n",
       "\n",
       "   active_work_category work_stability_score married_dependent_ratio  \\\n",
       "0              Mid-term             5.666667                       4   \n",
       "1              Mid-term             5.600000                       3   \n",
       "2              Mid-term             1.750000                       1   \n",
       "3              Mid-term             7.000000                       1   \n",
       "4              Mid-term             2.777778                       1   \n",
       "\n",
       "   position_score  job_income_position_score education_score  \\\n",
       "0               1                  4708861.0               4   \n",
       "1               1                  1430853.0               1   \n",
       "2               1                  1379381.0               1   \n",
       "3               1                  1911583.0               1   \n",
       "4               1                  3724157.0               3   \n",
       "\n",
       "  education_income_ratio  weighted_satisfaction_performance  \\\n",
       "0           1.177215e+06                                1.4   \n",
       "1           1.430853e+06                                1.0   \n",
       "2           1.379381e+06                                2.4   \n",
       "3           1.911583e+06                                1.0   \n",
       "4           1.241386e+06                                2.0   \n",
       "\n",
       "   resign_risk_indicator  adjusted_work_time  \n",
       "0                 Medium            9.857106  \n",
       "1                 Medium            9.694593  \n",
       "2                 Medium            9.059429  \n",
       "3                 Medium            9.842189  \n",
       "4                 Medium            9.047730  \n",
       "\n",
       "[5 rows x 37 columns]"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_test = pd.read_csv('D:\\Tugas Akhir\\Codingan\\Development\\Data\\data_testing_resign_6.csv')\n",
    "df_test.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "R² Score: 0.9983869290912244\n",
      "Mean Absolute Error (MAE): 0.18480799751480997\n",
      "Mean Squared Error (MSE): 0.06445121600267545\n",
      "Root Mean Squared Error (RMSE): 0.25387244041580304\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import r2_score, mean_absolute_error, mean_squared_error\n",
    "import numpy as np\n",
    "\n",
    "# Menghapus kolom yang tidak diperlukan untuk prediksi\n",
    "X_test = df_test.drop(columns=['active_work_months','churn_status', 'employee_id', 'date_of_birth', 'join_date', 'resign_date'])\n",
    "\n",
    "# Lakukan prediksi menggunakan model final\n",
    "y_pred = final_model.predict(X_test)\n",
    "\n",
    "# Tambahkan prediksi ke DataFrame\n",
    "X_test['predicted_active_work'] = y_pred\n",
    "\n",
    "# Hitung metrik evaluasi\n",
    "r2 = r2_score(df_test['active_work_months'], y_pred)\n",
    "mae = mean_absolute_error(df_test['active_work_months'], y_pred)\n",
    "mse = mean_squared_error(df_test['active_work_months'], y_pred)\n",
    "rmse = np.sqrt(mse)\n",
    "\n",
    "# Cetak hasil\n",
    "print(\"R² Score:\", r2)\n",
    "print(\"Mean Absolute Error (MAE):\", mae)\n",
    "print(\"Mean Squared Error (MSE):\", mse)\n",
    "print(\"Root Mean Squared Error (RMSE):\", rmse)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 58584 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 152140 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "\n",
    "# Nilai aktual dan prediksi\n",
    "y_actual = df_test['active_work_months']  # Nilai aktual dari active_work_months\n",
    "y_pred = X_test['predicted_active_work']  # Nilai prediksi dari model regresi\n",
    "\n",
    "# Membuat DataFrame untuk mempermudah visualisasi\n",
    "comparison_df = pd.DataFrame({'Actual': y_actual, 'Predicted': y_pred})\n",
    "\n",
    "# Scatter plot untuk membandingkan prediksi dan nilai asli\n",
    "plt.figure(figsize=(10, 6))\n",
    "plt.scatter(range(len(y_actual)), y_actual, label='Actual Values', alpha=0.7, color='blue')\n",
    "plt.scatter(range(len(y_pred)), y_pred, label='Predicted Values', alpha=0.7, color='orange')\n",
    "plt.title('Comparison of Actual vs Predicted Active Work Months')\n",
    "plt.xlabel('Index')\n",
    "plt.ylabel('Active Work Months')\n",
    "plt.legend()\n",
    "plt.grid(True)\n",
    "plt.show()\n",
    "\n",
    "# Line plot untuk membandingkan prediksi dan nilai asli\n",
    "plt.figure(figsize=(10, 6))\n",
    "plt.plot(range(len(y_actual)), y_actual, label='Actual Values', marker='o', linestyle='-', color='blue')\n",
    "plt.plot(range(len(y_pred)), y_pred, label='Predicted Values', marker='x', linestyle='-', color='orange')\n",
    "plt.title('Actual vs Predicted Active Work Months (Line Plot)')\n",
    "plt.xlabel('Index')\n",
    "plt.ylabel('Active Work Months')\n",
    "plt.legend()\n",
    "plt.grid(True)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CatBoost Regression model saved to 'regression_model.sav'\n"
     ]
    }
   ],
   "source": [
    "import pickle\n",
    "\n",
    "with open('regression_model.sav', 'wb') as f:\n",
    "    pickle.dump(final_model, f)\n",
    "print(\"CatBoost Regression model saved to 'regression_model.sav'\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting shap\n",
      "  Obtaining dependency information for shap from https://files.pythonhosted.org/packages/02/48/033ab9a2dee26d3de7e57cf532ab1d8408a608544c85ff98e6ea65775bdf/shap-0.46.0-cp39-cp39-win_amd64.whl.metadata\n",
      "  Downloading shap-0.46.0-cp39-cp39-win_amd64.whl.metadata (25 kB)\n",
      "Requirement already satisfied: numpy in c:\\users\\jesselyn mu\\anaconda3\\lib\\site-packages (from shap) (1.22.4)\n",
      "Requirement already satisfied: scipy in c:\\users\\jesselyn mu\\anaconda3\\lib\\site-packages (from shap) (1.9.3)\n",
      "Requirement already satisfied: scikit-learn in c:\\users\\jesselyn mu\\anaconda3\\lib\\site-packages (from shap) (1.0.2)\n",
      "Requirement already satisfied: pandas in c:\\users\\jesselyn mu\\anaconda3\\lib\\site-packages (from shap) (1.4.2)\n",
      "Requirement already satisfied: tqdm>=4.27.0 in c:\\users\\jesselyn mu\\anaconda3\\lib\\site-packages (from shap) (4.65.0)\n",
      "Requirement already satisfied: packaging>20.9 in c:\\users\\jesselyn mu\\anaconda3\\lib\\site-packages (from shap) (23.1)\n",
      "Collecting slicer==0.0.8 (from shap)\n",
      "  Obtaining dependency information for slicer==0.0.8 from https://files.pythonhosted.org/packages/63/81/9ef641ff4e12cbcca30e54e72fb0951a2ba195d0cda0ba4100e532d929db/slicer-0.0.8-py3-none-any.whl.metadata\n",
      "  Downloading slicer-0.0.8-py3-none-any.whl.metadata (4.0 kB)\n",
      "Requirement already satisfied: numba in c:\\users\\jesselyn mu\\anaconda3\\lib\\site-packages (from shap) (0.58.1)\n",
      "Requirement already satisfied: cloudpickle in c:\\users\\jesselyn mu\\anaconda3\\lib\\site-packages (from shap) (1.6.0)\n",
      "Requirement already satisfied: colorama in c:\\users\\jesselyn mu\\anaconda3\\lib\\site-packages (from tqdm>=4.27.0->shap) (0.4.6)\n",
      "Requirement already satisfied: llvmlite<0.42,>=0.41.0dev0 in c:\\users\\jesselyn mu\\anaconda3\\lib\\site-packages (from numba->shap) (0.41.0)\n",
      "Requirement already satisfied: python-dateutil>=2.8.1 in c:\\users\\jesselyn mu\\anaconda3\\lib\\site-packages (from pandas->shap) (2.8.2)\n",
      "Requirement already satisfied: pytz>=2020.1 in c:\\users\\jesselyn mu\\anaconda3\\lib\\site-packages (from pandas->shap) (2023.3.post1)\n",
      "Requirement already satisfied: joblib>=0.11 in c:\\users\\jesselyn mu\\anaconda3\\lib\\site-packages (from scikit-learn->shap) (1.2.0)\n",
      "Requirement already satisfied: threadpoolctl>=2.0.0 in c:\\users\\jesselyn mu\\anaconda3\\lib\\site-packages (from scikit-learn->shap) (2.2.0)\n",
      "Requirement already satisfied: six>=1.5 in c:\\users\\jesselyn mu\\anaconda3\\lib\\site-packages (from python-dateutil>=2.8.1->pandas->shap) (1.16.0)\n",
      "Downloading shap-0.46.0-cp39-cp39-win_amd64.whl (456 kB)\n",
      "   ---------------------------------------- 0.0/456.1 kB ? eta -:--:--\n",
      "    --------------------------------------- 10.2/456.1 kB ? eta -:--:--\n",
      "   --------- ------------------------------ 112.6/456.1 kB 1.6 MB/s eta 0:00:01\n",
      "   ------------------------------------ --- 419.8/456.1 kB 3.8 MB/s eta 0:00:01\n",
      "   ---------------------------------------- 456.1/456.1 kB 3.6 MB/s eta 0:00:00\n",
      "Downloading slicer-0.0.8-py3-none-any.whl (15 kB)\n",
      "Installing collected packages: slicer, shap\n",
      "Successfully installed shap-0.46.0 slicer-0.0.8\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: Ignoring invalid distribution -pencv-python (c:\\users\\jesselyn mu\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -treamlit (c:\\users\\jesselyn mu\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -pencv-python (c:\\users\\jesselyn mu\\anaconda3\\lib\\site-packages)\n",
      "WARNING: Ignoring invalid distribution -treamlit (c:\\users\\jesselyn mu\\anaconda3\\lib\\site-packages)\n"
     ]
    }
   ],
   "source": [
    "%pip install shap"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "authorship_tag": "ABX9TyNQ761UqVawGErIP7JLgmhK",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "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.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
