”工欲善其事,必先利其器。“—孔子《论语.录灵公》
首页 > 编程 > Gemika 使用决策树算法对霍格沃茨学生进行排序的神奇指南(第 7 部分)

Gemika 使用决策树算法对霍格沃茨学生进行排序的神奇指南(第 7 部分)

发布于2024-08-23
浏览:927

7. Casting One-Hot Encoding Spells ?✨

The air is thick with the scent of parchment and the crackle of magical energy, as we delve into the depths of data wizardry. Today, we shall explore a spellbinding technique known as One-Hot Encoding. Imagine transforming a mischievous Pixie, with its vibrant personality and unpredictable nature, into a series of precise coordinates on a magical map. That, dear reader, is the essence of One-Hot Encoding. ?‍♂️✨

Just as Professor McGonagall's wand can turn a teacup into a Dachshund, this spell can turn categorical data – those pesky labels that refuse to conform to numerical calculations – into a format our models can understand. Think of it as turning a chaotic swarm of Hufflepuffs, Ravenclaws, Gryffindors, and Slytherins into a neat grid of ones and zeros, each representing a specific house. ?✨

With One-Hot Encoding, we create new columns for each unique category, filling them with ones and zeros to indicate presence or absence. It’s like sorting mischievous house-elves into their designated rooms, ensuring each has its own space. By the end of this chapter, you'll be able to cast this spell with the confidence of a seasoned Charms master, transforming your data from a tangled knot into a beautifully organized tapestry. ?✨


7.1 Categorical Data: Sorting Hats and Magical Labels

In the enchanting realm of data science, where numbers dance and patterns reveal themselves, we encounter a curious breed of information known as categorical data. Unlike their numerical counterparts, these data points don't represent quantities but rather distinct categories or groups.

Imagine the Sorting Hat at Hogwarts, that wise old magical object that places students into their rightful houses. The houses – Gryffindor, Hufflepuff, Ravenclaw, and Slytherin – are examples of categorical data. They represent distinct groups with unique characteristics, just like the houses themselves. Similarly, the type of pet a student chooses – a loyal owl, a purring cat, or a grumpy toad – also falls into the category of categorical data.

Categorical data is like placing magical labels on objects, helping us differentiate and classify them. Just as a Herbology student would meticulously categorize different plants based on their properties, we use categorical data to sort and understand the diverse elements within our datasets. By understanding these magical labels, we can unlock hidden patterns and cast powerful spells (analyses) to uncover the secrets of our data. Let's seek some further knowledge, what values lie beneath the House column, cast your wand dear sorcerers. ?✨

# Displaying the unique categories in the 'House' column
unique_houses = hogwarts_df['house'].unique()
print(f"Unique Houses: {unique_houses}")
Unique Houses: ['Gryffindor' 'Slytherin' 'Ravenclaw' 'Hufflepuff' 'Durmstrang' 'Beauxbatons']

Understanding these categories is crucial because our magical algorithms (or models) need to know how to interpret this data. However, these algorithms often struggle with non-numerical data, as they are more comfortable with numbers. This is where the magic of One-Hot Encoding comes into play.


7.2 Transforming Categorical Data using One-Hot Encoding

Imagine our Hogwarts student records, filled with enchanting details like house, wand type, and favorite subject. These qualities are like magical sigils, carrying unique energies. However, our brilliant data models, while capable of wondrous feats, cannot decipher these sigils directly. We must transform them into a language they understand – numbers.

Enter the spell of One-Hot Encoding, a powerful incantation that reveals the hidden essence of each categorical variable. It's like casting a Lumos spell on a hidden chamber, illuminating every nook and cranny. With a flick of our coding wand, we transform each category into its own standalone column. If a student belongs to Gryffindor, for instance, a 1 will magically appear in the Gryffindor column, while the other house columns remain dark.

This transformation is akin to creating a magical tapestry, where each thread represents a category. By weaving these threads together, we create a rich and detailed portrait of our students, ready to be analyzed by our data models. It's as if we're granting our models the ability to see the world through the eyes of a Polyjuice Potion drinker, experiencing each student's unique perspective. Let's go ahead and try to One-Hot Encoding our first column or feature, will try the gender column first.

# Importing necessary libraries
import pandas as pd
from sklearn.preprocessing import OneHotEncoder
from IPython.display import display, HTML

# Assuming hogwarts_df is already defined and contains the 'gender' column

# Applying One-Hot Encoding to the 'gender' column
encoder = OneHotEncoder(sparse_output=False)  # Updated parameter name
encoded_data = encoder.fit_transform(hogwarts_df[['gender']])

# Converting the encoded data into a DataFrame and attaching it to the original dataset
encoded_df = pd.DataFrame(encoded_data, columns=encoder.get_feature_names_out(['gender']))
hogwarts_df = pd.concat([hogwarts_df, encoded_df], axis=1)

# Dropping the original 'gender' column as it's now encoded
hogwarts_df.drop('gender', axis=1, inplace=True)

