DEV Community

Cover image for Gemika’s Enchanted Guide to Iris Dataset with Magic and Machine Learning 🌟🧙‍♂️ (Part #8)
gerry leo nugroho
gerry leo nugroho

Posted on

Gemika’s Enchanted Guide to Iris Dataset with Magic and Machine Learning 🌟🧙‍♂️ (Part #8)

Greetings, my brilliant young sorcerers! I’m Professor Gerry Leo Nugroho, your guide through the mystical arts of data science at Hogwarts, and a steadfast friend of Albus Dumbledore. Last time, we twirled into Professor Flitwick’s Charms Class, casting the K-Nearest Neighbors spell to charm our Iris Dataset into guessing flower types—like finding friends in a crowded Great Hall! 🌸

Now, my little Gryffindor shape-shifter, Gemika Haziq Nugroho, and I are marching to Professor McGonagall’s Transfiguration room. It’s time to turn numbers into a magical tree—wands up, let’s transform! 📚✨


Chapter 8: Transfiguration Triumph: Transforming with Decision Trees 🌳🪄

Imagine stepping into the stern yet wondrous Transfiguration classroom, where Professor McGonagall peers over her spectacles, her voice crisp as autumn leaves. “Nugroho,” she says, “today we’ll transfigure this Iris Dataset into a Decision Tree—a map of choices as precise as turning a teacup into a tortoise!” 🌺

With this spell, we’ll grow a tree from our flower traits—petal lengths, sepal widths—branching out with questions like “Is the petal longer than 2.5 cm?” Each branch leads to an answer—Setosa, Versicolor, or Virginica—like a Sorting Hat made of bark and leaves! It’s transfiguration at its finest—numbers into nature, chaos into clarity! 🌿🪄


8.1 The Code & Algorithm: Growing the Decision Tree

Let’s crack open our spellbook (or Jupyter Lab) and cast the DecisionTreeClassifier spell from sklearn. It’s a charm as elegant as McGonagall’s Animagus form—simple yet powerful! Here’s the magic, with a nod to my eager Gemika:

import pandas as pd  # 📜 Summoning our magical scroll-handler (pandas)!
from sklearn.datasets import load_iris  # 🌸 Summoning the legendary Iris dataset!
from sklearn.model_selection import train_test_split  # ✂️ For splitting data like a Sorting Hat!
from sklearn.tree import DecisionTreeClassifier  # 🌳 A mystical decision-making tree!

# 📜 Unraveling the ancient Iris scroll to uncover its botanical secrets!
iris = load_iris()
iris_df = pd.DataFrame(data=iris.data, columns=iris.feature_names)  # 🌸 Converting our sacred text into a DataFrame scroll!
iris_df['species'] = pd.Categorical.from_codes(iris.target, iris.target_names)  # 🧙‍♂️ Translating numbers into magical species names!

# 🏹 Separating the magical traits (features) from their names (labels)
X = iris_df.drop('species', axis=1)  # 🌱 Flower traits: Sepal & Petal spells!
y = iris_df['species']  # 🌺 The true identity of each bloom!

# 🎩✨ The Sorting Hat moment! Randomly assigning blooms to training & test groups!
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 📖 80% of the flowers are for training (learning magic), 20% for testing (proving their skills)!

# 🌳 Casting a spell to grow our Decision Tree!
tree = DecisionTreeClassifier(max_depth=3, random_state=42)  # A tree with 3 powerful levels!
tree.fit(X_train, y_train)  # 🌿 Absorbing knowledge from the training data like an ancient Ent!

# 🔮 The tree attempts to predict the future!
y_pred = tree.predict(X_test)  # 🌟 Making educated guesses based on training spells!

# 🕵️‍♂️ Let’s see if our tree is truly wise!
print("🔍 First few guesses:", y_pred[:5])  # 🧙‍♀️ What species does our tree predict?
print("📜 Real answers:", y_test[:5].values)  # 📖 Comparing with the actual flower identities!
Enter fullscreen mode Exit fullscreen mode

8.1.1 What’s Sprouting Here?

  • DecisionTreeClassifier(max_depth=3): Plants our tree with a limit—three levels of branches, so it doesn’t grow wild like the Whomping Willow!
  • .fit(X_train, y_train): Roots the tree in our training data—like teaching it to sort flowers step-by-step!
  • .predict(X_test): Sends test flowers down the branches—each choice leads to a species, quicker than a Transfiguration spell!

Run this, and you might see:

First few guesses: ['versicolor' 'setosa' 'virginica' 'versicolor' 'versicolor']
Real answers: ['versicolor' 'setosa' 'virginica' 'versicolor' 'versicolor']
Enter fullscreen mode Exit fullscreen mode

Blimey—our tree’s as sharp as McGonagall’s glare! Perfect guesses—full marks! 🌟🍃


8.1.2 🔮 Peering into the Crystal Ball of Predictions

The tree has spoken! With its deep-rooted wisdom (limited to a depth of 3, for even the mightiest of trees can only stretch so far), it has attempted to foretell the fate of the test flowers. And lo, before our very eyes, it has conjured a sequence of guesses—those first few delicate predictions, standing bravely against the actual truths of nature. 🌸

8.1.3. The Great Sorting: A Test of the Tree’s Wisdom**

Our y_pred (the tree’s guesses) are compared against y_test (the true species), much like a first-year student clutching their wand for the first time, hoping—nay, praying—that they can cast a proper Lumos instead of setting their robes on fire.

If our tree is as wise as Dumbledore, we should see most predictions aligning with reality. However, should it falter, we may find mischievous misclassifications lurking like Peeves the Poltergeist, sowing chaos in our botanical records! 🌪️

8.1.4 📜 What Do These Predictions Tell Us?

Should our predictions match the real labels perfectly, then rejoice, dear scholar, for we have trained a truly sagacious tree, one that knows its petal from its sepal and its virginica from its versicolor. But should errors creep in… well, then, we must consider the limits of our spell:

1. 🌿 The Depth Dilemma

  • Our tree is only three levels deep. Had we let it grow unchecked, stretching its limbs toward the heavens, it might have remembered every single flower from our training data perfectly—but at the terrible cost of generalizing poorly when confronted with new blooms. This is the curse of overfitting, much like an overzealous Divination student seeing omens of doom in every teacup. ☕🔮

2. 🧙‍♀️ The Balance of Magic & Logic

  • If errors appear, it is not necessarily our tree’s failing! The dataset itself may contain overlapping patterns, flowers that look suspiciously alike, like Fred and George Weasley pulling their classic switcheroo. In such cases, even our most powerful spells may falter slightly.

8.1.5 🎓 The Final Verdict

If the tree’s guesses are mostly correct, then huzzah! 🎉 We have wielded our magic well, crafting a model that is not only powerful but balanced—like a well-brewed Polyjuice Potion. Should it stumble in its classification, fear not! We can train deeper trees, tweak parameters, or even explore the mystical arts of random forests—a whole grove of decision trees working together in harmony. 🌳🌳🌳

So, what say you, young sorcerer of data? Shall we refine our spell, or does this tree stand tall, its roots firmly planted in truth? The choice, as always, is yours. ⚡📖✨


8.2 Hogwarts Application: Sorting Students into Houses

Picture Professor McGonagall tapping her wand, saying, “Gerry, sort these first-years with a Decision Tree!” We’d feed it traits—bravery, wit, loyalty, ambition—and watch it branch: “Bravery over 7? Gryffindor! Wit above 8? Ravenclaw!” Just like sorting Irises, it’d place students into Houses with crisp precision—no need for the Sorting Hat’s mumbling. Even Harry’s scarf would flutter with approval! 🏰🧣✨


8.3 Gemika’s Quiz Time! �

My little Gemika, sketching a tree with crayons, looks up with a grin. “Abi,” he wonders, “how does a tree decide which Iris flower is which?” I ruffle his hair—he’s growing as fast as a Venomous Tentacula! Pick your answer, young transfigurers:

  • A) It asks each flower a riddle and picks based on the answer.
  • B) It grows branches with questions, guiding flowers to their type—like a map!
  • C) It shakes its leaves and hopes the right name falls out.

Scribble your guess or shout it louder than a Transfigured teapot—Gemika’s all ears! 🗣️📝✨ (Hint: Think branches, not luck!)


8.4 Next Chapter: A Forest of Wonders

Hold your broomsticks, because next we’re summoning a Random Forest—a whole grove of magical trees! We’ll turn our Iris guesses into a council of wisdom, like Dumbledore’s advisors. It’ll be so grand, even Hagrid might bring a picnic! Get ready for more magic and a rustle of leaves! 🌲🪄✨

Top comments (0)