DEV Community

Cover image for The Gemika's Magical Guide to Sorting Hogwarts Students using the Decision Tree Algorithm (Part #3)
gerry leo nugroho
gerry leo nugroho

Posted on • Updated on

The Gemika's Magical Guide to Sorting Hogwarts Students using the Decision Tree Algorithm (Part #3)

3. Exploring the Enchanted Dataset 🌟

The Gemika's Magical Guide to Sorting Hogwarts Students using the Decision Tree Algorithm - Exploring the Enchanted Dataset

Gather 'round, brave witches and wizards! Let us embark on a thrilling adventure into the heart of Hogwarts – not its ancient stone corridors, but its digital soul. This enchanted dataset is our very own Marauder's Map, revealing the hidden patterns and secrets of our magical world. πŸ§™β€β™€οΈβœ¨

Imagine this dataset as a sprawling parchment, filled with intricate details about every student who has ever graced the hallowed halls. Each row is a unique character, a miniature version of Harry, Ron, or Hermione, with their own magical essence and potential. From the mischievous Fred and George Weasley to the brilliant Hermione Granger, the possibilities are endless.

Now, let's explore the columns, the magical spells that bring our characters to life. Here, we find incantations for names, ages, houses, and the wands that channel their magic. These spells are like the building blocks of our enchanting world, combining to create a tapestry of information that is as rich and complex as the Forbidden Forest itself. πŸŒ³πŸ”


3.1 Introduction to Hogwarts Students Dataset πŸͺ„βœ¨

Deep within the labyrinthine shelves of the Hogwarts Library, past the watchful gaze of Madam Pince, lies a section shrouded in mystery – the Restricted Section. Here, amongst dusty tomes whispering forgotten lore and grimoires bound in dragonhide, lies a treasure unlike any other: a data scroll brimming with the secrets of Hogwarts students! ✨

Unlike the ornately illustrated scrolls detailing the history of Quidditch or the intricacies of potion-making, this particular scroll is etched with a curious script of numbers and symbols. To the untrained eye, it might resemble a faded map or a cryptic incantation. But for a budding data sorcerer like yourself, it's a treasure trove waiting to be unlocked!

Imagine, if you will, this data scroll unfurling before you, its surface shimmering with an otherworldly glow. Each inscription whispers tales of past students – their bravery, their wit, their cunning, and their ambition. You'll find the fiery courage of a Gryffindor encoded in a sequence of numbers, the intellectual prowess of a Ravenclaw represented by a complex algorithm, the unwavering ambition of a Slytherin hidden within a data chart, and the unwavering loyalty of a Hufflepuff revealed in a hidden pattern.

Much like deciphering an ancient spell, we must delve into this data scroll, unravel its secrets, and uncover the underlying patterns that bind a student's traits to their rightful Hogwarts house. With a flick of your wand (or perhaps a tap on your enchanted tablet!), you'll be able to sort future students with an accuracy that would rival the Sorting Hat itself!

But before we embark on this magical data wrangling quest, a word of caution is necessary. Just like the Restricted Section holds forbidden knowledge, this data scroll too may contain its own set of challenges. Missing information (think of it as an erased passage in a spellbook!), inconsistencies (imagine a rogue pixie messing with your potions!), and outliers (students with unique personalities that defy categorization!) may lurk within. But fear not, for with a dash of perseverance and a sprinkle of data-driven ingenuity, we shall overcome these obstacles and unlock the true potential of this extraordinary scroll! πŸͺ„βœ¨


3.2 Into the Data Vault: Unlocking the Power of Python Libraries

Our quest to unveil the secrets of sorting at Hogwarts is upon us, but before we can utter a single incantation or brew a potent potion of data, we must first gather our essential tools. In the world of data science, these tools are not wands or cauldrons, but something far more powerful – Python libraries. ✨

Think of these libraries as our very own spellbooks, each containing unique collections of incantations (functions and code) that will empower us to manipulate and conjure order from the chaos of raw data. Just as a skilled witch or wizard wouldn't dream of facing a dragon without their wand, a data scientist wouldn't dare approach a mountain of information without these invaluable resources. πŸͺ„

The first library on our list is none other than NumPy, a powerful tome filled with spells for numerical computation. With a flick of our metaphorical wand (or rather, a line of Python code), NumPy allows us to summon forth multi-dimensional arrays, which act like magical containers that can hold mountains of data – be it student grades, wand core materials, or even the number of Chocolate Frogs consumed each week!

Next, we'll call upon the wisdom of the Pandas library. Imagine a dusty tome overflowing with enchanted spreadsheets, capable of wrangling and taming even the most unruly sets of data. Pandas grants us the power to sort, filter, and clean our information with the ease of a seasoned Herbology student weeding their Dragonhide gloves.

Finally, to illuminate the insights hidden within our data, we'll beseech the aid of Matplotlib and Seaborn. These libraries act as our personal portrait wizards, conjuring dazzling charts and graphs that transform numbers into breathtaking visuals. With Matplotlib, we can craft bar charts that soar like magical broomsticks, while Seaborn allows us to paint landscapes of information, each hue and line revealing a hidden truth.