# Displaying the transformed DataFrame in a scrollable pane
html = hogwarts_df.head(5).to_html() # Convert DataFrame to HTML
scrollable_html = f"""
{html}
""" display(HTML(scrollable_html))
name    age origin  specialty   house   blood_status    pet wand_type   patronus    quidditch_position  boggart favorite_class  house_points    gender_Female   gender_Male
0   Harry Potter    11  England Defense Against the Dark Arts   Gryffindor  Half-blood  Owl Holly   Stag    Seeker  Dementor    Defense Against the Dark Arts   150.0   0.0 1.0
1   Hermione Granger    11  England Transfiguration Gryffindor  Muggle-born Cat Vine    Otter   Seeker  Failure Arithmancy  200.0   1.0 0.0
2   Ron Weasley 11  England Chess   Gryffindor  Pure-blood  Rat Ash Jack Russell Terrier    Keeper  Spider  Charms  50.0    0.0 1.0
3   Draco Malfoy    11  England Potions Slytherin   Pure-blood  Owl Hawthorn    Non-corporeal   Seeker  Lord Voldemort  Potions 100.0   0.0 1.0
4   Luna Lovegood   11  Ireland Creatures   Ravenclaw   Half-blood  Owl Fir Hare    Seeker  Her mother  Creatures   120.0   1.0 0.0

And if you scroll to the right, you might notice that the dataset now has additional two columns, the gender_Female and the gender_Male on top of the existing one, while dropping the original gender column that was there previously.


7.3 The Two Great Treasures of the Data Realm ?✨

In the grand tapestry of the wizarding world of data, there exist two primary categories of magical artifacts: Structured Data and Unstructured Data. These are the building blocks of our enchanting spells and powerful potions.

Categorical Data is akin to a well-organized Herbology garden, where every plant (data point) has its rightful place. It's like a neatly filled Hogwarts student record, with columns for names, houses, and wand types, all aligned in perfect order. Structured data is a wizard's delight, easily understood and manipulated with a flick of the wand (or a few lines of code). ?✨

On the other hand, Numerical Data is a sprawling Forbidden Forest, filled with magical creatures (data points) roaming freely. It's like a collection of owls' letters, each with its own unique style and format. This data can be as diverse as the stars in the night sky, ranging from social media posts to news articles, images, and even spoken words. While it holds immense potential, taming this wild magic requires special spells and a keen eye for patterns. ??

  1. Categorical Data (Qualitative Data):

    • Definition: Categorical data refers to information that can be sorted into distinct groups or categories based on qualitative characteristics, rather than numerical values.
    • Types:
      • Nominal Data: This type includes categories without any specific order (e.g., gender, hair color). It is often used for labeling variables without providing a numerical value.
      • Ordinal Data: This type has a defined order or ranking (e.g., customer satisfaction ratings, economic class ratings, movie ratings). The differences between the ranks may not be equal.
    • Examples: Gender, race, color, and types of products.
    • Analysis: Categorical data is typically analyzed using frequency counts, bar graphs, and pie charts. It does not support arithmetic operations like addition or averaging.
  2. Numerical Data (Quantitative Data):

    • Definition: Numerical data consists of values that can be measured and expressed numerically, allowing for mathematical operations.
    • Types:
      • Discrete Data: Countable values (e.g., number of students in a class).
      • Continuous Data: Measurable quantities that can take any value within a range (e.g., height, weight).
    • Analysis: Numerical data can be analyzed using various statistical methods, including mean, median, mode, and standard deviation.

7.4 Transforming Text into Numbers: The Magic of One-Hot Encoding

Imagine a world where numbers and words could converse, where the language of magic flowed seamlessly from one to the other. This is the realm of one-hot encoding, a powerful spell that transforms the mysterious world of text into the concrete world of numbers.

Just as a skilled Herbologist categorizes plants by their properties, one-hot encoding sorts textual data into distinct categories. Consider the Sorting Hat, which assigns students to houses based on their unique qualities. Similarly, one-hot encoding creates separate columns for each category, with values of 0 or 1 indicating whether a data point belongs to that category or not.

For instance, if you have a column representing the houses of Hogwarts students (Gryffindor, Slytherin, Ravenclaw, and Hufflepuff), one-hot encoding would conjure four new columns, one for each house. A value of 1 in the Gryffindor column would signify a student belonging to that house, while other columns would be filled with 0s. This numerical representation allows our magical models to understand and process textual information with ease. With that being said, let's try to transform the remaining of the columns in our dataset, and see what we have to work on. ?

# Importing necessary libraries
import pandas as pd
from sklearn.preprocessing import OneHotEncoder
from IPython.display import display, HTML

# Assuming hogwarts_df is already defined and contains the necessary columns
columns_to_encode = [
    'origin', 'specialty', 'blood_status', 'pet', 'wand_type', 'patronus', 'quidditch_position', 'boggart', 'favorite_class'
]

# Creating an instance of OneHotEncoder
encoder = OneHotEncoder(sparse_output=False)

# List to hold encoded DataFrames
encoded_dfs = []

