Header Ads Widget

Responsive Advertisement

Ticker

6/recent/ticker-posts

Day-14 – Unsupervised Learning (K-Means Clustering)

Welcome to Day-14 – Unsupervised Learning (K-Means Clustering)

Till now:

  • You had labeled data (Pass = 0/1)

Today:

Model finds patterns WITHOUT labels 🤯

This is called:  Unsupervised Learning


🎯 Goal of Day-14

You will:

✅ Understand clustering
✅ Learn K-Means algorithm
✅ Group similar data automatically
✅ Visualize clusters


🧠 What is Clustering?

Grouping similar data points together.

Example:

  • Customers with similar spending habits
  • Similar products
  • Similar users

WITHOUT predefined labels.


🧠 Real-Life Examples

Use CaseExample
    E-commerce                      Customer segmentation
    Netflix                      Similar viewers
    Banking                       Fraud pattern groups
    Marketing                      Target audience        
        

🧠 K-Means Concept

Algorithm tries to create:

K groups (clusters)

Example:

K = 2

Means: 2 clusters


🧠 Visual Idea

Think like:

📊 Customer Groups

 








🚀 Part 1 – Import Libraries

import pandas as pd
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans


🚀 Part 2 – Create Dataset

Example:

  • Study hours
  • Marks
data = {
"Hours": [1,2,3,4,5,6,7,8],
"Marks": [40,42,50,54,70,78,90,95]
}

df = pd.DataFrame(data)


🚀 Part 3 – Visualize Data

plt.scatter(df["Hours"], df["Marks"])

plt.xlabel("Hours")
plt.ylabel("Marks")
plt.title("Student Data")

plt.show()


🚀 Part 4 – 

Create KMeans Model

plt.scatter(df["Hours"], df["Marks"])

plt.xlabel("Hours")
plt.ylabel("Marks")
plt.title("Student Data")

plt.show()


Create KMeans Mode

model = KMeans(
n_clusters=2,
random_state=42
)


🧠 Meaning

n_clusters=2

👉 Create 2 groups.


🚀 Part 5 – Train Model

df["Cluster"] = model.fit_predict(
df[["Hours", "Marks"]]
)

print(df)


🚀 Part 6 – Visualize Clusters

plt.scatter(
df["Hours"],
df["Marks"],
c=df["Cluster"]
)

plt.xlabel("Hours")
plt.ylabel("Marks")
plt.title("K-Means Clustering")

plt.show()

🧠 What Happened?

Model automatically grouped:

  • Similar students together

Without:

Pass / Fail labels


🚀 Part 7 – Cluster Centers

print(model.cluster_centers_)

These are:

center points of clusters


🧠 Key Difference

Supervised Learning                 Unsupervised Learning
Has labels                                    No labels
Predict output                              Find hidden patterns
Example: Pass/Fail                      Example: Customer groups


🧠 Important Concept – Elbow Method

How to choose best K?

We use:

Elbow Method

(We’ll learn advanced version later.)


⚠ Important Interview Question

Q: What is unsupervised learning?

Answer:

A type of ML where models find hidden patterns in unlabeled data.


🧠 Real AI Insight

Clustering is heavily used in:

  • Recommendation systems
  • Fraud detection
  • Market segmentation
  • Behavioral analysis

🎯 End of Day-14 Goals

You now:

✅ Understand unsupervised learning
✅ Built clustering model
✅ Visualized clusters


Github link: https://github.com/dotnetfullstackdeveloper/ai-engineer-journey/blob/main/Week-02-Machine-Learning/Day-14%20%E2%80%93%20Unsupervised%20Learning%20(K-Means%20Clustering)

Post a Comment

0 Comments