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

k-Means: Partitioning Traffic into K Groups

k-means iterates between assigning points to their nearest centroid and recomputing each centroid, until the assignments stop changing. Injected scan-burst traffic sits so far from every centroid that a distance threshold catches it without ever needing a label.

Ashish Revar25 October 20265 min read

k-means asks for a number of clusters, $k$, chosen in advance, and finds $k$ centroids that minimise the total squared distance from each point to its nearest centroid. It iterates: assign every point to its nearest centroid, recompute each centroid as the mean of its assigned points, repeat until the assignments stop changing.

  1. Choose $k$ and initialise $k$ centroids (k-means++ in scikit-learn picks good starting points automatically).
  2. Assign every point to its nearest centroid.
  3. Move each centroid to the mean of the points now assigned to it.
  4. Repeat steps 2 and 3 until assignments stop changing (or a maximum iteration count is reached).

kmeans_network_flows.py

from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans

# flow_features: packets/second, mean packet size, flow duration, ...
scaled = StandardScaler().fit_transform(flow_features)

kmeans = KMeans(n_clusters=3, n_init=10, random_state=3)
labels = kmeans.fit_predict(scaled)
Standardised feature 1 (e.g. packets/second)Standardised feature 2 (e.g. mean packet size)
k-means finds three clusters of normal-looking traffic. The injected scan-burst points (triangles) sit far from every centroid; a simple distance-to-nearest-centroid threshold would flag them without ever needing a label

Caution: k-Means Needs k, and Needs Scaled Features

k-means has two sharp edges. First, you must choose $k$ in advance; choosing it badly (too few, too many) produces clusters that do not correspond to anything meaningful. Second, k-means uses raw Euclidean distance, so a feature measured in the thousands (bytes transferred) will dominate a feature measured in single digits (TCP flags set) unless every feature is standardised first, exactly as StandardScaler does above.

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

k-Means: Partitioning Traffic into K Groups

Hi! I'm EZ Mentor

Your AI tutor for
k-Means: Partitioning Traffic into K Groups

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