{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Support Vector Machine"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Confusion Matrix:\n",
      "[[500  71  20]\n",
      " [ 15 558  18]\n",
      " [ 12  45 534]]\n",
      "\n",
      "Classification Report:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "     Negatif       0.95      0.85      0.89       591\n",
      "      Netral       0.83      0.94      0.88       591\n",
      "     Positif       0.93      0.90      0.92       591\n",
      "\n",
      "    accuracy                           0.90      1773\n",
      "   macro avg       0.90      0.90      0.90      1773\n",
      "weighted avg       0.90      0.90      0.90      1773\n",
      "\n",
      "\n",
      "Cross-Validation Scores:\n",
      "[0.81556684 0.82177101 0.87986464 0.94190637 0.91878173]\n",
      "Mean Cross-Validation Score: 0.8756\n",
      "Standard Deviation of Cross-Validation Scores: 0.0506\n"
     ]
    }
   ],
   "source": [
    "# Import library yang diperlukan\n",
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split, cross_val_score\n",
    "from sklearn.svm import SVC \n",
    "from sklearn.metrics import classification_report, confusion_matrix \n",
    "\n",
    "# Membaca dataset yang sudah di-balance\n",
    "df_balanced = pd.read_csv('data-analisis/datasets-balanced.csv')\n",
    "\n",
    "# Memisahkan fitur dan label\n",
    "X = df_balanced.drop(columns=['label'])  # Menghapus kolom label\n",
    "y = df_balanced['label']  # Mengambil kolom label\n",
    "\n",
    "# Membagi dataset menjadi data latih (80%) dan data uji 20%)\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)\n",
    "\n",
    "# Membuat model SVM\n",
    "svm_model = SVC(kernel='linear', random_state=42)  # Anda bisa mengganti kernel sesuai kebutuhan\n",
    "svm_model.fit(X_train, y_train)\n",
    "\n",
    "# Melakukan prediksi pada data uji\n",
    "y_pred = svm_model.predict(X_test)\n",
    "\n",
    "# Menampilkan hasil evaluasi\n",
    "print(\"Confusion Matrix:\")\n",
    "print(confusion_matrix(y_test, y_pred))\n",
    "print(\"\\nClassification Report:\")\n",
    "print(classification_report(y_test, y_pred))\n",
    "\n",
    "# Evaluasi model menggunakan cross-validation\n",
    "cv_scores = cross_val_score(svm_model, X, y, cv=5)  # Menggunakan 5-fold cross-validation\n",
    "\n",
    "# Menampilkan hasil cross-validation\n",
    "print(\"\\nCross-Validation Scores:\")\n",
    "print(cv_scores)\n",
    "print(f\"Mean Cross-Validation Score: {cv_scores.mean():.4f}\")\n",
    "print(f\"Standard Deviation of Cross-Validation Scores: {cv_scores.std():.4f}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 36144 characters]",
      "text/plain": [
       "<Figure size 1200x600 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "\n",
    "# Generate confusion matrix\n",
    "conf_matrix = confusion_matrix(y_test, y_pred)\n",
    "\n",
    "# Visualization\n",
    "plt.figure(figsize=(12, 6))\n",
    "\n",
    "# Subplot for Confusion Matrix\n",
    "plt.subplot(1, 2, 1)\n",
    "sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues', cbar=False)\n",
    "plt.title('Confusion Matrix')\n",
    "plt.xlabel('Predicted')\n",
    "plt.ylabel('True')\n",
    "\n",
    "# Subplot for Cross-Validation Scores\n",
    "plt.subplot(1, 2, 2)\n",
    "sns.boxplot(data=cv_scores, orient='w')\n",
    "plt.title('Cross-Validation Scores')\n",
    "plt.xlabel('Score')\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# save model\n",
    "import joblib # type: ignore\n",
    "\n",
    "with open('model-svm.pkl', 'wb') as file:\n",
    "    joblib.dump(svm_model, file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import joblib\n",
    "from sklearn.metrics import classification_report\n",
    "\n",
    "# Menampilkan hasil evaluasi\n",
    "class_report = classification_report(y_test, y_pred, output_dict=True)  # Get report as dict\n",
    "\n",
    "# # Save the model\n",
    "# joblib.dump(svm_model, 'HASIL-RISET/svm_model-new.')  # Save the model to a file\n",
    "\n",
    "# Save evaluation results\n",
    "results = {\n",
    "    'Confusion Matrix': [conf_matrix.flatten()],  # Flatten for easier saving\n",
    "    'Classification Report': [class_report],\n",
    "    'Cross-Validation Scores': [cv_scores.tolist()],\n",
    "    'Mean CV Score': [cv_scores.mean()],\n",
    "    'Std Dev CV Score': [cv_scores.std()]\n",
    "}\n",
    "\n",
    "results_df = pd.DataFrame(results)\n",
    "results_df.to_csv('HASIL-RISET/evaluation_results_SVM-new.csv', index=False)  # Save results to CSV"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 42652 characters]",
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt # type: ignore\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "# Muat data uji\n",
    "# X_test = pd.read_csv('HASIL-RISET/X_test.csv')\n",
    "\n",
    "# # Muat model dari file joblib\n",
    "# nb_model = joblib.load('HASIL-RISET/svm_model.pkl')\n",
    "\n",
    "# Lakukan prediksi pada data uji\n",
    "y_pred = svm_model.predict(X_test)\n",
    "\n",
    "# Ekspor hasil prediksi ke file CSV\n",
    "predictions_df = pd.DataFrame(y_pred, columns=['predicted_label'])\n",
    "predictions_df.to_csv('HASIL-RISET/predict_SVM.csv', index=False)\n",
    "\n",
    "# Hitung jumlah prediksi untuk setiap kelas\n",
    "unique, counts = np.unique(y_pred, return_counts=True)\n",
    "sentiment_counts = dict(zip(unique, counts))\n",
    "\n",
    "# Buat pie chart\n",
    "labels = sentiment_counts.keys()\n",
    "sizes = sentiment_counts.values()\n",
    "colors = ['red', 'blue', 'green']\n",
    "# explode = (0.1, 0, 0)  # Hanya meledakkan bagian pertama (Negatif)\n",
    "\n",
    "plt.figure(figsize=(8, 8))\n",
    "plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%',\n",
    "        shadow=False, startangle=140)\n",
    "plt.title('Distribusi Sentimen pada Data Uji')\n",
    "plt.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle.\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 51192 characters]",
      "text/plain": [
       "<Figure size 800x600 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.metrics import classification_report\n",
    "\n",
    "# Mendapatkan classification report dalam bentuk dictionary\n",
    "report = classification_report(y_test, y_pred, output_dict=True)\n",
    "\n",
    "# Mengonversi ke DataFrame untuk visualisasi\n",
    "df_report = pd.DataFrame(report).T\n",
    "\n",
    "# Menghapus support karena bukan metrik evaluasi\n",
    "df_report = df_report.drop(columns=['support'])\n",
    "\n",
    "# Membuat heatmap\n",
    "plt.figure(figsize=(8, 6))\n",
    "sns.heatmap(df_report, annot=True, cmap='coolwarm', fmt='.2f', linewidths=0.5)\n",
    "plt.title(\"Classification Report Heatmap\")\n",
    "plt.ylabel(\"Classes & Averages\")\n",
    "plt.xlabel(\"Metrics\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 1 entries, 0 to 0\n",
      "Data columns (total 5 columns):\n",
      " #   Column                   Non-Null Count  Dtype  \n",
      "---  ------                   --------------  -----  \n",
      " 0   Confusion Matrix         1 non-null      object \n",
      " 1   Classification Report    1 non-null      object \n",
      " 2   Cross-Validation Scores  1 non-null      object \n",
      " 3   Mean CV Score            1 non-null      float64\n",
      " 4   Std Dev CV Score         1 non-null      float64\n",
      "dtypes: float64(2), object(3)\n",
      "memory usage: 172.0+ bytes\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "df = pd.read_csv('HASIL-RISET/evaluation_results_SVM.csv')\n",
    "df.info()"
   ]
  }
 ],
 "metadata": {
  "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.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
