{ "cells": [ { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "import seaborn as sns\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import sklearn.preprocessing as pre\n", "from sklearn.model_selection import train_test_split, StratifiedKFold\n", "from sklearn.metrics import accuracy_score, confusion_matrix, classification_report\n", "import joblib" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "X = np.load(\"E:\\! KULIAHHH\\Ivano Kuliah\\!SEMESTER 8\\!SKRIPSI\\Data Suara\\!REVISI\\hasil_ekstrak_mfcc_v2/X_train.npy\")\n", "y = np.load(\"E:\\! KULIAHHH\\Ivano Kuliah\\!SEMESTER 8\\!SKRIPSI\\Data Suara\\!REVISI\\hasil_ekstrak_mfcc_v2/y_train.npy\")" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "# Encode label menjadi angka\n", "label_encoder = pre.LabelEncoder()\n", "y_encoded = label_encoder.fit_transform(y)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "# Inisialisasi K-Fold Cross-Validation\n", "k = 5 # Jumlah fold\n", "kf = StratifiedKFold(n_splits=k, shuffle=True, random_state=42)\n", "accuracies = [] # Simpan akurasi tiap fold" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "class LVQ:\n", " def __init__(self, n_classes, n_prototypes=2, learning_rate=0.01, epochs=200):\n", " self.n_classes = n_classes\n", " self.n_prototypes = n_prototypes\n", " self.learning_rate = learning_rate\n", " self.epochs = epochs\n", " self.prototypes = None\n", " self.prototype_labels = None\n", "\n", " def fit(self, X, y):\n", " np.random.seed(42)\n", " self.prototypes = []\n", " self.prototype_labels = []\n", "\n", " for label in np.unique(y):\n", " idx = np.where(y == label)[0]\n", " chosen = np.random.choice(idx, self.n_prototypes, replace=False)\n", " self.prototypes.extend(X[chosen])\n", " self.prototype_labels.extend([label] * self.n_prototypes)\n", "\n", " self.prototypes = np.array(self.prototypes)\n", " self.prototype_labels = np.array(self.prototype_labels)\n", "\n", " for epoch in range(self.epochs):\n", " for i in range(len(X)):\n", " sample = X[i]\n", " label = y[i]\n", " distances = np.linalg.norm(self.prototypes - sample, axis=1)\n", " winner_idx = np.argmin(distances)\n", "\n", " if self.prototype_labels[winner_idx] == label:\n", " self.prototypes[winner_idx] += self.learning_rate * (sample - self.prototypes[winner_idx])\n", " else:\n", " self.prototypes[winner_idx] -= self.learning_rate * (sample - self.prototypes[winner_idx])\n", "\n", " self.learning_rate *= 0.95\n", "\n", " def predict(self, X):\n", " y_pred = []\n", " for sample in X:\n", " distances = np.linalg.norm(self.prototypes - sample, axis=1)\n", " winner_idx = np.argmin(distances)\n", " y_pred.append(self.prototype_labels[winner_idx])\n", " return np.array(y_pred)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "šŸ”„ Fold 1/5...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "āœ… Akurasi Fold 1: 79.59%\n", "šŸ”„ Fold 2/5...\n", "āœ… Akurasi Fold 2: 87.76%\n", "šŸ”„ Fold 3/5...\n", "āœ… Akurasi Fold 3: 77.08%\n", "šŸ”„ Fold 4/5...\n", "āœ… Akurasi Fold 4: 83.33%\n", "šŸ”„ Fold 5/5...\n", "āœ… Akurasi Fold 5: 70.83%\n", "\n", "šŸ“Š Rata-rata Akurasi dari 5-Fold Cross Validation: 79.72%\n" ] } ], "source": [ "# K-Fold Cross-Validation\n", "for fold, (train_idx, test_idx) in enumerate(kf.split(X, y_encoded)):\n", " print(f\"šŸ”„ Fold {fold+1}/{k}...\")\n", "\n", " # Split data untuk fold ini\n", " X_train, X_test = X[train_idx], X[test_idx]\n", " y_train, y_test = y_encoded[train_idx], y_encoded[test_idx]\n", "\n", " # Inisialisasi dan training model LVQ\n", " lvq_model = LVQ(n_classes=len(set(y_encoded)), n_prototypes=2, learning_rate=0.01, epochs=200)\n", " lvq_model.fit(X_train, y_train)\n", "\n", " # Prediksi data uji\n", " y_pred = lvq_model.predict(X_test)\n", "\n", " # Evaluasi\n", " accuracy = accuracy_score(y_test, y_pred)\n", " accuracies.append(accuracy)\n", " print(f\"āœ… Akurasi Fold {fold+1}: {accuracy * 100:.2f}%\")\n", "\n", "# Hitung rata-rata akurasi dari semua fold\n", "mean_accuracy = np.mean(accuracies)\n", "print(f\"\\nšŸ“Š Rata-rata Akurasi dari {k}-Fold Cross Validation: {mean_accuracy * 100:.2f}%\")\n" ] } ], "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.10.0" } }, "nbformat": 4, "nbformat_minor": 2 }