{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "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>EM10510</td>\n",
       "      <td>Kota Jakarta Utara</td>\n",
       "      <td>Laki-laki</td>\n",
       "      <td>1983-09-11</td>\n",
       "      <td>2021-02-09</td>\n",
       "      <td>2023-06-22</td>\n",
       "      <td>Married</td>\n",
       "      <td>1</td>\n",
       "      <td>SLTA</td>\n",
       "      <td>9.0</td>\n",
       "      <td>...</td>\n",
       "      <td>Mid-term</td>\n",
       "      <td>2.800000</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1418218.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.418218e+06</td>\n",
       "      <td>2.2</td>\n",
       "      <td>Medium</td>\n",
       "      <td>9.246870</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>EM4322</td>\n",
       "      <td>Kabupaten Bekasi</td>\n",
       "      <td>Perempuan</td>\n",
       "      <td>1987-03-22</td>\n",
       "      <td>2022-02-28</td>\n",
       "      <td>2023-04-04</td>\n",
       "      <td>Married</td>\n",
       "      <td>1</td>\n",
       "      <td>SLTA</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>Mid-term</td>\n",
       "      <td>13.000000</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1060575.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.060575e+06</td>\n",
       "      <td>1.6</td>\n",
       "      <td>Medium</td>\n",
       "      <td>9.650000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>EM1637</td>\n",
       "      <td>Kota Jakarta Barat</td>\n",
       "      <td>Laki-laki</td>\n",
       "      <td>1970-04-27</td>\n",
       "      <td>2020-12-23</td>\n",
       "      <td>2023-03-25</td>\n",
       "      <td>Married</td>\n",
       "      <td>4</td>\n",
       "      <td>D2</td>\n",
       "      <td>4.0</td>\n",
       "      <td>...</td>\n",
       "      <td>Mid-term</td>\n",
       "      <td>5.400000</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>4885136.0</td>\n",
       "      <td>3</td>\n",
       "      <td>1.628379e+06</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Medium</td>\n",
       "      <td>9.813826</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>EM14613</td>\n",
       "      <td>Kota Jakarta Pusat</td>\n",
       "      <td>Laki-laki</td>\n",
       "      <td>1988-06-10</td>\n",
       "      <td>2022-11-21</td>\n",
       "      <td>2024-03-23</td>\n",
       "      <td>Married</td>\n",
       "      <td>1</td>\n",
       "      <td>D3</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>Mid-term</td>\n",
       "      <td>5.333333</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>4602479.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1.150620e+06</td>\n",
       "      <td>2.4</td>\n",
       "      <td>Medium</td>\n",
       "      <td>9.756440</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>EM1084</td>\n",
       "      <td>Kabupaten Bogor</td>\n",
       "      <td>Perempuan</td>\n",
       "      <td>1977-05-25</td>\n",
       "      <td>2021-06-07</td>\n",
       "      <td>2023-07-21</td>\n",
       "      <td>Married</td>\n",
       "      <td>3</td>\n",
       "      <td>SLTA</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>Mid-term</td>\n",
       "      <td>25.000000</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>1066966.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.066966e+06</td>\n",
       "      <td>2.6</td>\n",
       "      <td>Medium</td>\n",
       "      <td>9.080000</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     EM10510  Kota Jakarta Utara     Laki-laki    1983-09-11  2021-02-09   \n",
       "1      EM4322    Kabupaten Bekasi     Perempuan    1987-03-22  2022-02-28   \n",
       "2      EM1637  Kota Jakarta Barat     Laki-laki    1970-04-27  2020-12-23   \n",
       "3     EM14613  Kota Jakarta Pusat     Laki-laki    1988-06-10  2022-11-21   \n",
       "4      EM1084     Kabupaten Bogor     Perempuan    1977-05-25  2021-06-07   \n",
       "\n",
       "  resign_date marriage_stat  dependant education  absent_90D  ...  \\\n",
       "0  2023-06-22       Married          1      SLTA         9.0  ...   \n",
       "1  2023-04-04       Married          1      SLTA         0.0  ...   \n",
       "2  2023-03-25       Married          4        D2         4.0  ...   \n",
       "3  2024-03-23       Married          1        D3         2.0  ...   \n",
       "4  2023-07-21       Married          3      SLTA         0.0  ...   \n",
       "\n",
       "   active_work_category work_stability_score married_dependent_ratio  \\\n",
       "0              Mid-term             2.800000                       2   \n",
       "1              Mid-term            13.000000                       2   \n",
       "2              Mid-term             5.400000                       5   \n",
       "3              Mid-term             5.333333                       2   \n",
       "4              Mid-term            25.000000                       4   \n",
       "\n",
       "   position_score  job_income_position_score education_score  \\\n",
       "0               1                  1418218.0               1   \n",
       "1               1                  1060575.0               1   \n",
       "2               1                  4885136.0               3   \n",
       "3               1                  4602479.0               4   \n",
       "4               1                  1066966.0               1   \n",
       "\n",
       "  education_income_ratio  weighted_satisfaction_performance  \\\n",
       "0           1.418218e+06                                2.2   \n",
       "1           1.060575e+06                                1.6   \n",
       "2           1.628379e+06                                1.0   \n",
       "3           1.150620e+06                                2.4   \n",
       "4           1.066966e+06                                2.6   \n",
       "\n",
       "   resign_risk_indicator  adjusted_work_time  \n",
       "0                 Medium            9.246870  \n",
       "1                 Medium            9.650000  \n",
       "2                 Medium            9.813826  \n",
       "3                 Medium            9.756440  \n",
       "4                 Medium            9.080000  \n",
       "\n",
       "[5 rows x 37 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "df = pd.read_csv('preprocessed_data_train_3.csv')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "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": 4,
   "metadata": {
    "id": "qT7W_7qbUmtZ"
   },
   "outputs": [],
   "source": [
    "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",
    "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": 5,
   "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.5942904\ttest: 23.3418347\tbest: 23.3418347 (0)\ttotal: 191ms\tremaining: 3m 10s\n",
      "200:\tlearn: 2.2250306\ttest: 5.0068018\tbest: 5.0068018 (200)\ttotal: 8.01s\tremaining: 31.8s\n",
      "400:\tlearn: 0.5799503\ttest: 2.2619679\tbest: 2.2619679 (400)\ttotal: 16.9s\tremaining: 25.2s\n",
      "600:\tlearn: 0.3616911\ttest: 1.7906967\tbest: 1.7906967 (600)\ttotal: 25.9s\tremaining: 17.2s\n",
      "800:\tlearn: 0.3091173\ttest: 1.6207404\tbest: 1.6207404 (800)\ttotal: 34.6s\tremaining: 8.6s\n",
      "999:\tlearn: 0.2895548\ttest: 1.5407851\tbest: 1.5407851 (999)\ttotal: 43s\tremaining: 0us\n",
      "\n",
      "bestTest = 1.540785132\n",
      "bestIteration = 999\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<catboost.core.CatBoostRegressor at 0x1b653161810>"
      ]
     },
     "execution_count": 5,
     "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.374019</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>MAE</th>\n",
       "      <td>0.902642</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>RMSE</th>\n",
       "      <td>1.540785</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>R2 Score</th>\n",
       "      <td>0.925505</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             Score\n",
       "MSE       2.374019\n",
       "MAE       0.902642\n",
       "RMSE      1.540785\n",
       "R2 Score  0.925505"
      ]
     },
     "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, 41536 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 82272 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": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[I 2025-01-31 22:03:50,597] A new study created in memory with name: no-name-474216de-e104-4659-8f4c-89a9d348bbe2\n",
      "[I 2025-01-31 22:04:09,058] Trial 0 finished with value: 11.852841151612708 and parameters: {'iterations': 759, 'learning_rate': 0.0014964661109911665, 'depth': 4, 'subsample': 0.5345403763007321, 'colsample_bylevel': 0.5557879487166669, 'l2_leaf_reg': 14.523593619112225, 'random_strength': 5.436136126384264}. Best is trial 0 with value: 11.852841151612708.\n",
      "[I 2025-01-31 22:04:26,185] Trial 1 finished with value: 2.782326827024678 and parameters: {'iterations': 631, 'learning_rate': 0.0688926002637438, 'depth': 4, 'subsample': 0.5589286667267055, 'colsample_bylevel': 0.5102559732260701, 'l2_leaf_reg': 12.03344970062869, 'random_strength': 7.74554502548946}. Best is trial 1 with value: 2.782326827024678.\n",
      "[I 2025-01-31 22:04:40,370] Trial 2 finished with value: 6.727048715765419 and parameters: {'iterations': 543, 'learning_rate': 0.004111528098062327, 'depth': 4, 'subsample': 0.7394617829547927, 'colsample_bylevel': 0.6796950909515516, 'l2_leaf_reg': 14.783270804681484, 'random_strength': 6.010320137030617}. Best is trial 1 with value: 2.782326827024678.\n",
      "[I 2025-01-31 22:05:06,114] Trial 3 finished with value: 4.690053329511069 and parameters: {'iterations': 962, 'learning_rate': 0.004328088977730701, 'depth': 4, 'subsample': 0.613225875921462, 'colsample_bylevel': 0.7077028189537486, 'l2_leaf_reg': 5.0512639148135055, 'random_strength': 8.213889453763334}. Best is trial 1 with value: 2.782326827024678.\n",
      "[I 2025-01-31 22:05:37,726] Trial 4 finished with value: 3.7838313230344505 and parameters: {'iterations': 809, 'learning_rate': 0.011313129007769103, 'depth': 6, 'subsample': 0.5016261860337349, 'colsample_bylevel': 0.5766456136058663, 'l2_leaf_reg': 5.998060972309887, 'random_strength': 6.018890370319019}. Best is trial 1 with value: 2.782326827024678.\n",
      "[I 2025-01-31 22:06:02,736] Trial 5 finished with value: 4.852253440694461 and parameters: {'iterations': 748, 'learning_rate': 0.00520517267258286, 'depth': 5, 'subsample': 0.5485566035315403, 'colsample_bylevel': 0.5337409295485034, 'l2_leaf_reg': 14.713809483161187, 'random_strength': 5.279249030163157}. Best is trial 1 with value: 2.782326827024678.\n",
      "[I 2025-01-31 22:06:23,820] Trial 6 finished with value: 3.8733461927022397 and parameters: {'iterations': 694, 'learning_rate': 0.009239979129411872, 'depth': 4, 'subsample': 0.688277277261536, 'colsample_bylevel': 0.6620877160267076, 'l2_leaf_reg': 11.460620744541892, 'random_strength': 6.934032197250438}. Best is trial 1 with value: 2.782326827024678.\n",
      "[I 2025-01-31 22:07:03,131] Trial 7 finished with value: 3.2239910016287734 and parameters: {'iterations': 918, 'learning_rate': 0.031176473967143774, 'depth': 6, 'subsample': 0.6860333334924864, 'colsample_bylevel': 0.7839197568184811, 'l2_leaf_reg': 14.496727744464716, 'random_strength': 7.6124177760405765}. Best is trial 1 with value: 2.782326827024678.\n",
      "[I 2025-01-31 22:07:39,702] Trial 8 finished with value: 2.3342054540264265 and parameters: {'iterations': 831, 'learning_rate': 0.04518694466082468, 'depth': 6, 'subsample': 0.5547145239784439, 'colsample_bylevel': 0.6605692487332455, 'l2_leaf_reg': 5.931089190499905, 'random_strength': 9.381970152240088}. Best is trial 8 with value: 2.3342054540264265.\n",
      "[I 2025-01-31 22:08:15,167] Trial 9 finished with value: 6.898961698774608 and parameters: {'iterations': 801, 'learning_rate': 0.0030466204092430872, 'depth': 6, 'subsample': 0.5885034372327637, 'colsample_bylevel': 0.7795385742828266, 'l2_leaf_reg': 8.36924405524966, 'random_strength': 7.924454636979776}. Best is trial 8 with value: 2.3342054540264265.\n",
      "[I 2025-01-31 22:08:44,351] Trial 10 finished with value: 2.813059906292731 and parameters: {'iterations': 869, 'learning_rate': 0.09506915985958232, 'depth': 5, 'subsample': 0.773053102270278, 'colsample_bylevel': 0.6098195826696574, 'l2_leaf_reg': 19.907766421634943, 'random_strength': 9.961512860567842}. Best is trial 8 with value: 2.3342054540264265.\n",
      "[I 2025-01-31 22:09:03,895] Trial 11 finished with value: 2.368947282806889 and parameters: {'iterations': 616, 'learning_rate': 0.09490011276218288, 'depth': 5, 'subsample': 0.5879129821636959, 'colsample_bylevel': 0.5114011291482327, 'l2_leaf_reg': 9.74540222842541, 'random_strength': 9.118958428692489}. Best is trial 8 with value: 2.3342054540264265.\n",
      "[I 2025-01-31 22:09:20,976] Trial 12 finished with value: 2.9492933999301365 and parameters: {'iterations': 538, 'learning_rate': 0.03734917543038907, 'depth': 5, 'subsample': 0.634101535755758, 'colsample_bylevel': 0.6149087607795556, 'l2_leaf_reg': 8.754851646806793, 'random_strength': 9.52252352465122}. Best is trial 8 with value: 2.3342054540264265.\n",
      "[I 2025-01-31 22:09:46,193] Trial 13 finished with value: 3.065532855429015 and parameters: {'iterations': 630, 'learning_rate': 0.038017280754407835, 'depth': 6, 'subsample': 0.5930689989662581, 'colsample_bylevel': 0.7302958917359129, 'l2_leaf_reg': 8.836162641729628, 'random_strength': 8.981981072468267}. Best is trial 8 with value: 2.3342054540264265.\n",
      "[I 2025-01-31 22:10:11,617] Trial 14 finished with value: 3.3908649932569825 and parameters: {'iterations': 628, 'learning_rate': 0.019178482067712114, 'depth': 5, 'subsample': 0.668068547462943, 'colsample_bylevel': 0.6232205705636187, 'l2_leaf_reg': 7.173889636634161, 'random_strength': 8.820311100011175}. Best is trial 8 with value: 2.3342054540264265.\n",
      "[I 2025-01-31 22:10:39,432] Trial 15 finished with value: 2.9044988796143985 and parameters: {'iterations': 869, 'learning_rate': 0.06260333828159473, 'depth': 5, 'subsample': 0.5010675524635919, 'colsample_bylevel': 0.5004056047202005, 'l2_leaf_reg': 10.229894565431731, 'random_strength': 8.816149945416772}. Best is trial 8 with value: 2.3342054540264265.\n",
      "[I 2025-01-31 22:11:00,589] Trial 16 finished with value: 3.725099096600906 and parameters: {'iterations': 501, 'learning_rate': 0.09429316501260981, 'depth': 6, 'subsample': 0.5698961859044438, 'colsample_bylevel': 0.58173768320992, 'l2_leaf_reg': 6.5820739952329905, 'random_strength': 9.533926389710716}. Best is trial 8 with value: 2.3342054540264265.\n",
      "[I 2025-01-31 22:11:23,980] Trial 17 finished with value: 2.722341758877473 and parameters: {'iterations': 686, 'learning_rate': 0.023070272594262208, 'depth': 5, 'subsample': 0.6317324050504781, 'colsample_bylevel': 0.72562237372333, 'l2_leaf_reg': 10.240074501827097, 'random_strength': 6.92600047428353}. Best is trial 8 with value: 2.3342054540264265.\n",
      "[I 2025-01-31 22:11:56,050] Trial 18 finished with value: 2.6781381800641975 and parameters: {'iterations': 996, 'learning_rate': 0.05258897533675518, 'depth': 5, 'subsample': 0.5231598132903472, 'colsample_bylevel': 0.6477077001044423, 'l2_leaf_reg': 18.592000812751706, 'random_strength': 8.344932947613101}. Best is trial 8 with value: 2.3342054540264265.\n",
      "[I 2025-01-31 22:12:18,385] Trial 19 finished with value: 3.4976580765293415 and parameters: {'iterations': 584, 'learning_rate': 0.019380508698435384, 'depth': 6, 'subsample': 0.5836852243872083, 'colsample_bylevel': 0.6877774946763483, 'l2_leaf_reg': 7.57681462211325, 'random_strength': 9.432363107129735}. Best is trial 8 with value: 2.3342054540264265.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Best trial:\n",
      "  RMSE: 2.3342054540264265\n",
      "  Params: {'iterations': 831, 'learning_rate': 0.04518694466082468, 'depth': 6, 'subsample': 0.5547145239784439, 'colsample_bylevel': 0.6605692487332455, 'l2_leaf_reg': 5.931089190499905, 'random_strength': 9.381970152240088}\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 = np.sqrt(mean_squared_error(y_valid, y_pred))\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": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0:\tlearn: 13.2553205\ttest: 22.9596935\tbest: 22.9596935 (0)\ttotal: 33.4ms\tremaining: 27.7s\n",
      "200:\tlearn: 0.9396532\ttest: 3.4434437\tbest: 3.4434437 (200)\ttotal: 7.79s\tremaining: 24.4s\n",
      "400:\tlearn: 0.3768311\ttest: 2.5350476\tbest: 2.5350476 (400)\ttotal: 18.5s\tremaining: 19.8s\n",
      "600:\tlearn: 0.3062632\ttest: 2.4067533\tbest: 2.4067533 (599)\ttotal: 28s\tremaining: 10.7s\n",
      "800:\tlearn: 0.2733607\ttest: 2.3423564\tbest: 2.3423564 (800)\ttotal: 37s\tremaining: 1.39s\n",
      "830:\tlearn: 0.2690607\ttest: 2.3342055\tbest: 2.3342055 (830)\ttotal: 38.8s\tremaining: 0us\n",
      "\n",
      "bestTest = 2.334205454\n",
      "bestIteration = 830\n",
      "\n",
      "Final RMSE: 2.3342054540264265\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 = np.sqrt(mean_squared_error(y_valid, y_pred))  # Hitung RMSE\n",
    "print(f\"Final RMSE: {final_rmse}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 40856 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 84192 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.28955484233140216\n",
      "Final Validation RMSE: 1.5407851315191226\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 38224 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": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Final Training RMSE: 0.2690606507832273\n",
      "Final Validation RMSE: 2.334205454026432\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 35452 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": 15,
   "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>5.448515</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>MAE</th>\n",
       "      <td>1.116134</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>RMSE</th>\n",
       "      <td>2.334205</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>R2 Score</th>\n",
       "      <td>0.829029</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             Score\n",
       "MSE       5.448515\n",
       "MAE       1.116134\n",
       "RMSE      2.334205\n",
       "R2 Score  0.829029"
      ]
     },
     "execution_count": 15,
     "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": 16,
   "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": 17,
   "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": 18,
   "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": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0:\tlearn: 9.1646718\ttotal: 29.5ms\tremaining: 29.4s\n",
      "200:\tlearn: 1.8381351\ttotal: 6.45s\tremaining: 25.7s\n",
      "400:\tlearn: 0.6192440\ttotal: 12.9s\tremaining: 19.3s\n",
      "600:\tlearn: 0.4141800\ttotal: 19.9s\tremaining: 13.2s\n",
      "800:\tlearn: 0.3521180\ttotal: 27.1s\tremaining: 6.74s\n",
      "999:\tlearn: 0.3155382\ttotal: 35.7s\tremaining: 0us\n",
      "0:\tlearn: 9.8938338\ttotal: 41.5ms\tremaining: 41.4s\n",
      "200:\tlearn: 1.8182014\ttotal: 7.76s\tremaining: 30.8s\n",
      "400:\tlearn: 0.5997486\ttotal: 15s\tremaining: 22.4s\n",
      "600:\tlearn: 0.4063431\ttotal: 22.2s\tremaining: 14.7s\n",
      "800:\tlearn: 0.3487810\ttotal: 30.2s\tremaining: 7.51s\n",
      "999:\tlearn: 0.3168919\ttotal: 38s\tremaining: 0us\n",
      "0:\tlearn: 11.9438216\ttotal: 34.6ms\tremaining: 34.6s\n",
      "200:\tlearn: 2.0373408\ttotal: 8.97s\tremaining: 35.7s\n",
      "400:\tlearn: 0.5734042\ttotal: 18.7s\tremaining: 27.9s\n",
      "600:\tlearn: 0.3694254\ttotal: 28.3s\tremaining: 18.8s\n",
      "800:\tlearn: 0.3167774\ttotal: 37.7s\tremaining: 9.38s\n",
      "999:\tlearn: 0.2925512\ttotal: 47.9s\tremaining: 0us\n",
      "0:\tlearn: 13.7420148\ttotal: 47.3ms\tremaining: 47.2s\n",
      "200:\tlearn: 2.2489494\ttotal: 11.4s\tremaining: 45.2s\n",
      "400:\tlearn: 0.5929080\ttotal: 23s\tremaining: 34.3s\n",
      "600:\tlearn: 0.3608850\ttotal: 33.3s\tremaining: 22.1s\n",
      "800:\tlearn: 0.3051274\ttotal: 44s\tremaining: 10.9s\n",
      "999:\tlearn: 0.2856118\ttotal: 55.1s\tremaining: 0us\n",
      "0:\tlearn: 14.7940073\ttotal: 55.6ms\tremaining: 55.6s\n",
      "200:\tlearn: 2.3902581\ttotal: 11s\tremaining: 43.8s\n",
      "400:\tlearn: 0.6209255\ttotal: 22s\tremaining: 32.9s\n",
      "600:\tlearn: 0.3622676\ttotal: 33.4s\tremaining: 22.2s\n",
      "800:\tlearn: 0.3017520\ttotal: 46s\tremaining: 11.4s\n",
      "999:\tlearn: 0.2791322\ttotal: 58.3s\tremaining: 0us\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 37700 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.98369199 0.9882848  0.99649707 0.99211623 0.80882796]\n",
      "Mean R²: 0.95\n",
      "Standard Deviation: 0.07\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": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0:\tlearn: 9.0145461\ttotal: 10.6ms\tremaining: 8.78s\n",
      "200:\tlearn: 1.2582659\ttotal: 4.91s\tremaining: 15.4s\n",
      "400:\tlearn: 0.4363778\ttotal: 11s\tremaining: 11.8s\n",
      "600:\tlearn: 0.3126921\ttotal: 18.7s\tremaining: 7.15s\n",
      "800:\tlearn: 0.2545572\ttotal: 25.5s\tremaining: 957ms\n",
      "830:\tlearn: 0.2481421\ttotal: 26.5s\tremaining: 0us\n",
      "0:\tlearn: 9.6128998\ttotal: 44ms\tremaining: 36.5s\n",
      "200:\tlearn: 0.8781166\ttotal: 6.74s\tremaining: 21.1s\n",
      "400:\tlearn: 0.4015050\ttotal: 15.2s\tremaining: 16.3s\n",
      "600:\tlearn: 0.3090510\ttotal: 24.2s\tremaining: 9.27s\n",
      "800:\tlearn: 0.2592984\ttotal: 31.9s\tremaining: 1.2s\n",
      "830:\tlearn: 0.2535358\ttotal: 33.1s\tremaining: 0us\n",
      "0:\tlearn: 11.7063627\ttotal: 17.1ms\tremaining: 14.2s\n",
      "200:\tlearn: 0.9246313\ttotal: 7.73s\tremaining: 24.2s\n",
      "400:\tlearn: 0.4010091\ttotal: 16.9s\tremaining: 18.2s\n",
      "600:\tlearn: 0.3216253\ttotal: 26.5s\tremaining: 10.1s\n",
      "800:\tlearn: 0.2779483\ttotal: 35.2s\tremaining: 1.32s\n",
      "830:\tlearn: 0.2736423\ttotal: 36.6s\tremaining: 0us\n",
      "0:\tlearn: 13.3519464\ttotal: 46.7ms\tremaining: 38.7s\n",
      "200:\tlearn: 0.9643347\ttotal: 8.18s\tremaining: 25.6s\n",
      "400:\tlearn: 0.3782954\ttotal: 18.7s\tremaining: 20s\n",
      "600:\tlearn: 0.3032669\ttotal: 29.7s\tremaining: 11.3s\n",
      "800:\tlearn: 0.2713702\ttotal: 39.4s\tremaining: 1.47s\n",
      "830:\tlearn: 0.2671092\ttotal: 40.7s\tremaining: 0us\n",
      "0:\tlearn: 14.3572371\ttotal: 37.2ms\tremaining: 30.9s\n",
      "200:\tlearn: 0.8697156\ttotal: 8.57s\tremaining: 26.9s\n",
      "400:\tlearn: 0.3724428\ttotal: 18s\tremaining: 19.3s\n",
      "600:\tlearn: 0.3016009\ttotal: 28s\tremaining: 10.7s\n",
      "800:\tlearn: 0.2724626\ttotal: 37.7s\tremaining: 1.41s\n",
      "830:\tlearn: 0.2678372\ttotal: 39.1s\tremaining: 0us\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 39564 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.95016289 0.96477909 0.99462171 0.97371542 0.38662643]\n",
      "Mean R²: 0.85\n",
      "Standard Deviation: 0.23\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": 21,
   "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>EM0406</td>\n",
       "      <td>Kota Jakarta Utara</td>\n",
       "      <td>Laki-laki</td>\n",
       "      <td>1975-01-07</td>\n",
       "      <td>2021-09-25</td>\n",
       "      <td>2023-12-07</td>\n",
       "      <td>Married</td>\n",
       "      <td>3</td>\n",
       "      <td>SLTA</td>\n",
       "      <td>3.0</td>\n",
       "      <td>...</td>\n",
       "      <td>Mid-term</td>\n",
       "      <td>6.500000</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>1578410.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1578410.00</td>\n",
       "      <td>1.0</td>\n",
       "      <td>Medium</td>\n",
       "      <td>9.797428</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>EM1772</td>\n",
       "      <td>Kabupaten Bogor</td>\n",
       "      <td>Perempuan</td>\n",
       "      <td>1993-04-18</td>\n",
       "      <td>2021-02-23</td>\n",
       "      <td>2023-06-24</td>\n",
       "      <td>Married</td>\n",
       "      <td>1</td>\n",
       "      <td>SLTA</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>Mid-term</td>\n",
       "      <td>9.333333</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2003154.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2003154.00</td>\n",
       "      <td>2.2</td>\n",
       "      <td>Medium</td>\n",
       "      <td>9.342582</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>EM7996</td>\n",
       "      <td>Tangerang</td>\n",
       "      <td>Laki-laki</td>\n",
       "      <td>1998-02-12</td>\n",
       "      <td>2023-05-04</td>\n",
       "      <td>2024-06-29</td>\n",
       "      <td>Single</td>\n",
       "      <td>0</td>\n",
       "      <td>SLTA</td>\n",
       "      <td>5.0</td>\n",
       "      <td>...</td>\n",
       "      <td>Mid-term</td>\n",
       "      <td>2.333333</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1394384.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1394384.00</td>\n",
       "      <td>3.0</td>\n",
       "      <td>Medium</td>\n",
       "      <td>9.551975</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>EM13978</td>\n",
       "      <td>Kota Jakarta Barat</td>\n",
       "      <td>Perempuan</td>\n",
       "      <td>1982-12-26</td>\n",
       "      <td>2021-09-11</td>\n",
       "      <td>2023-04-03</td>\n",
       "      <td>Married</td>\n",
       "      <td>0</td>\n",
       "      <td>D3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>Mid-term</td>\n",
       "      <td>18.000000</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>4151999.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1037999.75</td>\n",
       "      <td>2.6</td>\n",
       "      <td>Medium</td>\n",
       "      <td>9.180000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>EM9860</td>\n",
       "      <td>Tangerang</td>\n",
       "      <td>Perempuan</td>\n",
       "      <td>1997-03-26</td>\n",
       "      <td>2023-06-20</td>\n",
       "      <td>2024-10-02</td>\n",
       "      <td>Single</td>\n",
       "      <td>0</td>\n",
       "      <td>SLTA</td>\n",
       "      <td>15.0</td>\n",
       "      <td>...</td>\n",
       "      <td>Mid-term</td>\n",
       "      <td>0.937500</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1560817.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1560817.00</td>\n",
       "      <td>2.6</td>\n",
       "      <td>Medium</td>\n",
       "      <td>9.414301</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      EM0406  Kota Jakarta Utara     Laki-laki    1975-01-07  2021-09-25   \n",
       "1      EM1772     Kabupaten Bogor     Perempuan    1993-04-18  2021-02-23   \n",
       "2      EM7996           Tangerang     Laki-laki    1998-02-12  2023-05-04   \n",
       "3     EM13978  Kota Jakarta Barat     Perempuan    1982-12-26  2021-09-11   \n",
       "4      EM9860           Tangerang     Perempuan    1997-03-26  2023-06-20   \n",
       "\n",
       "  resign_date marriage_stat  dependant education  absent_90D  ...  \\\n",
       "0  2023-12-07       Married          3      SLTA         3.0  ...   \n",
       "1  2023-06-24       Married          1      SLTA         2.0  ...   \n",
       "2  2024-06-29        Single          0      SLTA         5.0  ...   \n",
       "3  2023-04-03       Married          0        D3         0.0  ...   \n",
       "4  2024-10-02        Single          0      SLTA        15.0  ...   \n",
       "\n",
       "   active_work_category work_stability_score married_dependent_ratio  \\\n",
       "0              Mid-term             6.500000                       4   \n",
       "1              Mid-term             9.333333                       2   \n",
       "2              Mid-term             2.333333                       1   \n",
       "3              Mid-term            18.000000                       1   \n",
       "4              Mid-term             0.937500                       1   \n",
       "\n",
       "   position_score  job_income_position_score education_score  \\\n",
       "0               1                  1578410.0               1   \n",
       "1               1                  2003154.0               1   \n",
       "2               1                  1394384.0               1   \n",
       "3               1                  4151999.0               4   \n",
       "4               1                  1560817.0               1   \n",
       "\n",
       "  education_income_ratio  weighted_satisfaction_performance  \\\n",
       "0             1578410.00                                1.0   \n",
       "1             2003154.00                                2.2   \n",
       "2             1394384.00                                3.0   \n",
       "3             1037999.75                                2.6   \n",
       "4             1560817.00                                2.6   \n",
       "\n",
       "   resign_risk_indicator  adjusted_work_time  \n",
       "0                 Medium            9.797428  \n",
       "1                 Medium            9.342582  \n",
       "2                 Medium            9.551975  \n",
       "3                 Medium            9.180000  \n",
       "4                 Medium            9.414301  \n",
       "\n",
       "[5 rows x 37 columns]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_test = pd.read_csv('D:/Tugas Akhir/Codingan/Notebook - Playground/preprocessed_data_test_7.csv')\n",
    "df_test.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "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.9965371265802584\n",
      "Mean Absolute Error (MAE): 0.3994272346412294\n",
      "Mean Squared Error (MSE): 0.6346462447604223\n",
      "Root Mean Squared Error (RMSE): 0.7966468758241774\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 = 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": 23,
   "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>EM0120</td>\n",
       "      <td>Tangerang</td>\n",
       "      <td>Laki-laki</td>\n",
       "      <td>1990-02-18</td>\n",
       "      <td>2023-01-11</td>\n",
       "      <td>2024-01-30</td>\n",
       "      <td>Married</td>\n",
       "      <td>1</td>\n",
       "      <td>SLTA</td>\n",
       "      <td>11.0</td>\n",
       "      <td>...</td>\n",
       "      <td>Mid-term</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1140915.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1140915.0</td>\n",
       "      <td>1.4</td>\n",
       "      <td>Medium</td>\n",
       "      <td>9.393432</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>EM13985</td>\n",
       "      <td>Kepulauan Seribu</td>\n",
       "      <td>Perempuan</td>\n",
       "      <td>1987-02-01</td>\n",
       "      <td>2022-09-26</td>\n",
       "      <td>2023-11-08</td>\n",
       "      <td>Married</td>\n",
       "      <td>1</td>\n",
       "      <td>SLTA</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>Mid-term</td>\n",
       "      <td>13.000000</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2103348.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2103348.0</td>\n",
       "      <td>1.8</td>\n",
       "      <td>Medium</td>\n",
       "      <td>9.300000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>EM2560</td>\n",
       "      <td>Tangerang</td>\n",
       "      <td>Perempuan</td>\n",
       "      <td>1999-11-01</td>\n",
       "      <td>2023-01-05</td>\n",
       "      <td>2024-05-04</td>\n",
       "      <td>Single</td>\n",
       "      <td>0</td>\n",
       "      <td>SLTA</td>\n",
       "      <td>10.0</td>\n",
       "      <td>...</td>\n",
       "      <td>Mid-term</td>\n",
       "      <td>1.454545</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2145814.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2145814.0</td>\n",
       "      <td>1.6</td>\n",
       "      <td>Medium</td>\n",
       "      <td>9.205670</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>EM0343</td>\n",
       "      <td>Kabupaten Bekasi</td>\n",
       "      <td>Laki-laki</td>\n",
       "      <td>1990-10-12</td>\n",
       "      <td>2022-02-01</td>\n",
       "      <td>2023-07-17</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>8.500000</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2331081.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2331081.0</td>\n",
       "      <td>2.6</td>\n",
       "      <td>Medium</td>\n",
       "      <td>9.154017</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>EM14458</td>\n",
       "      <td>Kabupaten Bogor</td>\n",
       "      <td>Perempuan</td>\n",
       "      <td>1996-04-24</td>\n",
       "      <td>2022-10-23</td>\n",
       "      <td>2023-12-30</td>\n",
       "      <td>Married</td>\n",
       "      <td>1</td>\n",
       "      <td>SLTA</td>\n",
       "      <td>12.0</td>\n",
       "      <td>...</td>\n",
       "      <td>Mid-term</td>\n",
       "      <td>1.076923</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1798725.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1798725.0</td>\n",
       "      <td>2.6</td>\n",
       "      <td>Medium</td>\n",
       "      <td>9.706741</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      EM0120         Tangerang     Laki-laki    1990-02-18  2023-01-11   \n",
       "1     EM13985  Kepulauan Seribu     Perempuan    1987-02-01  2022-09-26   \n",
       "2      EM2560         Tangerang     Perempuan    1999-11-01  2023-01-05   \n",
       "3      EM0343  Kabupaten Bekasi     Laki-laki    1990-10-12  2022-02-01   \n",
       "4     EM14458   Kabupaten Bogor     Perempuan    1996-04-24  2022-10-23   \n",
       "\n",
       "  resign_date marriage_stat  dependant education  absent_90D  ...  \\\n",
       "0  2024-01-30       Married          1      SLTA        11.0  ...   \n",
       "1  2023-11-08       Married          1      SLTA         0.0  ...   \n",
       "2  2024-05-04        Single          0      SLTA        10.0  ...   \n",
       "3  2023-07-17        Single          0      SLTA         1.0  ...   \n",
       "4  2023-12-30       Married          1      SLTA        12.0  ...   \n",
       "\n",
       "   active_work_category work_stability_score married_dependent_ratio  \\\n",
       "0              Mid-term             1.000000                       2   \n",
       "1              Mid-term            13.000000                       2   \n",
       "2              Mid-term             1.454545                       1   \n",
       "3              Mid-term             8.500000                       1   \n",
       "4              Mid-term             1.076923                       2   \n",
       "\n",
       "   position_score  job_income_position_score education_score  \\\n",
       "0               1                  1140915.0               1   \n",
       "1               1                  2103348.0               1   \n",
       "2               1                  2145814.0               1   \n",
       "3               1                  2331081.0               1   \n",
       "4               1                  1798725.0               1   \n",
       "\n",
       "  education_income_ratio  weighted_satisfaction_performance  \\\n",
       "0              1140915.0                                1.4   \n",
       "1              2103348.0                                1.8   \n",
       "2              2145814.0                                1.6   \n",
       "3              2331081.0                                2.6   \n",
       "4              1798725.0                                2.6   \n",
       "\n",
       "   resign_risk_indicator  adjusted_work_time  \n",
       "0                 Medium            9.393432  \n",
       "1                 Medium            9.300000  \n",
       "2                 Medium            9.205670  \n",
       "3                 Medium            9.154017  \n",
       "4                 Medium            9.706741  \n",
       "\n",
       "[5 rows x 37 columns]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_test = pd.read_csv('D:/Tugas Akhir/Codingan/Notebook - Playground/preprocessed_data_test_5.csv')\n",
    "df_test.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "R² Score: 0.9965371265802584\n",
      "Mean Absolute Error (MAE): 0.3994272346412294\n",
      "Mean Squared Error (MSE): 0.6346462447604223\n",
      "Root Mean Squared Error (RMSE): 0.7966468758241774\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 = 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",
   "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.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
