0% found this document useful (0 votes)
4 views8 pages

AI Report

The document discusses artificial intelligence (AI) and its capability to mimic human cognitive functions, with a focus on an AI application for email spam filtering. It details the implementation of a spam filter using Python and various machine learning techniques, including data preprocessing, model training, and evaluation through accuracy, confusion matrix, and ROC curve. The report concludes with a function to classify emails as spam or ham based on the trained model.

Uploaded by

omarhamdy4927
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views8 pages

AI Report

The document discusses artificial intelligence (AI) and its capability to mimic human cognitive functions, with a focus on an AI application for email spam filtering. It details the implementation of a spam filter using Python and various machine learning techniques, including data preprocessing, model training, and evaluation through accuracy, confusion matrix, and ROC curve. The report concludes with a function to classify emails as spam or ham based on the trained model.

Uploaded by

omarhamdy4927
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Introduction

Artificial Intelligence is The technology that allows computers and other


devices to mimic human learning, comprehension, problem-solving,
decision-making, creativity, and autonomy is known as artificial
intelligence (AI).AI-enabled apps and gadgets are able to see and
recognize items. They are able to comprehend and react to human words.
They can pick up new knowledge and skills. They can provide consumers
and specialists with thorough advice. They can act on their own.

Application
In this report, one of the applications of AI is explained. Email spam filter
is an AI application which can be used in big work fields such as
universities and big companies, such feature saves time and effort to
remove unneeded emails.

Simulation
Our work is done using a colab application using python code. the code is
as follows:
# Step 1: Import necessary libraries

!pip install scikit-learn pandas

import pandas as pd

from sklearn.model_selection import train_test_split

from sklearn.feature_extraction.text import TfidfVectorizer

from sklearn.naive_bayes import MultinomialNB

from sklearn.metrics import classification_report, accuracy_score

# Step 2: Load the dataset

# Replace the path below with your actual file path in Colab

csv_path = '/content/spam_ham_dataset1.csv' # Update this path as needed

data = pd.read_csv(csv_path)

# Step 3: Preprocess the data

X = data['text']

y = data['label_num']

# Convert text to TF-IDF features

 vectorizer = TfidfVectorizer(stop_words='english', max_features=5000)

X_tfidf = vectorizer.fit_transform(X)

# Split the dataset into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(X_tfidf, y, test_size=0.2,


random_state=42)

# Step 4: Train the machine learning model

model = MultinomialNB( )

model.fit(X_train, y_train)

# Step 5: Evaluate the model

y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)

report = classification_report(y_test, y_pred)

print(f"\nAccuracy: {accuracy * 100:.2f}%")

print("\nClassification Report:\n")

print(report)

import matplotlib.pyplot as plt

from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay, roc_curve, auc

# Step 6: Confusion Matrix

conf_matrix = confusion_matrix(y_test, y_pred)

disp = ConfusionMatrixDisplay(conf_matrix, display_labels=["Ham","Spam"])

disp.plot(cmap='Blues')

plt.title("Confusion Matrix")

plt.show()

# Step 7: ROC Curve

y_prob = model.predict_proba(X_test)[:, 1] # Get probabilities for the positive class

fpr, tpr, thresholds = roc_curve(y_test, y_prob)

roc_auc = auc(fpr, tpr)

plt.figure()

plt.plot(fpr, tpr, color='blue', label=f"ROC curve (AUC = {roc_auc:.2f})" )

plt.plot([0, 1], [0, 1], color='red', linestyle='--') # Diagonal line

plt.xlabel("False Positive Rate")

plt.ylabel("True Positive Rate")

plt.title("Receiver Operating Characteristic (ROC) Curve")

plt.legend(loc="lower right")

plt.grid()

plt.show()

# Step 8: Visualizing Precision and Recall

from sklearn.metrics import precision_recall_curve

precision, recall, _ = precision_recall_curve(y_test, y_prob)


plt.figure()

plt.plot(recall, precision, color='green', label="Precision-Recall curve")

plt.xlabel("Recall")

plt.ylabel("Precision")

plt.title("Precision-Recall Curve")

plt.legend(loc="lower left")

plt.grid()

plt.show()

# Function to classify a single email as spam or ham

def classify_email(email_text):

email_tfidf = vectorizer.transform([email_text]) # Transform the email text into TF-IDF


features

prediction = model.predict(email_tfidf) # Predict using the trained model

return "Spam" if prediction[0] == 1 else "Ham"

# Example usage

example_email = "Subject: Meeting Reminder\nHi team, just a reminder about the


meeting scheduled for tomorrow at 10 AM in the conference room."

classification = classify_email(example_email)

classification

Results
1. Accuracy
2. Confusion Matrix

3. ROC Curve
4. Precision and Recall curve

You might also like