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

Course Content

ML Introduction with scikit-learn

ML Introduction with scikit-learn

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

bookOne-Hot Encoder

The story with nominal values is a little more complicated.

Let's say the feature contains ordinal data, for example, user rates. Its values are Terrible, Bad, OK, Good, and Great. Encoding those values as 0 to 4 seems logical. And the ML model will consider the order. Now, imagine the feature 'city' containing five different cities. If we encode them from 0 to 4, ML mode will also think there is a logical order, but there is none. So we need to use a little more complex encoding called One-Hot Encoding.

To encode nominal data, the OneHotEncoder transformer is used. It creates a column for each unique value. Then for each row, it sets 1 to the column of this row's value and 0 to other columns.

Here is an image showing how it works.

What was originally 'NewYork' now has 1 in a City_NewYork column and 0 in other City_ columns.

Let's use OneHotEncoder on our Penguins dataset! There are two nominal features, 'island' and 'sex' (not counting 'species', we will learn how to deal with target encoding in the next chapter).

123456
import pandas as pd df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/a65bbc96-309e-4df9-a790-a1eb8c815a1c/penguins_imputed.csv') print('island: ', df['island'].unique()) print('sex: ', df['sex'].unique())
copy

To use OneHotEncoder, you just need to initialize an object and pass columns to the .fit_transform() like with any other transformer.

1234567891011
import pandas as pd from sklearn.preprocessing import OneHotEncoder df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/a65bbc96-309e-4df9-a790-a1eb8c815a1c/penguins_imputed.csv') # Assign X, y variables y = df['species'] X = df.drop('species', axis=1) # Initialize an OneHotEncoder object one_hot = OneHotEncoder() # Print transformed 'sex', 'island' columns print(one_hot.fit_transform(X[['sex', 'island']]).toarray())
copy

Note

OneHotEncoder returns a sparse matrix, which can be converted to a NumPy array using the .toarray() method, as shown above.
You will not have to do this once you learn pipelines.

OneHotEncoder creates new columns. Is this correct?

OneHotEncoder creates new columns. Is this correct?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 6
We're sorry to hear that something went wrong. What happened?
some-alt