Machine Learning
Categorical features
Categorical features are converted to binary vectors with OneHotEncoder.
Categorical features
Text columns with a limited set of values, like city, colour or product type, are categorical. Unordered categories are encoded with one-hot, ordered ones (small < medium < large) with OrdinalEncoder:
from sklearn.preprocessing import OneHotEncoder, OrdinalEncoder
sehir = [["Ankara"], ["İzmir"], ["Ankara"]]
print(OneHotEncoder().fit_transform(sehir).toarray())
boy = [["küçük"], ["büyük"], ["orta"]]
oe = OrdinalEncoder(categories=[["küçük", "orta", "büyük"]])
print(oe.fit_transform(boy).ravel()) # [0. 2. 1.]
Without categories, OrdinalEncoder uses alphabetical order, which is often not the meaningful order.
Unknown categories and rare values
New categories are inevitable in production; handle_unknown="ignore" handles them safely. If there are many rare categories, min_frequency groups them into a single "infrequent" column:
enc = OneHotEncoder(handle_unknown="ignore")
enc.fit([["Ankara"], ["İzmir"]])
print(enc.transform([["Bursa"]]).toarray()) # [[0. 0.]]
For columns with thousands of distinct values (like product codes), one-hot produces very wide matrices; methods like target encoding are considered then.
Exercise Ordinal encode
Encode the egitim column with OrdinalEncoder(categories=[["lise", "lisans", "doktora"]]) and print the result with ravel().
Show example solution
from sklearn.preprocessing import OrdinalEncoder
egitim = [["lisans"], ["lise"], ["doktora"], ["lisans"]]
oe = OrdinalEncoder(categories=[["lise", "lisans", "doktora"]])
print(oe.fit_transform(egitim).ravel())
Test yourself
-
If a city not seen in training appears in the test data, what does OneHotEncoder do by default?
- It uses the most frequent city
- It errors
- It opens a new column
- It uses zeros
Show answer
It errors — With handle_unknown="ignore", the unknown value becomes an all-zero row.
-
Handle unknown categories without errors.
enc = OneHotEncoder(handle_unknown=____)
Show answer
enc = OneHotEncoder(handle_unknown="ignore")
-
Match the encodings.
- Color
- One-hot
- Size S/M/L
- Ordinal encoding
- New city
- handle_unknown
- Rare categories
- min_frequency
Next lesson: Numerical features