{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" v1 | \n",
" v2 | \n",
" Unnamed: 2 | \n",
" Unnamed: 3 | \n",
" Unnamed: 4 | \n",
"
\n",
" \n",
" \n",
" \n",
" 1820 | \n",
" ham | \n",
" I'll probably be by tomorrow (or even later to... | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 4348 | \n",
" ham | \n",
" ÌÏ bot notes oredi... Cos i juz rem i got... | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 1553 | \n",
" ham | \n",
" Ok how you dear. Did you call chechi | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 3395 | \n",
" spam | \n",
" URGENT! Your Mobile number has been awarded wi... | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 2415 | \n",
" ham | \n",
" Huh means computational science... Y they like... | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" v1 v2 Unnamed: 2 \\\n",
"1820 ham I'll probably be by tomorrow (or even later to... NaN \n",
"4348 ham ÌÏ bot notes oredi... Cos i juz rem i got... NaN \n",
"1553 ham Ok how you dear. Did you call chechi NaN \n",
"3395 spam URGENT! Your Mobile number has been awarded wi... NaN \n",
"2415 ham Huh means computational science... Y they like... NaN \n",
"\n",
" Unnamed: 3 Unnamed: 4 \n",
"1820 NaN NaN \n",
"4348 NaN NaN \n",
"1553 NaN NaN \n",
"3395 NaN NaN \n",
"2415 NaN NaN "
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.read_csv('spam.csv', encoding='ISO-8859-1')\n",
"df.sample(5)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(5572, 5)"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Steps include in this project:\n",
"1. Data Cleaning\n",
"2. EDA (Expraiotery Data analysis)\n",
"3. Text pre processing\n",
"4. Model building\n",
"5. Evaluation\n",
"6. Improvmenets depending upon the evaluation\n",
"7. Website\n",
"8. Deploy"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**1.Data Cleaning**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"RangeIndex: 5572 entries, 0 to 5571\n",
"Data columns (total 5 columns):\n",
" # Column Non-Null Count Dtype \n",
"--- ------ -------------- ----- \n",
" 0 v1 5572 non-null object\n",
" 1 v2 5572 non-null object\n",
" 2 Unnamed: 2 50 non-null object\n",
" 3 Unnamed: 3 12 non-null object\n",
" 4 Unnamed: 4 6 non-null object\n",
"dtypes: object(5)\n",
"memory usage: 217.8+ KB\n"
]
}
],
"source": [
"\n",
"df.info()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"#drop last three columns\n",
"df.drop(columns=['Unnamed: 2','Unnamed: 3','Unnamed: 4'], inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" v1 | \n",
" v2 | \n",
"
\n",
" \n",
" \n",
" \n",
" 807 | \n",
" ham | \n",
" Boooo you always work. Just quit. | \n",
"
\n",
" \n",
" 1913 | \n",
" ham | \n",
" You want to go? | \n",
"
\n",
" \n",
" 4365 | \n",
" ham | \n",
" Mm yes dear look how i am hugging you both. :-P | \n",
"
\n",
" \n",
" 776 | \n",
" ham | \n",
" Why don't you go tell your friend you're not s... | \n",
"
\n",
" \n",
" 814 | \n",
" spam | \n",
" U were outbid by simonwatson5120 on the Shinco... | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" v1 v2\n",
"807 ham Boooo you always work. Just quit.\n",
"1913 ham You want to go? \n",
"4365 ham Mm yes dear look how i am hugging you both. :-P\n",
"776 ham Why don't you go tell your friend you're not s...\n",
"814 spam U were outbid by simonwatson5120 on the Shinco..."
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.sample(5)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" target | \n",
" text | \n",
"
\n",
" \n",
" \n",
" \n",
" 4113 | \n",
" ham | \n",
" Where are you ? What do you do ? How can you s... | \n",
"
\n",
" \n",
" 4244 | \n",
" ham | \n",
" Is toshiba portege m100 gd? | \n",
"
\n",
" \n",
" 3799 | \n",
" spam | \n",
" We tried to contact you re your reply to our o... | \n",
"
\n",
" \n",
" 1075 | \n",
" ham | \n",
" Oi. Ami parchi na re. Kicchu kaaj korte iccha ... | \n",
"
\n",
" \n",
" 1560 | \n",
" ham | \n",
" Just got some gas money, any chance you and th... | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" target text\n",
"4113 ham Where are you ? What do you do ? How can you s...\n",
"4244 ham Is toshiba portege m100 gd?\n",
"3799 spam We tried to contact you re your reply to our o...\n",
"1075 ham Oi. Ami parchi na re. Kicchu kaaj korte iccha ...\n",
"1560 ham Just got some gas money, any chance you and th..."
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Renaming the columns\n",
"df.rename(columns={'v1':'target','v2':'text'}, inplace=True)\n",
"df.sample(5)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.preprocessing import LabelEncoder\n",
"encoder = LabelEncoder()"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"df['target'] = encoder.fit_transform(df['target'])"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" target | \n",
" text | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 0 | \n",
" Go until jurong point, crazy.. Available only ... | \n",
"
\n",
" \n",
" 1 | \n",
" 0 | \n",
" Ok lar... Joking wif u oni... | \n",
"
\n",
" \n",
" 2 | \n",
" 1 | \n",
" Free entry in 2 a wkly comp to win FA Cup fina... | \n",
"
\n",
" \n",
" 3 | \n",
" 0 | \n",
" U dun say so early hor... U c already then say... | \n",
"
\n",
" \n",
" 4 | \n",
" 0 | \n",
" Nah I don't think he goes to usf, he lives aro... | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" target text\n",
"0 0 Go until jurong point, crazy.. Available only ...\n",
"1 0 Ok lar... Joking wif u oni...\n",
"2 1 Free entry in 2 a wkly comp to win FA Cup fina...\n",
"3 0 U dun say so early hor... U c already then say...\n",
"4 0 Nah I don't think he goes to usf, he lives aro..."
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"target 0\n",
"text 0\n",
"dtype: int64"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Missing values\n",
"df.isnull().sum() #Use to check missing values"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"#Check for duplicate values.\n",
"df.duplicated().sum()\n",
"\n",
"#Remove duplicates\n",
"df = df.drop_duplicates(keep = 'first')"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.duplicated().sum()"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(5169, 2)"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**2. EDA** (Exploratory Data Analysis)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" target | \n",
" text | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 0 | \n",
" Go until jurong point, crazy.. Available only ... | \n",
"
\n",
" \n",
" 1 | \n",
" 0 | \n",
" Ok lar... Joking wif u oni... | \n",
"
\n",
" \n",
" 2 | \n",
" 1 | \n",
" Free entry in 2 a wkly comp to win FA Cup fina... | \n",
"
\n",
" \n",
" 3 | \n",
" 0 | \n",
" U dun say so early hor... U c already then say... | \n",
"
\n",
" \n",
" 4 | \n",
" 0 | \n",
" Nah I don't think he goes to usf, he lives aro... | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" target text\n",
"0 0 Go until jurong point, crazy.. Available only ...\n",
"1 0 Ok lar... Joking wif u oni...\n",
"2 1 Free entry in 2 a wkly comp to win FA Cup fina...\n",
"3 0 U dun say so early hor... U c already then say...\n",
"4 0 Nah I don't think he goes to usf, he lives aro..."
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#How many messages are spam and ham?\n",
"\n",
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"target\n",
"0 4516\n",
"1 653\n",
"Name: count, dtype: int64"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df['target'].value_counts()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"ename": "AttributeError",
"evalue": "module 'matplotlib' has no attribute 'pie'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[34], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mmatplotlib\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mplt\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m \u001b[43mplt\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpie\u001b[49m(df[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtarget\u001b[39m\u001b[38;5;124m'\u001b[39m]\u001b[38;5;241m.\u001b[39mvalue_counts(), labels\u001b[38;5;241m=\u001b[39m[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mham\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mspam\u001b[39m\u001b[38;5;124m'\u001b[39m],autopct \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m%0.2f\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n",
"File \u001b[1;32mc:\\Users\\Muhammad Arham\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\matplotlib\\_api\\__init__.py:217\u001b[0m, in \u001b[0;36mcaching_module_getattr..__getattr__\u001b[1;34m(name)\u001b[0m\n\u001b[0;32m 215\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m name \u001b[38;5;129;01min\u001b[39;00m props:\n\u001b[0;32m 216\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m props[name]\u001b[38;5;241m.\u001b[39m\u001b[38;5;21m__get__\u001b[39m(instance)\n\u001b[1;32m--> 217\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mAttributeError\u001b[39;00m(\n\u001b[0;32m 218\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmodule \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__module__\u001b[39m\u001b[38;5;132;01m!r}\u001b[39;00m\u001b[38;5;124m has no attribute \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mname\u001b[38;5;132;01m!r}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n",
"\u001b[1;31mAttributeError\u001b[0m: module 'matplotlib' has no attribute 'pie'"
]
}
],
"source": [
"import matplotlib as plt\n",
"plt.pie(df['target'].value_counts(), labels=['ham','spam'],autopct =\"%0.2f\")"
]
}
],
"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.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}