# Applying One-Hot Encoding to each column and storing the result
for column in columns_to_encode:
    encoded_data = encoder.fit_transform(hogwarts_df[[column]])
    encoded_df = pd.DataFrame(encoded_data, columns=encoder.get_feature_names_out([column]))
    encoded_dfs.append(encoded_df)

# Concatenating all encoded DataFrames into one
encoded_df_combined = pd.concat(encoded_dfs, axis=1)

# Concatenating the encoded DataFrame with the original DataFrame
hogwarts_df = pd.concat([hogwarts_df, encoded_df_combined], axis=1)

# Dropping the original columns that were encoded
hogwarts_df.drop(columns=columns_to_encode, inplace=True)

# Displaying the transformed DataFrame in a scrollable pane
html = hogwarts_df.head(5).to_html()  # Convert DataFrame to HTML
scrollable_html = f"""
{html}
""" display(HTML(scrollable_html))
name    age house   house_points    gender_Female   gender_Male origin_Bulgaria origin_England  origin_Europe   origin_France   origin_Indonesia    origin_Ireland  origin_Scotland origin_USA  origin_Wales    specialty_Auror specialty_Baking    specialty_Charms    specialty_Chess specialty_Creatures specialty_Dark Arts specialty_Defense Against the Dark Arts specialty_Dueling   specialty_Goat Charming specialty_Gossip    specialty_Herbology specialty_History of Magic  specialty_Household Charms  specialty_Legilimency   specialty_Magical Creatures specialty_Memory Charms specialty_Metamorphmagus    specialty_Muggle Artifacts  specialty_Obscurus  specialty_Potions   specialty_Quidditch specialty_Strength  specialty_Transfiguration   specialty_Transformation    blood_status_Half-blood blood_status_Muggle-born    blood_status_No-mag blood_status_Pure-blood pet_Cat pet_Demiguise   pet_Dog pet_Goat    pet_Owl pet_Phoenix pet_Rat pet_Snake   pet_Toad    wand_type_Alder wand_type_Ash   wand_type_Birch wand_type_Blackthorn    wand_type_Cedar wand_type_Cherry    wand_type_Chestnut  wand_type_Cypress   wand_type_Ebony wand_type_Elder wand_type_Elm   wand_type_Fir   wand_type_Hawthorn  wand_type_Hazel wand_type_Hemlock   wand_type_Holly wand_type_Hornbeam  wand_type_Maple wand_type_Oak   wand_type_Pine  wand_type_Rosewood  wand_type_Rowan wand_type_Sword wand_type_Teak  wand_type_Vine  wand_type_Walnut    wand_type_Willow    wand_type_Yew   patronus_Cat    patronus_Doe    patronus_Dog    patronus_Eagle  patronus_Hare   patronus_Horse  patronus_Jack Russell Terrier   patronus_Lion   patronus_Non-corporeal  patronus_Otter  patronus_Phoenix    patronus_Serpent    patronus_Stag   patronus_Swan   patronus_Wolf   quidditch_position_Azkaban  quidditch_position_Beater   quidditch_position_Chaser   quidditch_position_Keeper   quidditch_position_Seeker   boggart_Ariana's death  boggart_Dementor    boggart_Dueling boggart_Failure boggart_Full Moon   boggart_Her mother  boggart_Lily Potter boggart_Lord Voldemort  boggart_Severus Snape   boggart_Spider  boggart_Tom Riddle  favorite_class_Arithmancy   favorite_class_Baking   favorite_class_Charms   favorite_class_Creatures    favorite_class_Dark Arts    favorite_class_Defense Against the Dark Arts    favorite_class_Dueling  favorite_class_Goat Charming    favorite_class_Gossip   favorite_class_Herbology    favorite_class_Household Charms favorite_class_Legilimency  favorite_class_Memory Charms    favorite_class_Muggle Studies   favorite_class_Obscurus favorite_class_Potions  favorite_class_Quidditch    favorite_class_Strength favorite_class_Transfiguration  favorite_class_Transformation
0   Harry Potter    11  Gryffindor  150.0   0.0 1.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
1   Hermione Granger    11  Gryffindor  200.0   1.0 0.0 0.0 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.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

7.5 Discretizing the Numerical Values: Uncovering the Numerical Data) ✨

In the magical realm of data science, where numbers hold secrets and patterns dance in the shadows, there exists a particularly enchanting spell: one-hot encoding. This spell is a transfiguration charm, capable of transforming seemingly ordinary text into a numerical language that our magical computers can understand. Imagine a bustling Diagon Alley, filled with shops selling wands, cauldrons, and robes of every color. Each shop has a unique name, a string of letters that defines its identity. Now, picture these shop names as magical creatures, wild and untamed. To harness their power for our spells, we must transform them into something more manageable – numbers.

One-hot encoding is the spell that accomplishes this feat. It takes each unique shop name and creates a separate magical dimension (column) for it. Within these dimensions, we cast a binary spell, assigning a value of 1 to the shop that exists in that dimension and 0 to all others. It's like creating a magical grid, where each shop has its own spotlight moment. With this transformation, our once chaotic collection of shop names becomes an orderly array of numbers, ready to be analyzed and explored.?✨

