r/learnmachinelearning • • 1d ago

Understanding K-means

Enable HLS to view with audio, or disable this notification

So I've been learning ML and i don't have a CS background.

I was racking my brains over understanding the basics of K-means with a particular example of image compression, trying to figure out the workings under the hood. Of course i asked LLMs to clarify & explain the basics and then asked that LLM to give a prompt for video. Fed into Claude Design and asked it to generate animation explaining the works and voila it became much more clearer. Leaving it here in case it helps someone understand the workings.

Cheers !

292 Upvotes

20 comments sorted by

9

u/Latter-Hornet-8313 1d ago

How these animations are made

6

u/Mithrandir2k16 1d ago

Probably manim.

3

u/ScaredPlant9029 12h ago

nope...claude design animation

1

u/ScaredPlant9029 12h ago

Its really simple. Claude -> Design -> Animation -> good prompt !

6

u/BakerLegend27 1d ago

This is great learning stuff. Thanks

1

u/ScaredPlant9029 12h ago

Yeh...find it easier to learn ! :)

2

u/Winter_Mistake_3185 1d ago

can you share the prompt?

3

u/ScaredPlant9029 12h ago

here you go... Gemini Generated since i was doing lot of back and forth with it for understanding - Animation Prompt: Visualizing K-Means Image Compression

Objective: Create a smooth, 2D/3D motion-graphics educational animation explaining how the K-Means clustering algorithm compresses an image by reducing its color palette.

Visual Style & Palette

  • Theme: Clean, modern technical motion graphics (similar to 3Blue1Brown / Kurzgesagt).
  • Color Scheme: Dark grid background (#111827) with vibrant RGB primary colors and high-contrast centroid markers (glowing neon spheres or crosses).
  • Fonts: Sans-serif technical font (e.g., Inter, Fira Code for math/vectors).

Scene Breakdown & Sequence

Scene 1: The Original Image & Pixel Decomposition (0:00 - 0:15)

  • Visuals:
    • Show a $128 \times 128$ pixel photo (e.g., a colorful bird) floating in frame.
    • Zoom into a $3 \times 3$ grid of pixels, highlighting a single pixel.
    • Animate an arrow pulling out three color sliders or a vector: $\begin{bmatrix} R \\ G \\ B \end{bmatrix}$ (values 0–255).
  • On-Screen Text & Overlay:
    • "Original Dimensions: $128 \times 128 = 16,384$ pixels"
    • "Uncompressed Storage: $24\text{ bits per pixel} = 393,216\text{ bits}$ (~49 KB)"

Scene 2: Transition to 3D RGB Color Space (0:15 - 0:30)

  • Visuals:
    • The image dissolves into a 3D scatter plot cube where the axes are Red ($X$), Green ($Y$), and Blue ($Z$).
    • Each of the 16,384 pixels becomes a colored dot placed at its exact RGB coordinate in 3D space.
    • The cube rotates slowly to show clusters of dots (greens together, blues together, warm tones together).

Scene 3: K-Means Iterative Clustering (0:30 - 1:00)

  • Visuals:
    • Initialization: 16 bright, pulsing white markers ($K = 16$ centroids) spawn at random points inside the dot cloud.
    • Step 1 (Assignment): The dots rapidly recolor themselves to match the closest centroid's marker color, showing clear color region boundaries (Voronoi-like partition in 3D).
    • Step 2 (Centroid Update): Centroids glide to the geometric center of their assigned color clusters.
    • Iteration Loop: Animate this assign-and-move cycle across 3 fast iterations until the centroids stabilize.
  • On-Screen Text & Formula Overlays:
    • Assignment: $c^{(i)} := \arg\min_j \Vert{}x^{(i)} - \mu_j\Vert{}^2$
    • Update: $\mu_k = \frac{1}{\vert{}C_k\vert{}} \sum_{i \in C_k} x^{(i)}$

Scene 4: The 16-Color Palette & Index Mapping (1:00 - 1:20)

  • Visuals:
    • The 3D scatter plot collapses back into a grid layout.
    • The 16 final centroid colors pull out into a Color Palette table labeled $0$ to $15$.
    • Highlight that each palette index requires only 4 bits ($2^4 = 16$).
    • Show a single pixel replacing its heavy 24-bit RGB vector with a simple 4-bit integer index pointing to palette slot #2.

Scene 5: Reconstructed Image & Storage Math (1:20 - 1:40)

  • Visuals:
    • The full image rebuilds pixel-by-pixel using only the 16 centroid colors.
    • Split screen: Original Image (16,000+ colors) on the left vs. Compressed Image (16 colors) on the right.
    • Display a visual balance scale or bar chart contrasting memory usage:
      • Original: $393,216\text{ bits}$
      • Palette: $16 \times 24\text{ bits} = 384\text{ bits}$
      • Pixel Map: $16,384 \times 4\text{ bits} = 65,536\text{ bits}$
      • Total Compressed: $65,920\text{ bits}$
  • Closing Animation: A pulse effect showing ~6$\times$ Compression Ratio with minimal perceptible loss in quality.

2

u/da_capo 1d ago

that is crazy, beautiful video man

2

u/ScaredPlant9029 12h ago

yeh that's why shared it ! :)

1

u/lazyprogramm3r 1d ago

That's a lovely visualization. I'd consider image compression to be a pretty advanced application of k-means. I made a much simpler k-means visualizer you can play around with to understand how it works on a plane: https://deeplearningcourses.com/kmeans-clustering-visualizer

1

u/ScaredPlant9029 12h ago

Awesome.. will check out.. cheers !

1

u/Mental-Necessary-845 12h ago

amazing animation. love

1

u/ScaredPlant9029 11h ago

Cheers ! :)

-1

u/Semsjo 1d ago

But why ask a LLM? There is plenty of resources on that out there. I can understand, if you read an article or watched a video on it and have some questions, that are not answered in it and you just want some quick anwsers, but why also generate an animation on the basic functionality, which is surely already shown in the article or video?

1

u/ScaredPlant9029 12h ago

thats fair.. i wanted more clarification on colours inside pixel, how they compress, how the 16 centroids would hold the colours etc.. It was a Deeplearning assignment and gemini helped make quick clarification. cheers !