Header Ads Widget

Responsive Advertisement

Ticker

6/recent/ticker-posts

Day-7 – Decision Tree + Model Comparison

Now we move from basic ML → smarter ML models.


🎯 Goal of Day-7

We will:

✅ Understand Decision Tree
✅ Build a better classification model
✅ Compare models (Logistic vs Tree)
✅ Think like ML engineer


🧠 What is Decision Tree?

A Decision Tree works like if-else logic (very close to C# thinking).

Example:

IF Hours > 4 → Pass
ELSE → Fail

But instead of you writing rules → model learns rules automatically.


🧠 Visual Idea

Think like:

 Hours  >  4?
/              \
Yes         No
Pass        Fail

👉 This is why it's called a "Tree".


🚀 Part 1 – Import Library

from sklearn.tree import DecisionTreeClassifier
import pandas as pd


🚀 Part 2 – Dataset

data = {
"Hours": [1,2,3,4,5,6,7],
"Pass": [0,0,0,0,1,1,1]
}

df = pd.DataFrame(data)


🚀 Part 3 – Prepare Data

X = df[["Hours"]]
y = df["Pass"]


🚀 Part 4 – Train Model

model = DecisionTreeClassifier()
model.fit(X, y)


🚀 Part 5 – Predict

new_data = pd.DataFrame({"Hours": [3.5]})

prediction = model.predict(new_data)

print("Prediction:", prediction)


🚀 Part 6 – Visualize Tree (Optional but Powerful)

from sklearn.tree import plot_tree
import matplotlib.pyplot as plt

plt.figure(figsize=(6,4))
plot_tree(model, feature_names=["Hours"], class_names=["Fail","Pass"], filled=True)
plt.show()

👉 This shows actual decision logic learned.


🧠 Key Difference (Important)


🧠 When to Use What?

  • Linear Regression → straight-line relationships
  • Logistic Regression → simple classification
  • Decision Tree → complex rules

Github Link: https://github.com/dotnetfullstackdeveloper/ai-engineer-journey/blob/main/Week-02-Machine-Learning/Day-7:%20Decision%20Tree%20+%20Model%20Comparison

Post a Comment

0 Comments