7.5.1 Converting Columns with Numerical Values

To convert a numerical column with values ranging from 100 to 200 into a more machine learning-friendly format using one-hot encoding, you typically need to discretize the numerical values into categorical bins first. Here’s how to do it step by step:

You can create bins (categories) for the numerical values. For example, you might define bins like this:

  • 100-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200

7.5.2 Assign Categories

Next, assign each numerical value to its corresponding bin. For example:

Original Value Category
100 100-120
110 100-120
125 121-140
145 141-160
165 161-180
180 161-180
200 181-200

7.5.3 One-Hot Encode the Categories

Now, you can apply one-hot encoding to the categorical column. Each category will be represented as a binary vector:

Original Value 100-120 121-140 141-160 161-180 181-200
100 1 0 0 0 0
110 1 0 0 0 0
125 0 1 0 0 0
145 0 0 1 0 0
165 0 0 0 1 0
180 0 0 0 1 0
200 0 0 0 0 1

By discretizing the numerical values into categories and applying one-hot encoding, you transform the original numerical data into a format that machine learning algorithms can process effectively. This method captures the categorical nature of the data while preserving the information contained in the original numerical values.

import pandas as pd
from sklearn.preprocessing import OneHotEncoder
from IPython.display import display, HTML

# Step 1: Define bins and labels
bins = [100, 120, 140, 160, 180, 200]  # Define the bin edges
labels = ['hp_100_120', 'hp_121_140', 'hp_141_160', 'hp_161_180', 'hp_181_200']  # Define the bin labels

# Step 2: Create a new categorical column based on the bins
hogwarts_df['house_category'] = pd.cut(hogwarts_df['house_points'], bins=bins, labels=labels, right=True)

# Step 3: One-hot encode the categorical column
hogwarts_df_encoded = pd.get_dummies(hogwarts_df, columns=['house_category'], prefix='', prefix_sep='')

# Replace True with 1 and False with 0
hogwarts_df_encoded = hogwarts_df_encoded.replace({True: 1, False: 0})

# Drop the house_points column
hogwarts_df_encoded.drop('house_points', axis=1, inplace=True)

# Displaying the transformed DataFrame in a scrollable pane
html = hogwarts_df_encoded.head(5).to_html()  # Convert DataFrame to HTML & # Display first 5 rows in a scrollable pane
scrollable_html = f"""
{html}
""" display(HTML(scrollable_html))
name    age house   gender_Female   gender_Male origin_Bulgaria origin_England  origin_Europe   origin_France   origin_Indonesia    origin_Ireland  origin_Scotland origin_USA  origin_Wales    specialty_Auror specialty_Baking    specialty_Charms    specialty_Chess specialty_Creatures specialty_Dark Arts specialty_Defense Against the Dark Arts specialty_Dueling   specialty_Goat Charming specialty_Gossip    specialty_Herbology specialty_History of Magic  specialty_Household Charms  specialty_Legilimency   specialty_Magical Creatures specialty_Memory Charms specialty_Metamorphmagus    specialty_Muggle Artifacts  specialty_Obscurus  specialty_Potions   specialty_Quidditch specialty_Strength  specialty_Transfiguration   specialty_Transformation    blood_status_Half-blood blood_status_Muggle-born    blood_status_No-mag blood_status_Pure-blood pet_Cat pet_Demiguise   pet_Dog pet_Goat    pet_Owl pet_Phoenix pet_Rat pet_Snake   pet_Toad    wand_type_Alder wand_type_Ash   wand_type_Birch wand_type_Blackthorn    wand_type_Cedar wand_type_Cherry    wand_type_Chestnut  wand_type_Cypress   wand_type_Ebony wand_type_Elder wand_type_Elm   wand_type_Fir   wand_type_Hawthorn  wand_type_Hazel wand_type_Hemlock   wand_type_Holly wand_type_Hornbeam  wand_type_Maple wand_type_Oak   wand_type_Pine  wand_type_Rosewood  wand_type_Rowan wand_type_Sword wand_type_Teak  wand_type_Vine  wand_type_Walnut    wand_type_Willow    wand_type_Yew   patronus_Cat    patronus_Doe    patronus_Dog    patronus_Eagle  patronus_Hare   patronus_Horse  patronus_Jack Russell Terrier   patronus_Lion   patronus_Non-corporeal  patronus_Otter  patronus_Phoenix    patronus_Serpent    patronus_Stag   patronus_Swan   patronus_Wolf   quidditch_position_Azkaban  quidditch_position_Beater   quidditch_position_Chaser   quidditch_position_Keeper   quidditch_position_Seeker   boggart_Ariana's death  boggart_Dementor    boggart_Dueling boggart_Failure boggart_Full Moon   boggart_Her mother  boggart_Lily Potter boggart_Lord Voldemort  boggart_Severus Snape   boggart_Spider  boggart_Tom Riddle  favorite_class_Arithmancy   favorite_class_Baking   favorite_class_Charms   favorite_class_Creatures    favorite_class_Dark Arts    favorite_class_Defense Against the Dark Arts    favorite_class_Dueling  favorite_class_Goat Charming    favorite_class_Gossip   favorite_class_Herbology    favorite_class_Household Charms favorite_class_Legilimency  favorite_class_Memory Charms    favorite_class_Muggle Studies   favorite_class_Obscurus favorite_class_Potions  favorite_class_Quidditch    favorite_class_Strength favorite_class_Transfiguration  favorite_class_Transformation   hp_100_120  hp_121_140  hp_141_160  hp_161_180  hp_181_200
0   Harry Potter    11  Gryffindor  0.0 1.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.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0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1   0   0   0   0

