{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e29b569c-b6a4-4eff-898d-ba939193228d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sedang memproses data...\n",
      "Sedang melatih model Decision Tree...\n",
      "\n",
      "========================================\n",
      "HASIL EVALUASI (DECISION TREE)\n",
      "========================================\n",
      "1. Single Split Test:\n",
      "   - R2 Score (Akurasi) : 0.8059 (80.59%)\n",
      "   - RMSE (Error Kuadrat): 1.0580\n",
      "   - MAE (Rata-rata Error): 0.7046 poin\n",
      "\n",
      "2. Cross Validation (5-Fold):\n",
      "   - Skor per fold  : [0.5092657  0.74560943 0.78916584 0.80808243 0.81677625]\n",
      "   - Rata-rata R2   : 0.7338\n",
      "   - Kestabilan     : +/- 0.1149\n",
      "\n",
      "========================================\n",
      "Contoh Prediksi:\n",
      " - Rating Asli     : 7.0\n",
      " - Prediksi Model  : 6.19\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.model_selection import train_test_split, cross_val_score\n",
    "from sklearn.tree import DecisionTreeRegressor\n",
    "from sklearn.metrics import mean_squared_error, r2_score, mean_absolute_error\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "\n",
    "# ==========================================\n",
    "# 1. LOAD DATA & PREPROCESSING\n",
    "# ==========================================\n",
    "print(\"Sedang memproses data...\")\n",
    "\n",
    "df = pd.read_csv('Latest 2025 movies Datasets.csv')\n",
    "\n",
    "# Membersihkan data: pastikan kolom penting tidak kosong\n",
    "required_cols = ['release_date', 'vote_average', 'popularity', 'vote_count', 'original_language']\n",
    "df = df.dropna(subset=required_cols)\n",
    "\n",
    "# Konversi release_date ke datetime\n",
    "df['release_date'] = pd.to_datetime(df['release_date'], errors='coerce')\n",
    "df = df.dropna(subset=['release_date'])  # hapus yang gagal konversi\n",
    "\n",
    "# Feature Engineering\n",
    "df['release_year'] = df['release_date'].dt.year\n",
    "df['release_month'] = df['release_date'].dt.month\n",
    "\n",
    "# Encoding original_language\n",
    "le = LabelEncoder()\n",
    "df['original_language_encoded'] = le.fit_transform(df['original_language'])\n",
    "\n",
    "# Menentukan Fitur & Target\n",
    "features = ['popularity', 'vote_count', 'release_year', 'release_month', 'original_language_encoded']\n",
    "X = df[features]\n",
    "y = df['vote_average']\n",
    "\n",
    "# Split Data (80% train, 20% test)\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
    "\n",
    "# ==========================================\n",
    "# 2. TRAINING MODEL (DECISION TREE)\n",
    "# ==========================================\n",
    "print(\"Sedang melatih model Decision Tree...\")\n",
    "\n",
    "model = DecisionTreeRegressor(max_depth=5, random_state=42)\n",
    "model.fit(X_train, y_train)\n",
    "\n",
    "# ==========================================\n",
    "# 3. EVALUASI LENGKAP & CROSS VALIDATION\n",
    "# ==========================================\n",
    "print(\"\\n\" + \"=\"*40)\n",
    "print(\"HASIL EVALUASI (DECISION TREE)\")\n",
    "print(\"=\"*40)\n",
    "\n",
    "# A. Evaluasi Single Split (Test Set)\n",
    "y_pred = model.predict(X_test)\n",
    "\n",
    "r2 = r2_score(y_test, y_pred)\n",
    "rmse = np.sqrt(mean_squared_error(y_test, y_pred))\n",
    "mae = mean_absolute_error(y_test, y_pred)\n",
    "\n",
    "print(f\"1. Single Split Test:\")\n",
    "print(f\"   - R2 Score (Akurasi) : {r2:.4f} ({r2*100:.2f}%)\")\n",
    "print(f\"   - RMSE (Error Kuadrat): {rmse:.4f}\")\n",
    "print(f\"   - MAE (Rata-rata Error): {mae:.4f} poin\")\n",
    "\n",
    "# B. Cross Validation (5-Fold)\n",
    "print(f\"\\n2. Cross Validation (5-Fold):\")\n",
    "cv_scores = cross_val_score(model, X, y, cv=5, scoring='r2')\n",
    "print(f\"   - Skor per fold  : {cv_scores}\")\n",
    "print(f\"   - Rata-rata R2   : {cv_scores.mean():.4f}\")\n",
    "print(f\"   - Kestabilan     : +/- {cv_scores.std():.4f}\")\n",
    "\n",
    "# C. Contoh Prediksi\n",
    "print(\"\\n\" + \"=\"*40)\n",
    "sample_index = y_test.index[0]  # pastikan index asli\n",
    "print(f\"Contoh Prediksi:\")\n",
    "print(f\" - Rating Asli     : {y_test.loc[sample_index]}\")\n",
    "print(f\" - Prediksi Model  : {y_pred[0]:.2f}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e15a4120-6a82-4d24-a90b-a0b6df3e59db",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5d8d987a-7a3a-4601-a22c-7ed3b486d288",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1ce678cc-f5cb-461f-aaad-b9a25ce0ec40",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2c7ad7ba-191e-472a-97a9-3870b5ee7f93",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
