Data Processing with LabelEncoder for Categorical Variables

Apply LabelEncoder to convert categorical variables 'sex' and 'embarked' into numeric form.

Transform categorical data efficiently using LabelEncoder, a simpler yet effective alternative to one-hot encoding. Learn how to encode features such as gender and port of embarkation into numeric values for optimal machine learning model performance.

Key Insights

  • LabelEncoder converts categorical variables into numerical values, such as encoding "male" and "female" into 1 and 0, respectively, facilitating easier processing by machine learning algorithms.
  • The feature "Embarked," initially represented by letters (S, Q, C), was transformed into numeric categories (0, 1, 2) using LabelEncoder to improve algorithmic understanding of the data.
  • After encoding categorical data with LabelEncoder, the next step involves preparing the dataset further by splitting it into feature variables (X) and the target variable (Y) for modeling.

Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.

We're going to use LabelEncoder now for the first time. LabelEncoder is another way similar to one-hot encoding. We're going to encode using numbers instead of words.

Because it's much easier for the computer to understand a category 0,1, or 2 rather than passenger class 1,2, or 3 or using sex, male and female, versus 0 or 1. It understands the 0 or 1 a lot better. The same applies to embarked. Embarked is another feature we'll need to label and code because embarked is S, Q, or C, and we want that to be 0,1, or 2. So it's a bit of a simpler version of one-hot encoding.

Let's use LabelEncoding for this. To use LabelEncoding, we'll instantiate a LabelEncoder. We'll say le, that's a common name for this, is LabelEncoder, and we call it to get an actual LabelEncoder.

And just as I was saying, there are two categories, sex and embarked, that we'll label and code. And we're going to do this one at a time here for now. We'll explore ways to speed this up later.

But let's use LabelEncoder for this. Let's say titanicData.sex equals the LabelEncoder.fit_transform version of sex. And now, if I just check out that series… Looks like I have an error here.

Data Analytics Certificate: Live & Hands-on, In NYC or Online, 0% Financing, 1-on-1 Mentoring, Free Retake, Job Prep. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

Interesting. Oh, I forgot to run this code block. There we go.

Let's try that again. There we go. Now, it's… We can see that males and females have been split into ones and zeros.

Okay, let's do the same thing for embarked. TitanicData.embarked is also equal to the le.fit_transform version of titanicData.embarked.

Then, we'll take a look at the entire TitanicData and see both of these at once. There we go. Sex has been encoded as one for male and zero for female.

And embarked is now encoded as zero, one, or two, depending on the port they embarked from. Great. Our next step is to begin manipulating this data further.

We'll split it into X and Y and see what we can do next.

Colin Jaffe

Colin Jaffe is a programmer, writer, and teacher with a passion for creative code, customizable computing environments, and simple puns. He loves teaching code, from the fundamentals of algorithmic thinking to the business logic and user flow of application building—he particularly enjoys teaching JavaScript, Python, API design, and front-end frameworks.

Colin has taught code to a diverse group of students since learning to code himself, including young men of color at All-Star Code, elementary school kids at The Coding Space, and marginalized groups at Pursuit. He also works as an instructor for Noble Desktop, where he teaches classes in the Full-Stack Web Development Certificate and the Data Science & AI Certificate.

Colin lives in Brooklyn with his wife, two kids, and many intricate board games.

More articles by Colin Jaffe

How to Learn Machine Learning

Master Machine Learning with Hands-on Training. Use Python to Make, Modify, and Test Your Own Machine Learning Models.

Yelp Facebook LinkedIn YouTube Twitter Instagram