EZ LearnEZ Learn
Courses
EZ Board
Dashboard
All articles
ml-for-cyber-securitymlcsnlptfidf

NLP Features for Phishing Email Detection

Phishing lives or dies on language. This is where a real, trained model's own top ten spam-indicative words, claim, verify, immediately, winner, prize, come directly from code run against a real corpus, not chosen by hand.

Ashish Revar25 October 20265 min read

Phishing lives or dies on language, so this is where natural language processing earns its place in a security curriculum. The simplest useful representation is a bag of words: count how often each word appears, optionally weighted by TF-IDF (term frequency, inverse document frequency), which down-weights words so common they carry no signal ("the," "and") and up-weights words that are rare overall but concentrated in one class.

phishing_tfidf_pipeline.py

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline

pipeline = Pipeline([
    ("tfidf", TfidfVectorizer(stop_words="english", ngram_range=(1, 2),
                               min_df=2, max_features=5000)),
    ("clf", LogisticRegression(max_iter=2000, class_weight="balanced")),
])

pipeline.fit(email_texts_train, labels_train)
print(f"Test accuracy: {pipeline.score(email_texts_test, labels_test):.3f}")

ML concept: Why ngram_range=(1, 2)?

A pure bag-of-words model (unigrams only) sees "urgent" and "action" as two independent signals. Setting ngram_range=(1, 2) also counts adjacent word pairs (bigrams), so the model can separately learn that "urgent action" specifically, not just the two words in isolation, is a strong phishing signal. Bigrams roughly double the vocabulary size, so max_features and min_df matter more once they are enabled, to avoid an explosion of rare, overfit-prone bigrams.

inspect_top_words.py

import numpy as np

feature_names = np.array(pipeline.named_steps["tfidf"].get_feature_names_out())
coefficients = pipeline.named_steps["clf"].coef_[0]
top_spam_words = feature_names[np.argsort(coefficients)[-10:]]
print(top_spam_words)
Which words actually push a message toward "spam"claimverifyimmediatelywinnerprizeselectedaccountcardurgentwinLogistic regression coefficient (higher = more spam-indicative)
The actual top ten words a Logistic Regression model learned to associate with spam, from a model trained for this course. Every one of these numbers comes from the code above, run against a real text corpus, not chosen by hand to make a point

Tool in focus: A Public Benchmark: the SMS Spam Collection

For text classification specifically, the SMS Spam Collection dataset (compiled by Tiago Almeida and Jose Hidalgo, and hosted on the UCI Machine Learning Repository) is the field's most widely used public benchmark: 5,574 real, hand-labelled English SMS messages, about 13 percent of them spam. It is small enough to train on a laptop in seconds and realistic enough that a model's performance on it transfers meaningfully to real deployment, which is why it appears in so many of the companion code repositories this course draws on.

Related to this topic

Continue learning

Reference material

eBook

ML for Cyber Security eBook

v1.0

Open resource

Cheatsheet

ML for Cyber Security Cheatsheet

v1.0

Open resource

MCQ Bank

ML for Cyber Security MCQ Bank

v1.0

Open resource

Question Bank

ML for Cyber Security Question Bank

v1.0

Open resource

Discussion

Loading comments…
More articlesTest your knowledge
EZ MentorAI · Free

NLP Features for Phishing Email Detection

Hi! I'm EZ Mentor

Your AI tutor for
NLP Features for Phishing Email Detection

Sign in to ask, it's free.

EZ Mentor is AI-powered · Answers may not be perfect · 10 free questions / day

EZ Learn

Structured learning across Reverse Engineering, Cloud Security and Research Methodology, plus games and practice tools. Free, and open to anyone.

Learn

  • Courses
  • EZ Articles
  • EZ Watch
  • EZ Bytes
  • EZ Talks
  • Community

Library & Exams

  • All Resources
  • eBooks & PDFs
  • Question Banks
  • Cheatsheets
  • All Exams
  • EZ Board

EZ Arena

  • EZ Games
  • Byte Rush
  • Firewall Defence
  • 3D Case Files
  • Live Quiz
  • ML Challenges

EZ Cyber Hygiene

  • Hygiene Journey
  • Cyber Safety Check
  • दिशा - Hygiene
  • सजग - Awareness
  • अभ्यास - Practice
  • Scammed? Call 1930

Platform

  • About
  • Contact
  • Events
  • Verify Certificate
  • Terms of Use
  • Disclaimer

© 2026 EZ Learn by EpochZero. Educational content for learning purposes.

Platform Lead: Ashish Revar