Aryanji123/Spam_classifier
0
1{2 "cells": [3 {4 "cell_type": "code",5 "execution_count": 408,6 "id": "edf371a2-c473-4e40-aa45-7bac9e799703",7 "metadata": {},8 "outputs": [9 {10 "name": "stdout",11 "output_type": "stream",12 "text": [13 "Looking in links: /var/folders/fg/r_6zh_k540n9l8b5hxcy2w3m0000gn/T/tmp7i8iaqio\n",14 "Requirement already satisfied: pip in /Users/aryan/Desktop/agc_internship/.venv/lib/python3.12/site-packages (24.0)\n",15 "Requirement already satisfied: nltk in /Users/aryan/Desktop/agc_internship/.venv/lib/python3.12/site-packages (3.9.2)\n",16 "Requirement already satisfied: click in /Users/aryan/Desktop/agc_internship/.venv/lib/python3.12/site-packages (from nltk) (8.3.1)\n",17 "Requirement already satisfied: joblib in /Users/aryan/Desktop/agc_internship/.venv/lib/python3.12/site-packages (from nltk) (1.5.3)\n",18 "Requirement already satisfied: regex>=2021.8.3 in /Users/aryan/Desktop/agc_internship/.venv/lib/python3.12/site-packages (from nltk) (2025.11.3)\n",19 "Requirement already satisfied: tqdm in /Users/aryan/Desktop/agc_internship/.venv/lib/python3.12/site-packages (from nltk) (4.67.1)\n",20 "\n",21 "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.0\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m25.3\u001b[0m\n",22 "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpython -m pip install --upgrade pip\u001b[0m\n"23 ]24 }25 ],26 "source": [27 "import sys\n",28 "!{sys.executable} -m ensurepip --upgrade\n",29 "!{sys.executable} -m pip install nltk\n"30 ]31 },32 {33 "cell_type": "code",34 "execution_count": 410,35 "id": "59ff2bb9-b8a1-46ae-921f-718786577af2",36 "metadata": {},37 "outputs": [],38 "source": [39 "import pandas as pd\n",40 "import nltk\n",41 "messages =pd.read_csv(\"SMSSpamCollection.txt\",sep='\\t',names=[\"label\",\"messages\"])"42 ]43 },44 {45 "cell_type": "code",46 "execution_count": 413,47 "id": "22e8b0e3-a650-47d9-8c74-e95ff639f895",48 "metadata": {},49 "outputs": [50 {51 "name": "stderr",52 "output_type": "stream",53 "text": [54 "[nltk_data] Downloading package stopwords to /Users/aryan/nltk_data...\n",55 "[nltk_data] Package stopwords is already up-to-date!\n"56 ]57 },58 {59 "data": {60 "text/plain": [61 "True"62 ]63 },64 "execution_count": 413,65 "metadata": {},66 "output_type": "execute_result"67 }68 ],69 "source": [70 "#Data cleaning and preprocessing\n",71 "import re\n",72 "nltk.download('stopwords')"73 ]74 },75 {76 "cell_type": "markdown",77 "id": "ce6c616c-1bf1-4aed-9ba9-bdcdf5a2e5c7",78 "metadata": {},79 "source": [80 "###DATA Cleaning ###"81 ]82 },83 {84 "cell_type": "code",85 "execution_count": 416,86 "id": "66ec0cad-7055-4264-9156-578b349d6a69",87 "metadata": {},88 "outputs": [],89 "source": [90 "from nltk.corpus import stopwords\n",91 "from nltk.stem.porter import PorterStemmer"92 ]93 },94 {95 "cell_type": "code",96 "execution_count": 418,97 "id": "1352022e-171c-45f1-b3c4-4bd5bf78a1d1",98 "metadata": {},99 "outputs": [],100 "source": [101 "ps= PorterStemmer()\n",102 "wordnet=WordNetLemmatizer()\n",103 "from nltk.stem import WordNetLemmatizer\n",104 "corpus=[]"105 ]106 },107 {108 "cell_type": "code",109 "execution_count": 420,110 "id": "623b17f8-ac8d-48c6-8af7-601d104a4034",111 "metadata": {},112 "outputs": [],113 "source": [114 "for i in range(0, len(messages)):\n",115 " review = re.sub('[^a-zA-Z]', ' ', messages['messages'][i])\n",116 " review = review.lower()\n",117 " review = review.split()\n",118 " \n",119 " review = [wordnet.lemmatize(word) for word in review if not word in stopwords.words('english')]\n",120 " review = ' '.join(review)\n",121 " corpus.append(review)"122 ]123 },124 {125 "cell_type": "markdown",126 "id": "7d795d7b-e0e9-4d02-8858-ec03bf54b6e9",127 "metadata": {},128 "source": [129 "###Either Run the above code or the following one "130 ]131 },132 {133 "cell_type": "code",134 "execution_count": 368,135 "id": "210e1bd8-1e4d-4aa1-8aa0-269a13cb5572",136 "metadata": {},137 "outputs": [],138 "source": [139 "#either run this or the above one \n",140 "for i in range(0, len(messages)):\n",141 " review = re.sub('[^a-zA-Z]', ' ', messages['messages'][i])\n",142 " review = review.lower()\n",143 " review = review.split()\n",144 " \n",145 " review = [ps.stem(word) for word in review if not word in stopwords.words('english')]\n",146 " review = ' '.join(review)\n",147 " corpus.append(review)"148 ]149 },150 {151 "cell_type": "code",152 "execution_count": 422,153 "id": "bbd300da-6243-4492-9a13-4e7964f311fd",154 "metadata": {},155 "outputs": [156 {157 "name": "stdout",158 "output_type": "stream",159 "text": [160 "Requirement already satisfied: scikit-learn in /Users/aryan/Desktop/agc_internship/.venv/lib/python3.12/site-packages (1.8.0)\n",161 "Requirement already satisfied: numpy>=1.24.1 in /Users/aryan/Desktop/agc_internship/.venv/lib/python3.12/site-packages (from scikit-learn) (2.3.5)\n",162 "Requirement already satisfied: scipy>=1.10.0 in /Users/aryan/Desktop/agc_internship/.venv/lib/python3.12/site-packages (from scikit-learn) (1.16.3)\n",163 "Requirement already satisfied: joblib>=1.3.0 in /Users/aryan/Desktop/agc_internship/.venv/lib/python3.12/site-packages (from scikit-learn) (1.5.3)\n",164 "Requirement already satisfied: threadpoolctl>=3.2.0 in /Users/aryan/Desktop/agc_internship/.venv/lib/python3.12/site-packages (from scikit-learn) (3.6.0)\n",165 "\n",166 "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.0\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m25.3\u001b[0m\n",167 "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpython -m pip install --upgrade pip\u001b[0m\n"168 ]169 }170 ],171 "source": [172 "import sys\n",173 "!{sys.executable} -m pip install scikit-learn"174 ]175 },176 {177 "cell_type": "code",178 "execution_count": 354,179 "id": "4554d59d-394b-460b-9aa9-b5b02e5d1282",180 "metadata": {},181 "outputs": [],182 "source": [183 "# Creating the Bag of Words model\n",184 "from sklearn.feature_extraction.text import CountVectorizer\n",185 "cv = CountVectorizer(max_features=4500)\n",186 "X = cv.fit_transform(corpus).toarray()\n",187 "\n",188 "y=pd.get_dummies(messages['label'])\n",189 "y=y.iloc[:,1].values\n"190 ]191 },192 {193 "cell_type": "markdown",194 "id": "c476e25b-720e-454c-821a-cea5cc6a458c",195 "metadata": {},196 "source": [197 "###Either run this or the above one "198 ]199 },200 {201 "cell_type": "code",202 "execution_count": 424,203 "id": "b15dbe6a-c1be-4d42-bc3b-3bab0cf9d2fe",204 "metadata": {},205 "outputs": [],206 "source": [207 "# Creating the TF-IDF model\n",208 "from sklearn.feature_extraction.text import TfidfVectorizer\n",209 "cv = TfidfVectorizer()\n",210 "X = cv.fit_transform(corpus).toarray()\n",211 "y=pd.get_dummies(messages['label'])\n",212 "y=y.iloc[:,1].values"213 ]214 },215 {216 "cell_type": "code",217 "execution_count": 426,218 "id": "14f7606b-dff4-4667-a6e6-e053fa229001",219 "metadata": {},220 "outputs": [],221 "source": [222 "# Train Test Split\n",223 "\n",224 "from sklearn.model_selection import train_test_split\n",225 "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.20, random_state = 0)\n",226 "\n",227 "# Training model using Naive bayes classifier\n",228 "\n",229 "from sklearn.naive_bayes import MultinomialNB\n",230 "spam_detect_model = MultinomialNB().fit(X_train, y_train)\n",231 "\n",232 "y_pred=spam_detect_model.predict(X_test)"233 ]234 },235 {236 "cell_type": "code",237 "execution_count": 428,238 "id": "70fa1db1-eac6-452b-b233-1e7f46bd5ae4",239 "metadata": {},240 "outputs": [],241 "source": [242 "from sklearn.metrics import confusion_matrix\n",243 "from sklearn.metrics import accuracy_score"244 ]245 },246 {247 "cell_type": "code",248 "execution_count": 430,249 "id": "9ab6a49d-2971-4a32-87c0-1372aee24522",250 "metadata": {},251 "outputs": [252 {253 "data": {254 "text/plain": [255 "array([[955, 0],\n",256 " [ 31, 129]])"257 ]258 },259 "execution_count": 430,260 "metadata": {},261 "output_type": "execute_result"262 }263 ],264 "source": [265 "confusion_m=confusion_matrix(y_test,y_pred)\n",266 "confusion_m"267 ]268 },269 {270 "cell_type": "code",271 "execution_count": 432,272 "id": "4c4609eb-1cc7-4978-8489-3f513b4b0869",273 "metadata": {},274 "outputs": [275 {276 "data": {277 "text/plain": [278 "0.9721973094170404"279 ]280 },281 "execution_count": 432,282 "metadata": {},283 "output_type": "execute_result"284 }285 ],286 "source": [287 "acc_score=accuracy_score(y_test,y_pred)\n",288 "acc_score"289 ]290 },291 {292 "cell_type": "code",293 "execution_count": null,294 "id": "10231302-296e-4a18-8e51-58de9a21aa9c",295 "metadata": {},296 "outputs": [],297 "source": []298 },299 {300 "cell_type": "code",301 "execution_count": null,302 "id": "17f1f8df-d2d2-4cfb-ba7b-3301d453b6d2",303 "metadata": {},304 "outputs": [],305 "source": []306 },307 {308 "cell_type": "code",309 "execution_count": null,310 "id": "c8abd3bd-d08e-4df9-9e5d-f0451ae0f861",311 "metadata": {},312 "outputs": [],313 "source": []314 },315 {316 "cell_type": "code",317 "execution_count": null,318 "id": "a396e242-b31c-4310-bc52-7836a8a111c1",319 "metadata": {},320 "outputs": [],321 "source": []322 },323 {324 "cell_type": "code",325 "execution_count": null,326 "id": "152f4586-fc3e-469e-9369-e05e0d2e31d4",327 "metadata": {},328 "outputs": [],329 "source": []330 },331 {332 "cell_type": "code",333 "execution_count": null,334 "id": "9abf35e3-3f9d-4343-9b9f-70c5117535c7",335 "metadata": {},336 "outputs": [],337 "source": []338 },339 {340 "cell_type": "code",341 "execution_count": null,342 "id": "1139852d-40ec-4137-ad32-8b62b236232d",343 "metadata": {},344 "outputs": [],345 "source": []346 },347 {348 "cell_type": "code",349 "execution_count": null,350 "id": "66ab23d1-a78c-4f27-a937-3863e2c3f6c2",351 "metadata": {},352 "outputs": [],353 "source": []354 }355 ],356 "metadata": {357 "kernelspec": {358 "display_name": "Python (uv-env)",359 "language": "python",360 "name": "uv-env"361 },362 "language_info": {363 "codemirror_mode": {364 "name": "ipython",365 "version": 3366 },367 "file_extension": ".py",368 "mimetype": "text/x-python",369 "name": "python",370 "nbconvert_exporter": "python",371 "pygments_lexer": "ipython3",372 "version": "3.12.4"373 }374 },375 "nbformat": 4,376 "nbformat_minor": 5377}378 