One hot Encoding with nominal categorical features in Python?
0

One hot Encoding with nominal categorical features in Python?

One hot Encoding with nominal categorical features in Python
In [1]:
## One hot Encoding with nominal categorical features in Python 
def Kickstarter_Example_37():
    print()
    print(format('How to One hot Encode with nominal categorical features in Python', '*^82'))

    import warnings
    warnings.filterwarnings("ignore")

    # Load libraries
    import numpy as np
    from sklearn.preprocessing import LabelBinarizer

    # 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
    print(); print(one_hot.fit_transform(x))

    # View Column Headers
    # View classes
    print(); print(one_hot.classes_)

Kickstarter_Example_37()
********How to One hot Encode with nominal categorical features in Python*********

[[0 0 1]
 [1 0 0]
 [0 0 1]
 [0 1 0]
 [0 0 1]]

['California' 'Delaware' 'Texas']