7.6 Transforming Numbers into Magical Categories: Discretizing Age ?‍♀️✨

Imagine a bustling Hogwarts Express, filled with students of all ages. From wide-eyed first-years to wise-cracking seventh-years, each student possesses a unique blend of magic and mischief. To better understand these young wizards and witches, we can transform their ages from precise numbers into magical categories.

This process, known as discretization, is like sorting mischievous house-elves into their designated chores. Instead of treating age as a continuous spectrum, we group students into specific age ranges or bins. Picture these bins as enchanted compartments on the Hogwarts Express, each holding students of similar age.

By discretizing age, we unlock a new dimension of magical insight. We can explore how different age groups perform in classes, participate in Quidditch, or even their preferred wand type. It's like casting a revealing charm on our data, uncovering hidden patterns and trends that would otherwise remain obscured. So, let's transform those numbers into magical categories and discover the enchanting secrets hidden within the age of our Hogwarts students! ?✨

import pandas as pd
from IPython.display import display, HTML

# Step 1: Define bins and labels for the age column
bins = [10, 12, 14, 16, 18]  # Define the bin edges
labels = ['age_11', 'age_12', 'age_13', 'age_14']  # Define the bin labels

# Step 2: Create a new categorical column based on the bins
hogwarts_df_encoded['age_category'] = pd.cut(hogwarts_df_encoded['age'], bins=bins, labels=labels, right=True)

# Step 3: One-hot encode the categorical column
hogwarts_df_encoded_age = pd.get_dummies(hogwarts_df_encoded, columns=['age_category'], prefix='', prefix_sep='')

# Replace True with 1 and False with 0 (not necessary here since get_dummies already returns integers)
hogwarts_df_encoded_age = hogwarts_df_encoded_age.replace({True: 1, False: 0})

# Drop the age column
hogwarts_df_encoded_age.drop('age', axis=1, inplace=True)

# Displaying the transformed DataFrame in a scrollable pane
html = hogwarts_df_encoded_age.head(5).to_html()  # Convert DataFrame to HTML & Display first 5 rows in a scrollable pane
scrollable_html = f"""
{html}
""" display(HTML(scrollable_html))

7.7 Order in the Chaos: The Magic of Naming Conventions ? ✨

Imagine a bustling Hogwarts library, books overflowing from shelves, each with its own peculiar title. Without a proper cataloguing system, finding a specific spellbook would be like searching for a needle in a haystack, wouldn't it? In the realm of data, our columns are like these books. They hold valuable information, but without a clear and consistent naming system, they can quickly become a chaotic mess. This is where the magic of naming conventions comes into play.

Just as a librarian organizes books by subject, author, and title, we must impose order upon our columns. A well-crafted naming convention is like a powerful sorting spell, grouping similar columns together and making them easily identifiable. For example, using prefixes like 'student_', 'subject_', or 'score_' can instantly clarify the column's purpose.

By adopting a clear and consistent naming convention, you'll transform your data from a chaotic jumble into a well-organized magical library. This not only improves readability but also streamlines data manipulation and analysis. So, next time you're wrangling with data, remember the importance of a strong naming convention. It's the first step towards unlocking the hidden treasures within your dataset! ✨

import pandas as pd
from IPython.display import display, HTML

# Assuming hogwarts_df_encoded is already defined and contains the necessary columns

# Manipulate column names
hogwarts_df_encoded_age.columns = hogwarts_df_encoded_age.columns.str.lower().str.replace(' ', '_').str.replace('-', '-').str.replace("'", "")

