Skip to main content

Lesson 6 of 12

Step 5: Make Predictions on New Emails

With a trained and evaluated model, we can now make predictions on completely new emails it has never seen.

10 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Predict New Emails

Predict New Emails
# Making predictions on new, unseen emails
# (Assumes model and vectorizer are already trained from Step 3-4)

import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB

# Train model (same code as before - condensed)
emails = [
    ("Hi Alex, the meeting is rescheduled to Thursday at 3pm. Please confirm.", 0),
    ("WIN A FREE iPhone NOW! Click here to claim your prize immediately!", 1),
    ("Your monthly bank statement is ready. Log in to view it.", 0),
    ("CONGRATULATIONS! You have been selected for a $1000 gift card. Act fast!", 1),
    ("Can you send me the project report by end of day? Thanks, Sarah.", 0),
    ("Urgent! Your account will be suspended. Verify your information now.", 1),
    ("Reminder: Your dentist appointment is tomorrow at 10am.", 0),
    ("Make $5000 a week from home! No experience needed. Limited spots!", 1),
    ("The quarterly financial results are attached for your review.", 0),
    ("FREE pills! Lose 20 pounds in 10 days guaranteed! Order now!", 1),
    ("Team lunch is scheduled for Friday. Please RSVP by Wednesday.", 0),
    ("You won a lottery! Claim your prize of $500,000. Reply with your details.", 1),
    ("Please review the updated terms of service and provide feedback.", 0),
    ("Hot singles in your area! Click here to meet them tonight!", 1),
    ("The server maintenance will occur this Sunday from 2am to 4am.", 0),
    ("Buy cheap medications online! No prescription needed!", 1),
    ("Your package has been shipped. Track it at the link below.", 0),
    ("FINAL NOTICE: Pay now or face legal consequences. Click here.", 1),
    ("Let us catch up this week. Are you free for coffee on Tuesday?", 0),
    ("Earn unlimited income working from home. Sign up free today!", 1),
]

df = pd.DataFrame(emails, columns=["email", "label"])
vectorizer = CountVectorizer(lowercase=True, stop_words="english")
X = vectorizer.fit_transform(df["email"])
y = df["label"]
model = MultinomialNB()
model.fit(X, y)

# Now predict on NEW emails (never seen during training)
new_emails = [
    "Congratulations! You have won a free vacation to Hawaii! Click here to claim!",
    "Hi, are we still on for the team meeting at 2pm today?",
    "Limited time offer! Buy two get one free! Order before midnight!",
    "Your invoice for last month is attached. Please process payment.",
    "You have been pre-approved for a $50,000 loan! No credit check needed!",
    "The code review is ready. Please take a look when you have a chance.",
]

print("Spam Classifier - Predicting New Emails:")
print()

for email in new_emails:
    # Transform the new email using the SAME vectorizer used for training
    email_features = vectorizer.transform([email])
    
    # Get prediction and probability
    prediction = model.predict(email_features)[0]
    probabilities = model.predict_proba(email_features)[0]
    
    label = "SPAM" if prediction == 1 else "Not Spam"
    confidence = max(probabilities) * 100
    
    print(f"Email: '{email[:55]}...' " if len(email) > 55 else f"Email: '{email}'")
    print(f"  Prediction: {label} ({confidence:.1f}% confidence)")
    print()
Diagram
Loading diagram…

Educational visual guide for step 5 make predictions on new emails.

Try It Yourself

Try It YourselfHTML
HTML
✓ Valid
HTML|37 lines|✓ Valid syntax
UTF-8

Topics in This Module

0/12 done