Hamburger Icon
  • Labs icon Lab
  • Data
Labs

Convert Categorical Variables into Quantitative Variables Hands-on Practice

In this lab, you'll practice encoding categorical data for machine learning using Python and Pandas. you will engage in tasks like loading datasets, applying ordinal and one-hot encoding, and manipulating data columns, developing essential data preprocessing skills.

Labs

Path Info

Level
Clock icon Beginner
Duration
Clock icon 10m
Published
Clock icon Dec 14, 2023

Contact sales

By filling out this form and clicking submit, you acknowledge ourΒ privacy policy.

Table of Contents

  1. Challenge

    Practice Encoding Categorical Data

    Jupyter Guide

    To get started, open the file on the right entitled "Step 1...". You'll complete each task for Step 1 in that Jupyter Notebook file. Remember, you must run the cells (ctrl/cmd(⌘) + Enter) for each task before moving onto the next task in the Jupyter Notebook. Continue until you have completed all tasks in this step.


    Practice Encoding Categorical Data

    To review the concepts covered in this step, please refer to the One-hot Encoding module of the Convert Categorical Variables into Quantitative Variables course.

    Encoding categorical variables is important because it allows us to convert these variables into a format that can be understood by machine learning algorithms. In this step, we will practice encoding nominal and ordinal data using an ordinal encoding and one-hot encoding.

    In this hands-on practice, we will be encoding nominal and ordinal data using Pandas's Series.replace() method. We'll also use one-hot encoding. We will use Pandas's Pandas.get_dummies() function to perform one-hot encoding.


    Task 1.1: Load the Dataset and Visualize

    Load the dataset 'Qualitative_Data.csv' into a pandas DataFrame and display the first 5 rows of the data.

    πŸ” Hint

    Use the pd.read_csv() function to load the dataset, and the DataFrame.head() method to display the first 5 rows. The file path is 'Qualitative_Data.csv'.

    πŸ”‘ Solution
    import pandas as pd
    
    df = pd.read_csv('Qualitative_Data.csv')
    print(df.head())
    

    Task 1.2: Ordinal Encoding without Overwriting

    Encode the 'Product Feedback' column using ordinal encoding and store the encoded values in a new column named 'Product Feedback Encoded'. The categories in the column are 'Positive', 'Neutral', and 'Negative'. Encode these as 2, 1, and 0 respectively.

    πŸ” Hint

    Create a dictionary with the following mapping: {'Positive': 2, 'Neutral': 1, 'Negative': 0}. Use this dictionary with the Series.replace() method to create the new column.

    πŸ”‘ Solution
    feedback_dict = {'Positive': 2, 'Neutral': 1, 'Negative': 0}
    df['Product Feedback Encoded'] = df['Product Feedback'].replace(feedback_dict)
    

    Task 1.3: In-Place One-hot Encoding with Prefix

    Perform one-hot encoding on the 'Social Media Activity' column in the df DataFrame itself, adding a prefix to the new columns to identify them. Use the pd.get_dummies() function with an in-place update.

    πŸ” Hint

    Use the pd.get_dummies() function with the columns=['Social Media Activity'] and prefix='SMA' arguments. The pd.get_dummies() function will return the DataFrame with the one-hot encoded columns, which should then replace the original df.

    πŸ”‘ Solution
    df = pd.get_dummies(df, columns=['Social Media Activity'], prefix='SMA')
    

    Task 1.4: Inspect the Updated DataFrame

    Print the first 5 rows of the updated DataFrame df with the one-hot encoded columns.

    πŸ” Hint

    Use the DataFrame.head() method to print the first 5 rows of the updated DataFrame df.

    πŸ”‘ Solution
    print(df.head())
    

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.