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.
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.