{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "02e1d686-6bb5-42ad-87a2-40036c54b9e0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sedang memproses data...\n",
      "Sedang melatih model Polynomial Regression (Degree 2)...\n",
      "\n",
      "========================================\n",
      "HASIL EVALUASI (POLYNOMIAL DEGREE 2)\n",
      "========================================\n",
      "1. Single Split Test:\n",
      "   - R2 Score (Akurasi) : -0.3654\n",
      "   - RMSE (Error Kuadrat): 2.8060\n",
      "   - MAE (Rata-rata Error): 1.6331 poin\n",
      "\n",
      "2. Cross Validation (5-Fold):\n",
      "   - Skor per tes : [-1.46479088e+04  7.39100795e-02  1.21529017e-01  1.10146144e-01\n",
      "  5.71513075e-02]\n",
      "   - Rata-rata R2 : -2929.5092\n",
      "   - Kestabilan   : +/- 5859.1998\n",
      "\n",
      "========================================\n",
      "Contoh Prediksi: Rating Asli 7.0 | Prediksi Poly 5.28\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.linear_model import LinearRegression\n",
    "from sklearn.preprocessing import PolynomialFeatures, LabelEncoder\n",
    "from sklearn.pipeline import make_pipeline\n",
    "from sklearn.metrics import mean_squared_error, r2_score, mean_absolute_error\n",
    "\n",
    "# ==========================================\n",
    "# 1. LOAD DATA & PREPROCESSING\n",
    "# ==========================================\n",
    "print(\"Sedang memproses data...\")\n",
    "df = pd.read_csv('Latest 2025 movies Datasets.csv')\n",
    "# take a look at the dataset\n",
    "df.head()\n",
    "\n",
    "df = df.dropna(subset=['release_date', 'vote_average', 'popularity', 'vote_count'])\n",
    "df['release_date'] = pd.to_datetime(df['release_date'], errors='coerce')\n",
    "df = df.dropna(subset=['release_date'])\n",
    "\n",
    "df['release_year'] = df['release_date'].dt.year\n",
    "df['release_month'] = df['release_date'].dt.month\n",
    "\n",
    "le = LabelEncoder()\n",
    "df['original_language_encoded'] = le.fit_transform(df['original_language'])\n",
    "\n",
    "features = ['popularity', 'vote_count', 'release_year', 'release_month', 'original_language_encoded']\n",
    "X = df[features]\n",
    "y = df['vote_average']\n",
    "\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 (POLYNOMIAL DEGREE 2)\n",
    "# ==========================================\n",
    "degree = 2\n",
    "print(f\"Sedang melatih model Polynomial Regression (Degree {degree})...\")\n",
    "# Pipeline: Buat fitur pangkat -> Lalu Regresi Linear\n",
    "model = make_pipeline(PolynomialFeatures(degree), LinearRegression())\n",
    "model.fit(X_train, y_train)\n",
    "\n",
    "# ==========================================\n",
    "# 3. EVALUASI LENGKAP\n",
    "# ==========================================\n",
    "print(\"\\n\" + \"=\"*40)\n",
    "print(f\"HASIL EVALUASI (POLYNOMIAL DEGREE {degree})\")\n",
    "print(\"=\"*40)\n",
    "\n",
    "# Prediksi data test\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}\")\n",
    "print(f\"   - RMSE (Error Kuadrat): {rmse:.4f}\")\n",
    "print(f\"   - MAE (Rata-rata Error): {mae:.4f} poin\")\n",
    "\n",
    "# Cross Validation (5-Fold)\n",
    "print(f\"\\n2. Cross Validation (5-Fold):\")\n",
    "# Hati-hati: Polynomial CV bisa agak lambat dibanding Linear biasa\n",
    "cv_scores = cross_val_score(model, X, y, cv=5, scoring='r2')\n",
    "\n",
    "print(f\"   - Skor per tes : {cv_scores}\")\n",
    "print(f\"   - Rata-rata R2 : {cv_scores.mean():.4f}\")\n",
    "print(f\"   - Kestabilan   : +/- {cv_scores.std():.4f}\")\n",
    "\n",
    "# Contoh Prediksi\n",
    "print(\"\\n\" + \"=\"*40)\n",
    "print(f\"Contoh Prediksi: Rating Asli {y_test.iloc[0]} | Prediksi Poly {y_pred[0]:.2f}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8d83a184-b95f-4b73-a67d-1d523923ee1f",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'pd' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mNameError\u001b[39m                                 Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[1]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m df = \u001b[43mpd\u001b[49m.read_csv(\u001b[33m\"\u001b[39m\u001b[33mLatest 2025 movies Datasets.csv\u001b[39m\u001b[33m\"\u001b[39m)\n\u001b[32m      3\u001b[39m \u001b[38;5;66;03m# take a look at the dataset\u001b[39;00m\n\u001b[32m      4\u001b[39m df.head()\n",
      "\u001b[31mNameError\u001b[39m: name 'pd' is not defined"
     ]
    }
   ],
   "source": [
    "df = pd.read_csv(\"Latest 2025 movies Datasets.csv\")\n",
    "\n",
    "# take a look at the dataset\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9c933e26-cbc2-47e4-9f25-efbb65ef1d92",
   "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
}
