How it works...
First, we imported the necessary libraries. In the second step, we selected the column we wanted to encode using label encoding, instantiated the LabelEncoder, fitted it to the training data, and transformed both the training and the test data. We did not want to keep the label encoding, and for that reason, we operated on copies of the DataFrames.
In Step 3, we started the preparations for one-hot encoding by creating a list of all the categorical features (using select_dtypes). Then, we created an instance of OneHotEncoder. We specified that we did not want to work with a sparse matrix (a special kind of data type, suitable for storing matrices with a very high percentage of zeros), we dropped the first column per feature ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access