One-hot Encoding with Python
📖 Scenario: Imagine you work in a company that collects customer data. One of the columns in your data is favorite_fruit, which contains names of fruits customers like. You want to prepare this data for a machine learning model, which needs numbers instead of words.
🎯 Goal: You will create a simple one-hot encoding for the favorite_fruit column. This means you will turn each fruit name into a new column with 1 or 0, showing if the customer likes that fruit or not.
📋 What You'll Learn
Create a list called
favorite_fruits with exact values: 'apple', 'banana', 'apple', 'orange', 'banana'Create a list called
unique_fruits that contains the unique fruits from favorite_fruitsCreate a list of dictionaries called
one_hot_encoded where each dictionary represents one customer's one-hot encodingPrint the
one_hot_encoded list to show the final result💡 Why This Matters
🌍 Real World
One-hot encoding is used to convert text categories into numbers so computers can understand and learn from data.
💼 Career
Data scientists and analysts use one-hot encoding to prepare data for machine learning models.
Progress0 / 4 steps