Greetings, my daring young spellcasters! I’m Professor Gerry Leo Nugroho, your guide through the wondrous skies of data science at Hogwarts, and a loyal friend of Albus Dumbledore. Last time, we climbed to Dumbledore’s office and summoned a Random Forest—a grove of enchanted trees that predicted our Iris Dataset species with the wisdom of a hundred professors! 🌲 Now, my little Gryffindor phoenix, Gemika Haziq Nugroho, and I are watching Fawkes take flight. It’s time to measure our magic’s might—how well did our spells soar? ✨📊
Chapter 10: The Phoenix’s Flight: Evaluating Our Magical Models 🐦🔮
Imagine Fawkes, Dumbledore’s radiant phoenix, gliding above the castle, his fiery feathers trailing sparks. “Gerry,” Dumbledore twinkles, “let’s see if your spells match Fawkes’ grace!” Today, we’re evaluating our KNN, Decision Tree, and Random Forest charms on the Iris Dataset. 🌺 Did they guess Setosa
, Versicolor
, and Virginica
correctly? We’ll use magical tools—accuracy and confusion matrices
—to count our triumphs, like tallying points in the House Cup! It’s a dazzling flight of numbers, proving our magic’s as strong as a phoenix reborn! 🔥🪄
10.1 The Code & Algorithm: Summoning the Random Forest
Let’s open our spellbook (or Jupyter Lab
) and cast evaluation spells with sklearn
’s accuracy_score
and confusion_matrix
. These charms shine light on our predictions like Fawkes illuminating a dark night! Here’s the magic, with a wink to my curious Gemika:
# Summoning our evaluation tools
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, confusion_matrix
# Loading and splitting our Iris scroll
iris = load_iris()
iris_df = pd.DataFrame(data=iris.data, columns=iris.feature_names)
iris_df['species'] = pd.Categorical.from_codes(iris.target, iris.target_names)
X = iris_df.drop('species', axis=1)
y = iris_df['species']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Casting our spells (KNN, Tree, Forest)
knn = KNeighborsClassifier(n_neighbors=5)
tree = DecisionTreeClassifier(max_depth=3, random_state=42)
forest = RandomForestClassifier(n_estimators=100, random_state=42)
models = {'KNN': knn, 'Decision Tree': tree, 'Random Forest': forest}
for name, model in models.items():
model.fit(X_train, y_train) # Train the spell
y_pred = model.predict(X_test) # Cast it on test data
# Spell 1: Accuracy—like counting Phoenix feathers!
acc = accuracy_score(y_test, y_pred)
print(f"{name} Accuracy: {acc:.2f}")
# Spell 2: Confusion Matrix—a map of right and wrong!
cm = confusion_matrix(y_test, y_pred)
print(f"{name} Confusion Matrix:\n{cm}\n")
10.2 What’s Gleaming in the Flight?
-
accuracy_score
: Counts how often our spell guessed right—like a percentage of perfect phoenix notes! (e.g., 1.00 means 100% correct!) -
confusion_matrix
: Draws a magical grid—rows are real species, columns are guesses. Diagonal numbers show wins, off-diagonals show oopsies!
Run this, and you might see:
✨ Casting the KNN spell... ✨
📊 KNN Accuracy: 1.00
🔮 KNN Confusion Matrix:
[[10 0 0]
[ 0 9 0]
[ 0 0 11]]
✨ Casting the Decision Tree spell... ✨
📊 Decision Tree Accuracy: 1.00
🔮 Decision Tree Confusion Matrix:
[[10 0 0]
[ 0 9 0]
[ 0 0 11]]
✨ Casting the Random Forest spell... ✨
📊 Random Forest Accuracy: 1.00
🔮 Random Forest Confusion Matrix:
[[10 0 0]
[ 0 9 0]
[ 0 0 11]]
Wow—KNN, Decision Tree & Forest soar at 100%! Fawkes would trill with pride! 🌟🔥
10.3 Hogwarts Application: Grading OWLs
Picture Professor McGonagall, quill in hand, grading OWL exams. “Gerry, let’s score these with precision!” With accuracy, we’d tally correct spells—100% means top marks! The confusion matrix would show where students mixed up Charms with Transfiguration—diagonals for “Outstanding,” off-diagonals for “Troll.” Just like evaluating Iris guesses, we’d ensure every witch and wizard shines—Harry’s Patronus would ace it! 📜🦇✨
10.4 Gemika’s Quiz Time! 🧑🚀
My little Gemika, gazing at Fawkes’ perch, tilts his head. “Abi,” he ponders, “how do we know if our magic worked on the Iris flowers?” I grin—he’s brighter than a phoenix feather! Pick your answer, young evaluators:
- A) We count how many guesses were right with accuracy—like scoring points!
- B) We wave a wand and hope Fawkes nods.
- C) We ask the flowers if they’re happy with the guesses.
Scribble your guess or shout it louder than a phoenix song—Gemika’s all ears! 🗣️📝✨ (Hint: Think numbers, not nods!)
10.5 Next Chapter: The Grand Prediction
Hold your broomsticks, because next we’re waving our wands for the grand finale—predicting new Iris flowers! We’ll use our tested spells to guess unknown blooms, like spotting a new creature in the Forbidden Forest. It’ll be so thrilling, even Hermione might gasp! Get ready for more magic and a sprinkle of wow! 🌸🪄✨
Top comments (0)