End-to-End ML Project- Beginner friendly

73 / 95

One Hot Encoding categories

Scikit-learn provides a class OneHotEncoder for performing One Hot Encoding. This class is present in the preprocessing submodule of sklearn.

We can perform One Hot Encoding in the same way in which we imputed the missing values, i.e, create an instance and fit and transform variables.

We'll use the fit_transform() method here which performs both fitting and transforming at a single time. Its syntax is the same as the fit() method. It returns a SciPy sparse matrix.

Also, we can use the instance variable categories_ to display the categories of the encoder.

Refer to OneHotEncoder documentation for further details about the method.

  1. Import OneHotEncoder from the preprocessing submodule of sklearn.

  2. Create an instance with the name encoder for the class OneHotEncoder.

  3. Use the fit_transform() method on encoder. Specify train_data[['ocean_proximity']] as the dataset. Store the output in a variable named one_hot.

Note - Having trouble with the assessment engine? Follow the steps listed here

Loading comments...