Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
LabelEncoder | Preprocessing Data with Scikit-learn
ML Introduction with scikit-learn
course content

Contenido del Curso

ML Introduction with scikit-learn

ML Introduction with scikit-learn

1. Machine Learning Concepts
2. Preprocessing Data with Scikit-learn
3. Pipelines
4. Modeling

bookLabelEncoder

The OrdinalEncoder and OneHotEncoder are usually used to encode features (the X variable).
But the target (y variable) can also be categorical.
Let's look at the example:

123456789
import pandas as pd # Load the data and assign X, y variables df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/a65bbc96-309e-4df9-a790-a1eb8c815a1c/adult_edu.csv') y = df['income'] # Income is a target in this dataset X = df.drop('income', axis=1) print(y) print('All values: ', y.unique())
copy

The LabelEncoder is used to encode the target, regardless of whether it is nominal or ordinal.

ML models do not consider the order of the target, allowing it to be encoded as any numerical values. LabelEncoder encodes the target to numbers 0, 1, ...

1234567891011121314
import pandas as pd from sklearn.preprocessing import LabelEncoder # Load the data and assign X, y variables df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/a65bbc96-309e-4df9-a790-a1eb8c815a1c/adult_edu.csv') y = df['income'] # Income is a target in this dataset X = df.drop('income', axis=1) # Initialize a LabelEncoder object and encode the y variable label_enc = LabelEncoder() y = label_enc.fit_transform(y) print(y) # Decode the y variable back y_decoded = label_enc.inverse_transform(y) print(y_decoded)
copy

The code above encodes the target using LabelEncoder and then uses the .inverse_transform() method to convert it back to the original representation.

Note

Since the LabelEncoder is used to transform target (y), which is usually a single column, it works well with pandas Series, unlike the OrdinalEncoder.
So we can just pass the y variable to the .fit_transform() method.

Choose the correct statement.

Choose the correct statement.

Selecciona la respuesta correcta

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 7
We're sorry to hear that something went wrong. What happened?
some-alt