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

Lexical URL Features: TF-IDF as an Alternative to Hand-Engineering

Unit 2 classified phishing URLs using hand-engineered features. There is a second, complementary approach: treat the URL itself as text, tokenise it, and let TF-IDF discover the discriminative substrings automatically.

Ashish Revar25 October 20265 min read

Unit 2 classified phishing URLs using hand-engineered features (URL length, digit ratio, has-IP-address). There is a second, complementary approach worth knowing: treat the URL itself as text, tokenise it, and let TF-IDF discover the discriminative substrings automatically, exactly the way Unit 1 and this unit treat email and message text.

url_tokenize.py

def url_tokenize(url: str) -> list[str]:
    """Split a URL into its slash-, dot-, and hyphen-separated parts,
    the same token boundaries a human skimming a URL actually uses."""
    url = url.lower()
    tokens = []
    for segment in url.split("/"):
        for part in segment.split("-"):
            tokens.extend(part.split("."))
    tokens = [t for t in set(tokens) if t and t != "com"]
    return tokens

url_tfidf_classifier.py

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

url_vectorizer = TfidfVectorizer(tokenizer=url_tokenize)
X_urls = url_vectorizer.fit_transform(url_list)

url_model = LogisticRegression(max_iter=2000)
url_model.fit(X_urls, labels)

# scoring a brand-new URL at prediction time
new_urls = ["paypal-secure-login.account-verify.ru", "github.com"]
predictions = url_model.predict(url_vectorizer.transform(new_urls))

ML concept: Lexical versus Structured URL Features: Which to Use?

Hand-engineered structured features (Unit 2) are interpretable and cheap to compute, but require a human to think of every useful pattern in advance. Lexical TF-IDF features discover patterns automatically, including ones a human would never think to hand-code, such as a specific brand name appearing in a subdomain it has no business being in, but are harder to explain to a non-technical stakeholder and can overfit to coincidental vocabulary in a particular training set. Production phishing-URL classifiers commonly use both feature families together, exactly as this unit's phishing-email pipeline later combines text and structural features.

Try it yourself: Tokenise It Yourself

Trace url_tokenize("paypal-secure-login.account-verify.ru") by hand, segment by segment, and list the final set of tokens it would produce. Which of those tokens would you expect a trained classifier to weight most heavily toward phishing, and why?

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

Lexical URL Features: TF-IDF as an Alternative to Hand-Engineering

Hi! I'm EZ Mentor

Your AI tutor for
Lexical URL Features: TF-IDF as an Alternative to Hand-Engineering

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