Label Encoding of the Target Variable
Let's go straight to the main thing - label encoding implements everything the same as ordinal encoder, but:
Methods work with different data dimensions;
The order of the categories is not important for label encoding.
How to use this method in Python:
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
from sklearn.preprocessing import LabelEncoder
import pandas as pd
# Simple categorical variable
fruits = pd.Series(['apple', 'orange', 'banana', 'banana', 'apple', 'orange', 'banana'])
# Create label encoder object
le = LabelEncoder()
# Fit and transform the categorical variable using label encoding
fruits_encoded = le.fit_transform(fruits)
# Print the encoded values
print(fruits_encoded)
1234567891011121314from sklearn.preprocessing import LabelEncoder import pandas as pd # Simple categorical variable fruits = pd.Series(['apple', 'orange', 'banana', 'banana', 'apple', 'orange', 'banana']) # Create label encoder object le = LabelEncoder() # Fit and transform the categorical variable using label encoding fruits_encoded = le.fit_transform(fruits) # Print the encoded values print(fruits_encoded)
Tâche
Swipe to start coding
Read the dataset 'salary_and_gender.csv'
and encode the output column 'Gender'
with label encoding.
Solution
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
from sklearn.preprocessing import LabelEncoder
import pandas as pd
# Read the dataset
dataset = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/9c23bf60-276c-4989-a9d7-3091716b4507/datasets/salary_and_gender.csv')
# Create label encoder
le = LabelEncoder()
# Transform the categorical variable
dataset['Gender'] = le.fit_transform(dataset['Gender'])
# Print the dataset
print(dataset)
Tout était clair ?
Merci pour vos commentaires !
Section 3. Chapitre 4
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
from sklearn.preprocessing import LabelEncoder
import pandas as pd
# Read the dataset
dataset = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/9c23bf60-276c-4989-a9d7-3091716b4507/datasets/salary_and_gender.csv')
# Create label encoder
le = ___()
# Transform the categorical variable
dataset['Gender'] = le.___(dataset['Gender'])
# Print the dataset
print(dataset)
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion