materi-praktikum/NLP/.ipynb_checkpoints/Klasifikasi Teks FNN-checkpoint.ipynb

152 lines
4.8 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "f4a1399a-f23d-4060-a07e-bce5a5c7ddac",
"metadata": {},
"source": [
"# Klasifikasi Teks menggunakan ANN\n",
"## Arif R Dwiyanto\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "53a214ae-c9cf-4d46-925d-068f1685537b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"=== Classification Report ===\n",
" precision recall f1-score support\n",
"\n",
" negative 0.00 0.00 0.00 1.0\n",
" positive 0.00 0.00 0.00 1.0\n",
"\n",
" accuracy 0.00 2.0\n",
" macro avg 0.00 0.00 0.00 2.0\n",
"weighted avg 0.00 0.00 0.00 2.0\n",
"\n",
"=== Confusion Matrix ===\n",
"[[0 1]\n",
" [1 0]]\n",
"\n",
"Prediksi untuk: barang buruk, saya kecewa\n",
"Hasil: negative\n"
]
}
],
"source": [
"# ---------------------------------------------------------\n",
"# Klasifikasi Teks dengan TF-IDF + Feedforward Neural Network\n",
"# ---------------------------------------------------------\n",
"\n",
"import pandas as pd\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.feature_extraction.text import TfidfVectorizer\n",
"from sklearn.neural_network import MLPClassifier\n",
"from sklearn.metrics import classification_report, confusion_matrix\n",
"\n",
"# -----------------------------------------\n",
"# 1. Contoh Dataset\n",
"# -----------------------------------------\n",
"# Anda bisa mengganti dataset ini dengan dataset lain (CSV, JSON, dll)\n",
"\n",
"data = {\n",
" \"text\": [\n",
" \"Saya suka produk ini, luar biasa\",\n",
" \"Layanannya buruk, sangat kecewa\",\n",
" \"Pembelian terbaik yang pernah saya lakukan\",\n",
" \"Saya benci produk ini, buang-buang uang\",\n",
" \"Kualitasnya sangat bagus, direkomendasikan\",\n",
" \"Pengalaman buruk, tidak akan membeli lagi\"\n",
" ],\n",
" \"label\": [\"positive\", \"negative\", \"positive\", \"negative\", \"positive\", \"negative\"]\n",
"}\n",
"\n",
"df = pd.DataFrame(data)\n",
"\n",
"# -----------------------------------------\n",
"# 2. Split Train & Test\n",
"# -----------------------------------------\n",
"X_train, X_test, y_train, y_test = train_test_split(\n",
" df[\"text\"], df[\"label\"], test_size=0.3, random_state=42\n",
")\n",
"\n",
"# -----------------------------------------\n",
"# 3. TF-IDF Vectorization\n",
"# -----------------------------------------\n",
"tfidf = TfidfVectorizer(max_features=5000)\n",
"X_train_tfidf = tfidf.fit_transform(X_train)\n",
"X_test_tfidf = tfidf.transform(X_test)\n",
"\n",
"# -----------------------------------------\n",
"# 4. Feedforward ANN (MLPClassifier)\n",
"# -----------------------------------------\n",
"model = MLPClassifier(\n",
" hidden_layer_sizes=(256, 64),\n",
" activation='relu',\n",
" solver='adam',\n",
" max_iter=500,\n",
" random_state=42\n",
")\n",
"\n",
"model.fit(X_train_tfidf, y_train)\n",
"\n",
"# -----------------------------------------\n",
"# 5. Evaluasi Model\n",
"# -----------------------------------------\n",
"y_pred = model.predict(X_test_tfidf)\n",
"\n",
"print(\"=== Classification Report ===\")\n",
"print(classification_report(y_test, y_pred))\n",
"\n",
"print(\"=== Confusion Matrix ===\")\n",
"print(confusion_matrix(y_test, y_pred))\n",
"\n",
"# -----------------------------------------\n",
"# 6. Prediksi Teks Baru\n",
"# -----------------------------------------\n",
"sample_text = [\"barang bagus luar biasa\"]\n",
"sample_text = [\"barang buruk, saya kecewa\"]\n",
"sample_vec = tfidf.transform(sample_text)\n",
"prediction = model.predict(sample_vec)\n",
"\n",
"print(\"\\nPrediksi untuk:\", sample_text[0])\n",
"print(\"Hasil:\", prediction[0])\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9f7d90fe-4af4-446c-9547-c9312bfa6fc7",
"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.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}