# Display the transformed DataFrame in a scrollable pane
html = hogwarts_df_encoded_age.head(5).to_html()  # Convert first 5 rows to HTML
scrollable_html = f"""
{html}
""" display(HTML(scrollable_html)) # Display first 5 rows in a scrollable pane
name    house   gender_female   gender_male origin_bulgaria origin_england  origin_europe   origin_france   origin_indonesia    origin_ireland  origin_scotland origin_usa  origin_wales    specialty_auror specialty_baking    specialty_charms    specialty_chess specialty_creatures specialty_dark_arts specialty_defense_against_the_dark_arts specialty_dueling   specialty_goat_charming specialty_gossip    specialty_herbology specialty_history_of_magic  specialty_household_charms  specialty_legilimency   specialty_magical_creatures specialty_memory_charms specialty_metamorphmagus    specialty_muggle_artifacts  specialty_obscurus  specialty_potions   specialty_quidditch specialty_strength  specialty_transfiguration   specialty_transformation    blood_status_half-blood blood_status_muggle-born    blood_status_no-mag blood_status_pure-blood pet_cat pet_demiguise   pet_dog pet_goat    pet_owl pet_phoenix pet_rat pet_snake   pet_toad    wand_type_alder wand_type_ash   wand_type_birch wand_type_blackthorn    wand_type_cedar wand_type_cherry    wand_type_chestnut  wand_type_cypress   wand_type_ebony wand_type_elder wand_type_elm   wand_type_fir   wand_type_hawthorn  wand_type_hazel wand_type_hemlock   wand_type_holly wand_type_hornbeam  wand_type_maple wand_type_oak   wand_type_pine  wand_type_rosewood  wand_type_rowan wand_type_sword wand_type_teak  wand_type_vine  wand_type_walnut    wand_type_willow    wand_type_yew   patronus_cat    patronus_doe    patronus_dog    patronus_eagle  patronus_hare   patronus_horse  patronus_jack_russell_terrier   patronus_lion   patronus_non-corporeal  patronus_otter  patronus_phoenix    patronus_serpent    patronus_stag   patronus_swan   patronus_wolf   quidditch_position_azkaban  quidditch_position_beater   quidditch_position_chaser   quidditch_position_keeper   quidditch_position_seeker   boggart_arianas_death   boggart_dementor    boggart_dueling boggart_failure boggart_full_moon   boggart_her_mother  boggart_lily_potter boggart_lord_voldemort  boggart_severus_snape   boggart_spider  boggart_tom_riddle  favorite_class_arithmancy   favorite_class_baking   favorite_class_charms   favorite_class_creatures    favorite_class_dark_arts    favorite_class_defense_against_the_dark_arts    favorite_class_dueling  favorite_class_goat_charming    favorite_class_gossip   favorite_class_herbology    favorite_class_household_charms favorite_class_legilimency  favorite_class_memory_charms    favorite_class_muggle_studies   favorite_class_obscurus favorite_class_potions  favorite_class_quidditch    favorite_class_strength favorite_class_transfiguration  favorite_class_transformation   hp_100_120  hp_121_140  hp_141_160  hp_161_180  hp_181_200  age_11  age_12  age_13  age_14
0   Harry Potter    Gryffindor  0.0 1.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.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.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1   0   0   0   0   1   0   0   0

And once we've satisfied with the results, let's go ahead and save the current dataset to make the next enchanting journey more easier to navigate.

hogwarts_df_encoded_age.to_csv('data/hogwarts-students-03.csv', index=False)

7.8 Mapping the Magical Connections: Uncovering Hidden Relationships

Imagine a vast enchanted forest, teeming with magical creatures and extraordinary plants. Each creature and plant possesses unique qualities, and some may share hidden connections. To unveil these mysterious bonds, we must cast a spell of correlation. A correlation matrix is like a magical map, guiding us through this enchanted forest. Each point on the map represents a different creature or plant, and the lines connecting them reveal the strength of their relationship. A thick, vibrant line signifies a strong connection, while a thin, faint line indicates a weaker bond.

As we explore this magical map, we search for patterns and trends. Are certain creatures often found near specific plants? Do particular plants thrive in the company of others? By deciphering these connections, we can uncover hidden knowledge about the forest and its inhabitants. Just as a skilled Herbologist studies the interactions between plants, we, as data wizards, uncover the hidden relationships between variables. With the correlation matrix as our guide, we embark on a thrilling adventure to explore the magical tapestry of our data! ?️✨

# Importing necessary libraries
import pandas as pd
from IPython.display import display, HTML

# Assuming hogwarts_df is already defined and contains the necessary columns

# Selecting only numerical columns
numerical_df = hogwarts_df_encoded_age.select_dtypes(include=['number'])

# Calculating the correlation matrix
correlation_matrix = numerical_df.corr()

# Displaying the correlation matrix in a scrollable pane
correlation_html = correlation_matrix.to_html()  # Convert correlation matrix to HTML
scrollable_correlation_html = f"""
{correlation_html}
""" display(HTML(scrollable_correlation_html)) # Display correlation matrix in a scrollable pane

The Gemika

And to make our analysis easier, let's just go ahead and save the correlation matrix into a tabular format.

correlation_matrix.to_csv('data/correlation-matrix.csv')

7.9 Potion Ingredients: Verifying Our Data Types ?✨

Just as a skilled potioneer carefully examines their ingredients before brewing a powerful concoction, we data scientists must meticulously inspect our data types. These data types are like the magical properties of our ingredients, determining how they will react in our spells (algorithms).

Imagine our dataset as a cauldron brimming with magical elements. Each element, be it a student's age, house, or wand type, has a specific form or essence - its data type. These types can be as diverse as the magical creatures of the Forbidden Forest: numbers (like the count of house points), text (like a student's name), dates (like the founding year of Hogwarts), and more.

