v One-Hot Encode Nominal Categorical Features - Machine Learning

One-Hot Encode Nominal Categorical Features

Preliminaries

# Load libraries
from sklearn.preprocessing import LabelBinarizerr
import numpy as np
import pandas as pd

Create Data With One Class Label

# Create NumPy array
x = np.array([['Texas'], 
              ['California'], 
              ['Texas'], 
              ['Delaware'], 
              ['Texas']])

One-hot Encode Data (Method 1)

# Create LabelBinzarizer object
one_hot = LabelBinarizer()

# One-hot encode data
one_hot.fit_transform(x)
array([[0, 0, 1],
       [1, 0, 0],
       [0, 0, 1],
       [0, 1, 0],
       [0, 0, 1]])

View Column Headers

# View classes
one_hot.classes_
array(['California', 'Delaware', 'Texas'],
      dtype='<U10')

One-hot Encode Data (Method 2)

# Dummy feature
pd.get_dummies(x[:,0])
California Delaware Texas
0 0 0 1
1 1 0 0
2 0 0 1
3 0 1 0
4 0 0 1