Do you ever ask yourself how machines make decisions like humans?
Let’s break down one of the most beginner-friendly algorithms in machine learning,
Wanna know what’s that, ta-da “Decision Tress”, with a real working demo using Python and scikit-learn.
What Is a Decision Tree?
A decision tree is a flowchart-like structure where each internal node represents a question based on a feature, each branch represents an answer, and each leaf node represents an outcome or class label.
(Deos the definition overwhelm you?)
Here is the simplified version,
*Machines ask yes/no questions ----> (just like we do)----> to make decisions. *
Real Life Analogy
That’s a decision tree for you to visualize.
The Sample Use Case
Let's Build It in Python
Import Libraries
from sklearn.tree import DecisionTreeClassifier, plot_tree
import matplotlib.pyplot as plt
Prepare the Data
X = [[25, 50000], [35, 60000], [45, 80000], [22, 30000]]
y = [0, 1, 1, 0] # 0 = No Buy, 1 = Buy
Train the Model
clf = DecisionTreeClassifier()
clf.fit(X, y)
Visualize the Tree
plt.figure(figsize=(8, 5))
plot_tree(clf, feature_names=["Age", "Income"], class_names=["No", "Yes"], filled=True)
plt.show()
Output:
How Does It Decide?
Well, thats the cool one here,the decision tree uses something called Gini Impurity to choose the best split,
If a node has all “Yes” or all “No”, it’s pure (Gini = 0).
The more mixed it is, the higher the impurity.
So, the takeaway algorithm keeps splitting where impurity is lowest.
Try making a prediction using the above code!
print(clf.predict([[40, 70000]]))
The output will be [1] //means they will buy.
Where Are Decision Trees Used?
Credit card approval
Customer churn prediction
Recommender systems
Medical diagnosis
E-commerce decision engines
okay so finally, Decision Trees are powerful, visual, and easy to understand.
If you’re learning ML or just want to understand how AI “thinks”, start with Decision Trees.
And if you want to see a live demo in action,Here's a quick youtube video me explaining How decision tress actually work,
🙌 Follow me for more beginner-friendly AI & ML breakdowns!
🚀 Need help building real AI/ML solutions? Check out SiteEncoders, we build it for you.
Top comments (1)
very helpful