top of page

Decision-Trees Algorithm

About the Algorythm Recipe 🍰

Decision trees: like a choose-your-own-adventure book for data, guiding you through the twists and turns of information to make smart choices with a leafy simplicity.

Cookin' time! 🍳

Imagine you're playing a guessing game with a friend. You have a secret object in mind, and your friend tries to guess what it is by asking yes-or-no questions. Each question splits the possibilities in half, making it easier to guess the right answer. That's essentially how a decision tree algorithm works!


Here's the breakdown:


1. The Tree: Think of it as a flowchart with a starting point (root) and branches leading to different outcomes (leaves).


2. The Questions: Each branch asks a question based on a specific feature of the data. For example, "Is the object furry?"


3. The Answers: Depending on the answer, you follow the corresponding branch, narrowing down the possibilities. "Yes, it's furry" takes you to another question, while "No, it's not furry" might lead you straight to the answer (e.g., "Rock").


4. The Goal: The algorithm builds the tree by choosing the best questions at each level, eventually reaching leaves that represent the final predictions or classifications.


Here are some real-world examples:

  • Email spam filter: Is the email from a known sender? No? Is it about money? Yes? Then it might be spam.

  • Recommending movies: Do you like comedies? Yes? Do you like action movies? Yes? Then you might enjoy this movie.

  • Diagnosing a disease: Does the patient have a fever? Yes? Does the patient have a cough? No? Then it's probably not the flu.

Why are decision trees cool?

  • Easy to understand: The tree structure makes it visually clear how the algorithm makes decisions.

  • Interpretable: You can see which features are most important for making the prediction.

  • Works with different data: It can handle both numerical and categorical data.


However, they also have limitations:


  • Can be overfitting: They can become too specific to the training data and perform poorly on new data.

  • Not always the most accurate: There might be more complex algorithms that give better results for certain tasks.


Overall, decision tree algorithms are a powerful tool for making predictions and gaining insights from data, offering a clear and understandable way to make informed decisions.


from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score

# Load the Iris dataset
iris = load_iris()
X = iris.data
y = iris.target

# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Initialize the decision tree classifier
clf = DecisionTreeClassifier()

# Train the classifier on the training data
clf.fit(X_train, y_train)

# Make predictions on the testing data
y_pred = clf.predict(X_test)

# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)

This code loads the Iris dataset, splits it into training and testing sets, initializes a decision tree classifier, trains the classifier on the training data, makes predictions on the testing data, and calculates the accuracy of the model.

bottom of page