Mismatched data types can lead to disastrous results, like a potion exploding or a spell backfiring. That's why we must cast a discerning eye over our data, ensuring each element is of the correct type. It's like checking if a Mandrake root is truly a root and not a disguised Goblin! Only then can we confidently proceed with our magical data transformations. ?‍♀️✨

# Importing necessary libraries
import pandas as pd
from IPython.display import display, HTML

# Assuming hogwarts_df is already defined and contains the necessary columns

# Setting display options to show all columns and prevent truncation
pd.set_option('display.max_columns', None)  # Show all columns
pd.set_option('display.expand_frame_repr', False)  # Prevent truncation in output

# Checking the data types of each column
data_types_df = hogwarts_df_encoded_age.dtypes.to_frame(name='Data Type')  # Convert data types to a DataFrame

# Displaying the data types in a scrollable pane
data_types_html = data_types_df.to_html()  # Convert DataFrame to HTML
scrollable_data_types_html = f"""
{data_types_html}
""" display(HTML(scrollable_data_types_html)) # Display data types in a scrollable pane
    Data Type
name    object
house   object
gender_female   float64
gender_male float64
origin_bulgaria float64
origin_england  float64
origin_europe   float64
origin_france   float64
origin_indonesia    float64
origin_ireland  float64
origin_scotland float64
origin_usa  float64
origin_wales    float64
specialty_auror float64
specialty_baking    float64
specialty_charms    float64
specialty_chess float64
specialty_creatures float64
specialty_dark_arts float64
specialty_defense_against_the_dark_arts float64
specialty_dueling   float64
specialty_goat_charming float64
specialty_gossip    float64
specialty_herbology float64
specialty_history_of_magic  float64
specialty_household_charms  float64
specialty_legilimency   float64
specialty_magical_creatures float64

7.10 Gemika's Pop-Up Quiz: Spotting the Trends

And now, dear apprentices, Gemika Haziq Nugroho has prepared a quiz to test your understanding of this powerful encoding spell. Are you ready to decode the mysteries of One-Hot Encoding?

  • What is categorical data, and why is it important in data analysis?
  • How does One-Hot Encoding transform categorical data for machine learning models?
  • Why do we drop the original categorical column after applying One-Hot Encoding?

Answer these questions to prove your mastery over the art of data transformation. As we continue our journey through the magical world of data science, remember that each spell we learn brings us closer to unveiling the secrets of our enchanted dataset. ??‍♂️✨

With these newfound skills, you're well-equipped to handle categorical data in any dataset. The path to becoming a master data wizard is full of wonder and discovery, and with each step, we draw closer to the heart of the magical data that surrounds us. Let us press on, eager to learn and ready to explore! ??


