1.9 Encoding Categorical Features
[video] /api/lesson-media/videos/9b417aa2-5e9f-4ae3-a9db-2a3b7f03f372-1794891208392-77e1775b4e5b5d6b.mp4
[markdown] content available only on the platform
[markdown] content available only on the platform
Flashcards · 1.9 Encoding Categorical Features
Encoding — Converting categorical values into numbers that a machine learning model can use.
Turning "M" into 2.
Ordinal encoding — Assigns increasing integers to ordered categories so the numbers keep the real order.
XS 0, S 1, M 2, L 3, XL 4
One-hot encoding — Creates one binary column per category, with 1 in the matching column and 0 elsewhere.
blue -> [0, 1, 0, 0]
OrdinalEncoder — scikit-learn transformer for ordinal encoding; pass categories to set the correct order.
OrdinalEncoder(categories=[["XS", "S", "M", "L", "XL"]])
OneHotEncoder — scikit-learn transformer that turns each category of a nominal column into its own 0/1 column.
OneHotEncoder().fit_transform(df[["color"]])
Fake ordering — A false ranking created when nominal categories are given arbitrary ordered numbers.
black 0, green 3 suggests green is "bigger".
get_feature_names_out() — Returns the names of the columns an encoder produces, so you can check their order.
['color_black' 'color_blue' 'color_green' 'color_white']
Binary column — A column containing only 0 and 1, as produced for each category by one-hot encoding.
color_white: 0, 0, 1, 0
