NelworksNelworks

ML & AI / Season 1 / EP04

K-means - Clustering That Is Outlier Sensitive

Shez

I created the future of logistics! I reorganized the entire inventory system!

Kurumi

By "reorganized," do you mean "randomized"?

Shez

No! I used Machine Learning! **K-Means Clustering**.

Shez

I grouped all 50,000 SKUs based on Price and Sales Velocity.

Kurumi

Explain that shelf.

Shez

Oh, that's Cluster_3. The "Mid-Tier" cluster.

Kurumi

You think a handbag and toilet paper belong in the same logistics group?

Shez

Mathematically, yes! They fell into the same Voronoi cell!

Kurumi

Shez, you used K-Means on `Price` vs `Volume`. Let me guess. You normalized the data first?

Shez

O-Of course! `StandardScaler`. I'm not an amateur.

Kurumi

And how did you pick `k`?

Shez

The Elbow Method! I plotted the inertia. The curve bent at `k=4`. I've been learning!

Kurumi

Then you haven't finished your lesson. You used a spherical cookie cutter on dough that is shaped like a baguette.

Kurumi

Your data looks like this. Long streaks. Elongated shapes.

Kurumi

But K-Means assumes **Spheres**. It assumes the data is a ball.

Kurumi

Look at Cluster 3. It grabbed the expensive end of the cheap items and the cheap end of the expensive items.

Shez

But... the centroid minimizes the variance!

Kurumi

It minimizes *Euclidean distance*.

Kurumi

It draws a circle around a center. It doesn't care about the *shape* or *density* of the group.

Kurumi

K-Means places a gravity well. If a point is close to the center in straight-line distance, it gets sucked in.

Shez

But isn't that what we want? Things that are close together?

Kurumi

What if your data is shaped like a banana? Or a ring?

Kurumi

The average of a banana is in the empty space between the ends.

Kurumi

K-Means will put the centroid here. And it will split the banana in half.

Kurumi

What is that?

Shez

That's the "Super Prototype Engine." We only sold one. It costs $1,000,000.

Kurumi

And why is it grouped with these $50 blenders?

Shez

Uh... well...

Kurumi

That Engine is an **Outlier**.

Kurumi

In K-Means, the centroid is the *Mean*. The Mean is a slave to outliers.

Kurumi

$(1 + 1 + 1 + 1000) / 4 = 251$.

Kurumi

The outlier dragged the average from 1 to 251. Now the center of your cluster is nowhere near the actual data.

Shez

So my "Mid-Tier" cluster is actually "Cheap Blenders + One Jet Engine"?

Kurumi

Yup. You wanted "Segmentation." K-means gave you "Mathematical Averaging."

Shez

But everyone uses K-Means! It's in every Youtube tutorial! "Customer Segmentation 101"!

Kurumi

Because it's easy to teach. And it works... if your customers are spherical blobs floating in a vacuum.

Kurumi

Real data has covariance. Real data has shapes.

Kurumi

High-end items have high price variance but low volume variance. Cheap items are the opposite.

Kurumi

You need a shape that can stretch. You need to model the **Covariance Matrix**.

Kurumi

Enter **GMM** (Gaussian Mixture Models).

Shez

Gaussian... Mixture?

Kurumi

K-Means puts a hard fence around a circle. GMM puts a soft, probabilistic cloud over an ellipse.

Kurumi

GMM learns the *shape* of the cluster. It learns that "Luxury" means "High Price range, but narrow Volume range."

Kurumi

And because it's probabilistic, it knows the Jet Engine doesn't really fit. It's a "Low Likelihood" point.

Shez

It... it actually makes sense now.

Shez

So K-Means is... trash?

Kurumi

No. K-Means is a **Compression** algorithm.

Kurumi

It's great for compressing colors in an image (Vector Quantization). It's great when you want to partition space evenly for indexing.

Kurumi

But for **Inference**? For understanding the *structure* of your business? It is a blunt instrument.

Shez

I'm switching to GMM! Or maybe DBSCAN?

Kurumi

DBSCAN is good if you have weird shapes and noise, but it fails if the density varies too much.

Kurumi

Inventory data usually has varying density. GMM is your safest bet for this.

Kurumi

Just remember: **Covariance Type = 'full'**. Don't let it force spheres again.

Shez

`GaussianMixture(n_components=4, covariance_type='full')`.

Shez

Running it now.

Shez

It works! The robot knows the engine is special!

Kurumi

Another crisis averted. You're welcome.

Shez

Wait! How do I pick `k` for GMM? Do I just guess?

Kurumi

Elbow. Silhoutte score. **BIC** (Bayesian Information Criterion).