K-Means হলো একটি popular unsupervised learning algorithm।

এখানে target label থাকে না।

Model-এর goal হলো similar data points-কে groups বা clusters-এ ভাগ করা।

Example

ধরুন আমাদের customer data আছে:

Order Frequency
Average Order Value
Days Since Last Order

কিন্তু customer কোন type-এর সেটা label করা নেই।

K-Means হয়তো data থেকে এমন clusters বের করতে পারে:

Cluster 1 → High-value frequent users
Cluster 2 → Low-value frequent users
Cluster 3 → Occasional users
Cluster 4 → Dormant users

Cluster-এর business name algorithm দেয় না।

Business interpretation আমরা পরে করি।

K কী?

K হলো কতগুলো cluster চাই।

যেমন:

K = 3

মানে algorithm data-কে 3টি cluster-এ divide করার চেষ্টা করবে।

K-Means Algorithm

Basic process:

1. K centroids initialize করুন

2. প্রতিটি point nearest centroid-এর
   cluster-এ assign করুন

3. প্রতিটি cluster-এর points-এর mean
   calculate করুন

4. সেই mean-কে new centroid বানান

5. Assignment আবার করুন

6. Repeat করুন যতক্ষণ centroids
   প্রায় stable না হয়

Centroid কী?

Centroid হলো cluster-এর center।

ধরুন cluster-এর values:

2
4
6

Mean:

4

তাহলে simple 1D case-এ centroid:

4

Objective Function

K-Means চেষ্টা করে cluster-এর ভেতরের points যেন centroid-এর কাছাকাছি থাকে।

Objective:

$$\sum_{i}\| x_{i} - \mu_{c_{i}}\|^{2}$$

যেখানে:

x_i
→ Data point

μ_(c_i)
→ Assigned cluster-এর centroid

এটাকে commonly:

Within-Cluster Sum of Squares

বা:

WCSS

বলা হয়।

সহজ Intuition

Good clustering হলে:

Same cluster-এর points
→ কাছাকাছি

Different clusters
→ relatively আলাদা

Scaling কেন Important?

ধরুন features:

Age    → 18–60
Income → 20,000–500,000

K-Means distance ব্যবহার করে।

Income-এর scale অনেক বড় হওয়ায় distance calculation income dominate করতে পারে।

তাই numerical features সাধারণত standardize করা হয়।

StandardScaler
↓
K-Means

Outlier Problem

ধরুন বেশিরভাগ customer spending:

৳500–৳5,000

কিন্তু একজন:

৳1,000,000

Mean-based centroid outlier-এর কারণে shift করতে পারে।

তাই K-Means outlier sensitive।

Cluster Shape

K-Means সবচেয়ে ভালো কাজ করে যখন clusters roughly:

Compact
Spherical
Well-separated

কিন্তু data যদি এমন হয়:

Moon shape
Ring shape
Irregular shape

তাহলে K-Means poor clustering করতে পারে।

Limitations

K-Means-এর common limitations:

  • K আগে specify করতে হয়

  • feature scaling sensitive

  • outlier sensitive

  • initialization-এর উপর result depend করতে পারে

  • irregular cluster shape handle করতে সমস্যা হয়

K কীভাবে choose করবেন?

Common methods:

Elbow Method
Silhouette Score
Domain Knowledge
Cluster Stability
Business Usefulness

Interview-ready Explanation

K-Means is an unsupervised clustering algorithm that
partitions data into K groups.

It repeatedly assigns each point to the nearest centroid
and then updates each centroid using the mean of the
assigned points.

The objective is to minimize within-cluster squared distance.

K-Means works best for compact and roughly spherical
clusters, but it is sensitive to feature scaling,
outliers, initialization, and the chosen value of K.