EZ LearnEZ Learn
Courses
EZ Board
Dashboard
All articles
ml-for-cyber-securitymlcsisolation-forestone-class-svm

Purpose-Built Anomaly Detectors

Clustering finds structure as a side effect of grouping. These three algorithms are built specifically to answer one question for one point at a time: how anomalous is this?

Ashish Revar25 October 20265 min read

Clustering finds structure as a side effect of grouping; three other algorithms are built specifically to answer "how anomalous is this point," and are the ones most often wired directly into an intrusion detection pipeline.

MethodHow it decides a point is anomalous
Isolation ForestRandomly splits features until a point is isolated; anomalies isolate in fewer splits because they sit apart from the crowd
One-Class SVMLearns a boundary around the dense region of normal data; anything outside the boundary is flagged
Local Outlier FactorCompares a point's local density to its neighbours' local density; a point in a sparse neighbourhood scores as anomalous even if the dataset as a whole is dense elsewhere

isolation_forest.py

from sklearn.ensemble import IsolationForest

iso = IsolationForest(n_estimators=200, contamination=0.02, random_state=3)
iso.fit(scaled)
anomaly_scores = iso.decision_function(scaled)   # higher = more normal
is_anomaly = iso.predict(scaled) == -1           # True = flagged

ocsvm_and_lof.py

from sklearn.svm import OneClassSVM
from sklearn.neighbors import LocalOutlierFactor

ocsvm = OneClassSVM(kernel="rbf", nu=0.02, gamma="scale")
ocsvm.fit(scaled)
ocsvm_flagged = ocsvm.predict(scaled) == -1

# LOF has no separate .fit()/.predict(): it scores the training set itself
lof = LocalOutlierFactor(n_neighbors=20, contamination=0.02)
lof_flagged = lof.fit_predict(scaled) == -1

ML concept: novelty versus outlier mode

By default, LocalOutlierFactor and OneClassSVM operate in outlier detection mode: they score the data they were fit on. Set novelty=True on LocalOutlierFactor to instead fit once on known-clean historical data and then call .predict() on new, incoming flows, the mode an actual production IDS needs, since real traffic arrives continuously, not as one fixed batch.

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

Purpose-Built Anomaly Detectors

Hi! I'm EZ Mentor

Your AI tutor for
Purpose-Built Anomaly Detectors

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