With these potent Python libraries at our fingertips, we are well on our way to unlocking the secrets hidden within the Hogwarts data. So, grab your metaphorical quill (or keyboard) and prepare to be amazed, for our data-driven sorting ceremony is about to begin! ✨

# Importing the necessary libraries for our magical journey
import pandas as pd  # For data manipulation
import numpy as np  # For numerical operations
import matplotlib.pyplot as plt  # For data visualization
import seaborn as sns  # For advanced data visualization

# Ensuring our charts are in line with the Hogwarts aesthetic
sns.set(style="whitegrid")
Enter fullscreen mode Exit fullscreen mode

3.3 Reading the Dataset into a Pandas DataFrame πŸͺ„βœ¨

With our spellbooks of Python libraries open and our wands (keyboards) at the ready, it's time to embark on the next stage of our magical data adventure. Just as Professor McGonagall can transfigure a mundane object into something extraordinary, we shall use the enchanting powers of Pandas to transform our raw data into a magnificent DataFrame.

Imagine a sprawling parchment, divided into neat rows and columns, each cell filled with magical information about our Hogwarts students. This is our DataFrame, a powerful tool that will allow us to explore, analyze, and manipulate our data with the precision of a seasoned potioneer. And if you wish to follow along on this journey, you may download the scrolls (dataset) from this magical link.

As we cast the spell to create this DataFrame, we'll see the data come to life, transforming from a chaotic jumble of numbers and words into a structured and organized masterpiece. It's like watching a swarm of mischievous pixies magically align themselves into a beautiful formation. With our DataFrame in hand, we can now delve deeper into the secrets of Hogwarts, uncovering hidden patterns and revealing the true nature of each student.

# Reading the enchanted dataset into a Pandas DataFrame
dataset_path = 'data/hogwarts-students.csv'  # Path to our dataset
hogwarts_df = pd.read_csv(dataset_path)
Enter fullscreen mode Exit fullscreen mode

This is how they would look like in your Jupyter Lab, simply just copy and paste the code, from the above section to your Jupyter Notebook as instructed from the previous post, and don't forget to fire-up your Jupyter Lab environment by invoking the magic spell of jupyter notebook in your faithful terminal.

Displaying the first few rows of the dataset to get a glimpse of its contents in Jupyter Lab

Now that we've settled our magical requirements, let's have a peek over our enchanted dataset first few rows, so that we may have a short glimpse of what it's all about.

# Displaying the first few rows of the dataset to get a glimpse of its contents
print(hogwarts_df.head())
Enter fullscreen mode Exit fullscreen mode
                   name  gender  age   origin                      specialty  \
    0      Harry Potter    Male   11  England  Defense Against the Dark Arts   
    1  Hermione Granger  Female   11  England                Transfiguration   
    2       Ron Weasley    Male   11  England                          Chess   
    3      Draco Malfoy    Male   11  England                        Potions   
    4     Luna Lovegood  Female   11  Ireland                      Creatures   

            house blood_status  pet wand_type              patronus  \
    0  Gryffindor   Half-blood  Owl     Holly                  Stag   
    1  Gryffindor  Muggle-born  Cat      Vine                 Otter   
    2  Gryffindor   Pure-blood  Rat       Ash  Jack Russell Terrier   
    3   Slytherin   Pure-blood  Owl  Hawthorn                   NaN   
    4   Ravenclaw   Half-blood  NaN       Fir                  Hare   

      quidditch_position         boggart                 favorite_class  \
    0             Seeker        Dementor  Defense Against the Dark Arts   
    1                NaN         Failure                     Arithmancy   
    2             Keeper          Spider                         Charms   
    3             Seeker  Lord Voldemort                        Potions   
    4                NaN      Her mother                      Creatures   

       house_points  
    0         150.0  
    1         200.0  
    2          50.0  
    3         100.0  
    4         120.0  
Enter fullscreen mode Exit fullscreen mode

Ah, look at that! The first few rows of our DataFrame appear before us like the Marauder's Map, revealing the names, traits, and house placements of our fellow students. Each row tells a unique story, and together, they form the tapestry of Hogwarts.


3.4 Gemika's Pop-Up Quiz: Exploring the Enchanted Dataset πŸͺ„βœ¨

Gemika's Pop-Up Quiz: Exploring the Enchanted Dataset

And now, dear reader, my son Gemika Haziq Nugroho appears with a twinkle in his eye and a quiz in hand. He has prepared a series of questions to test your knowledge and ensure you are ready to proceed. Are you prepared to face the challenge?

  1. What Python library is used to read the dataset into a DataFrame?
  2. How do you display the first few rows of a DataFrame?
  3. What is the purpose of the sns.set(style="whitegrid") command?

Answer these questions correctly, and you will have proven your understanding of the enchanted dataset. Only then can we proceed to uncover the deeper mysteries that lie within. With our dataset unveiled and our understanding tested, we are now ready to embark on the next phase of our journey. The secrets of Hogwarts await, and with our wands and wisdom, we shall uncover them all. Onward, to adventure and discovery! πŸŒŸβœ¨πŸ§™β€β™‚οΈ


Top comments (0)