版本声明 本文转载于:https://dev.to/gerryleonugroho/the-gemikas-magical-guide-to-sorting-hogwarts-students-using-the-decision-tree-algorithm-part-7-1g5l?1如有侵犯,请联系[email protected]删除
最新教程 更多>
  • 掌握 TypeScript:了解扩展的力量
    掌握 TypeScript:了解扩展的力量
    TypeScript 中的 extends 关键字就像一把瑞士军刀。它用于多种上下文,包括继承、泛型和条件类型。了解如何有效地使用扩展可以生成更健壮、可重用和类型安全的代码。 使用扩展进行继承 extends 的主要用途之一是继承,允许您创建基于现有接口或类的新接口或类。 inter...
    编程 发布于2024-11-06
  • 如何将具有组计数的列添加到 Pandas 中的分组数据框?
    如何将具有组计数的列添加到 Pandas 中的分组数据框?
    如何在Pandas中向分组数据框中添加列在数据分析中,经常需要对数据进行分组并进行计算每组。 Pandas 通过其 groupby 函数提供了一种便捷的方法来做到这一点。一个常见的任务是计算每个组中某一列的值,并将包含这些计数的列添加到数据帧中。考虑数据帧 df:df = pd.DataFrame(...
    编程 发布于2024-11-06
  • 破解编码面试的热门必备书籍(从初级到高级排名)
    破解编码面试的热门必备书籍(从初级到高级排名)
    准备编码面试可能是一个充满挑战的旅程,但拥有正确的资源可以让一切变得不同。无论您是从算法开始的初学者、专注于系统设计的中级开发人员,还是完善编码实践的高级工程师,这份按难度排名的前 10 本书列表都将为您提供成功所需的知识和技能。你的软件工程面试。这些书籍涵盖了从基本算法到系统设计和简洁编码原则的所...
    编程 发布于2024-11-06
  • Java 字符串实习初学者指南
    Java 字符串实习初学者指南
    Java String Interning 引入了通过在共享池中存储唯一字符串来优化内存的概念,减少重复对象。它解释了 Java 如何自动实习字符串文字以及开发人员如何使用 intern() 方法手动将字符串添加到池中。 通过掌握字符串驻留,您可以提高 Java 应用程序的性能和内存效率。要深入...
    编程 发布于2024-11-06
  • 如何在 GUI 应用程序中的不同页面之间共享变量数据?
    如何在 GUI 应用程序中的不同页面之间共享变量数据?
    如何从类中获取变量数据在 GUI 编程环境中,单个应用程序窗口中包含多个页面是很常见的。每个页面可能包含各种小部件,例如输入字段、按钮或标签。当与这些小部件交互时,用户提供输入或做出需要在不同页面之间共享的选择。这就提出了如何从一个类访问另一个类的变量数据的问题,特别是当这些类代表不同的页面时。利用...
    编程 发布于2024-11-06
  • React 中的动态路由
    React 中的动态路由
    React 中的动态路由允许您基于动态数据或参数创建路由,从而在应用程序中实现更灵活、更强大的导航。这对于需要根据用户输入或其他动态因素呈现不同组件的应用程序特别有用。 使用 React Router 设置动态路由 您通常会使用react-router-dom库在React中实现动态路由。这是分步指...
    编程 发布于2024-11-06
  • 大批
    大批
    方法是可以在对象上调用的 fns 数组是对象,因此它们在 JS 中也有方法。 slice(begin):将数组的一部分提取到新数组中,而不改变原始数组。 let arr = ['a','b','c','d','e']; // Usecase: Extract till index p...
    编程 发布于2024-11-06
  • WPF中延迟操作时如何避免UI冻结?
    WPF中延迟操作时如何避免UI冻结?
    WPF 中的延迟操作WPF 中的延迟操作对于增强用户体验和确保平滑过渡至关重要。一种常见的情况是在导航到新窗口之前添加延迟。为了实现此目的,经常使用 Thread.Sleep,如提供的代码片段中所示。但是,在延迟过程中,使用 Thread.Sleep 阻塞 UI 线程会导致 UI 无响应。这表现为在...
    编程 发布于2024-11-06
  • 利用 Java 进行实时数据流和处理
    利用 Java 进行实时数据流和处理
    In today's data-driven world, the ability to process and analyze data in real-time is crucial for businesses to make informed decisions swiftly. Java...
    编程 发布于2024-11-06
  • 如何修复损坏的 InnoDB 表?
    如何修复损坏的 InnoDB 表?
    从 InnoDB 表损坏中恢复灾难性事件可能会导致数据库表严重损坏,特别是 InnoDB 表。遇到这种情况时,了解可用的修复选项就变得至关重要。InnoDB Table Corruption Symptoms查询中描述的症状,包括事务日志中的时间戳错误InnoDB 表的修复策略虽然已经有修复 MyI...
    编程 发布于2024-11-06
  • JavaScript 数组和对象中是否正式允许使用尾随逗号?
    JavaScript 数组和对象中是否正式允许使用尾随逗号?
    数组和对象中的尾随逗号:标准还是容忍?数组和对象中尾随逗号的存在引发了一些关于它们的争论JavaScript 的标准化。这个问题源于在不同浏览器中观察到的不一致行为,特别是旧版本的 Internet Explorer。规范状态根据 ECMAScript 5 规范(第 11.1.5 节) ),对象字面...
    编程 发布于2024-11-06
  • 最佳引导模板生成器
    最佳引导模板生成器
    在当今快速发展的数字环境中,速度和效率是关键,网页设计师和开发人员越来越依赖 Bootstrap 构建器来简化他们的工作流程。这些工具可以快速创建响应灵敏、具有视觉吸引力的网站,使团队能够比以往更快地将他们的想法变为现实。 Bootstrap 构建器真正改变了网站的构建方式,使该过程更加易于访问和高...
    编程 发布于2024-11-06
  • 简化 NestJS 中的文件上传:无需磁盘存储即可高效内存中解析 CSV 和 XLSX
    简化 NestJS 中的文件上传:无需磁盘存储即可高效内存中解析 CSV 和 XLSX
    Effortless File Parsing in NestJS: Manage CSV and XLSX Uploads in Memory for Speed, Security, and Scalability Introduction Handling file uploa...
    编程 发布于2024-11-06
  • 使用 SubDomainRadar.io 和 Python 轻松发现隐藏子域
    使用 SubDomainRadar.io 和 Python 轻松发现隐藏子域
    作为网络安全专业人员、漏洞赏金猎人或渗透测试人员,发现隐藏的子域对于识别至关重要域中的潜在漏洞。子域通常托管可能容易受到攻击的被遗忘的服务或测试环境。 在这篇文章中,我将向您介绍 SubDomainRadar.io 及其 Python API 包装器 — 自动化子域枚举的终极工具 和 安全工作流程...
    编程 发布于2024-11-06
  • Python 中的 HackerRank 问题 - 基本数据类型列表
    Python 中的 HackerRank 问题 - 基本数据类型列表
    此 Python 代码旨在根据用户提供的命令对列表执行一系列操作。让我们一步步分析代码,了解其工作原理: if __name__ == '__main__': N = int(input()) l = [] while(N>0): cmd_l = inp...
    编程 发布于2024-11-06

免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。

Copyright© 2022 湘ICP备2022001581号-3