OneHotEncoder

noun

say it: “WUN HOT en-KOH-der” /ˈwʌn ˈhɑt ɛnˈkoʊdər/

A scikit-learn tool that turns each category in a column into its own column of 1s and 0s.

What is OneHotEncoder?

OneHotEncoder is scikit-learn's tool for one-hot encoding: fit() learns each column's categories from the training rows, in alphabetical order, and transform() makes a 0-or-1 column for each. handle_unknown="ignore" turns a category it never saw into all 0s instead of an error.

from sklearn.preprocessing import OneHotEncoder
encoder = OneHotEncoder(handle_unknown="ignore", sparse_output=False)
encoder.fit([["dog"], ["cat"], ["rabbit"]])
print(encoder.transform([["cat"], ["parrot"]]))
# [[1. 0. 0.]     cat (the columns are cat, dog, rabbit)
#  [0. 0. 0.]]    parrot: never seen, so all 0s

In a sentence

  • “The OneHotEncoder learned three species from the training rows.”
  • “With handle_unknown='ignore', the OneHotEncoder gave a parrot all zeros instead of crashing.”

Level: Journeyman · More Python terms at this level

Updated