{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# GitHub Spam Comment Detector — Model Training\n", "\n", "**Author:** Sambhaji Patil \n", "**Dataset:** Custom-scraped GitHub comments via GraphQL (~109k samples) \n", "**Goal:** Train and compare three classification approaches:\n", "1. **Multinomial Naive Bayes** (TF-IDF baseline)\n", "2. **LinearSVC** (TF-IDF + Hyperparameter Tuning via GridSearchCV)\n", "3. **Bi-LSTM** (PyTorch Deep Learning)\n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Imports & Setup" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "PyTorch device : cuda\n", "Seaborn version: 0.13.2\n" ] } ], "source": [ "import numpy as np\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "import matplotlib.ticker as mticker\n", "import seaborn as sns\n", "import re, warnings, time\n", "from collections import Counter\n", "\n", "import torch\n", "import torch.nn as nn\n", "from torch.utils.data import Dataset, DataLoader\n", "\n", "from sklearn.feature_extraction.text import TfidfVectorizer\n", "from sklearn.naive_bayes import MultinomialNB\n", "from sklearn.svm import LinearSVC\n", "from sklearn.pipeline import Pipeline\n", "from sklearn.model_selection import GridSearchCV, train_test_split\n", "from sklearn.metrics import (\n", " classification_report, confusion_matrix,\n", " ConfusionMatrixDisplay, accuracy_score,\n", " f1_score, precision_score, recall_score\n", ")\n", "\n", "import joblib\n", "\n", "warnings.filterwarnings('ignore')\n", "sns.set_theme(style='darkgrid', palette='muted')\n", "\n", "DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n", "SEED = 42\n", "np.random.seed(SEED)\n", "torch.manual_seed(SEED)\n", "\n", "print(f'PyTorch device : {DEVICE}')\n", "print(f'Seaborn version: {sns.__version__}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. Data Loading" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Raw shape : (109662, 2)\n" ] }, { "data": { "text/html": [ "
| \n", " | label | \n", "text | \n", "
|---|---|---|
| 0 | \n", "0.0 | \n", "Thank you for reporting this issue.\\n\\nThis is... | \n", "
| 1 | \n", "0.0 | \n", "The test explicitly asks also for the closing ... | \n", "
| 2 | \n", "0.0 | \n", "This issue is open to first-time code contribu... | \n", "
| 3 | \n", "0.0 | \n", "The current behaviour is that everything clear... | \n", "
| 4 | \n", "0.0 | \n", "Some labs can be pretty long, more than the cu... | \n", "