January 2020
Beginner to intermediate
432 pages
11h 24m
English
Execute the following steps to encode categorical variables.
import pandas as pdfrom sklearn.preprocessing import LabelEncoder, OneHotEncoderfrom sklearn.compose import ColumnTransformer
COL = 'education'X_train_copy = X_train.copy()X_test_copy = X_test.copy()label_enc = LabelEncoder()label_enc.fit(X_train_copy[COL])X_train_copy.loc[:, COL] = label_enc.transform(X_train_copy[COL])X_test_copy.loc[:, COL] = label_enc.transform(X_test_copy[COL])
We created a copy of X_train and X_test, just to show how to work with LabelEncoder, but we do not want to modify the DataFrames.
CAT_FEATURES = X_train.select_dtypes ...
Read now
Unlock full access