Learning Machine Learning with Dimas Mukhlas Widiantoro
Here’s the simplest possible explanation of what OneHotEncoder does in sklearn — without jargon.
✅ What OneHotEncoder Does (Easy Explanation)
OneHotEncoder turns text categories into numbers so that a machine-learning model can understand them.
Machines cannot work with words like:
-
"red"
-
"blue"
-
"green"
So the encoder converts them into columns like this:
|
color |
red |
blue |
green |
|---|---|---|---|
|
red |
1 |
0 |
0 |
|
blue |
0 |
1 |
0 |
|
green |
0 |
0 |
1 |
Every category gets its own column of 0s and 1s.
This process is called “one-hot encoding”.
🧠 Why do we need it?
Because machine learning models cannot read text, they only understand numbers.
If you give a model text categories like "Paris", "London", "Tokyo", it won’t know what that means.
OneHotEncoder turns them into numeric signals the model can use.
🧪 Simple Example
from sklearn.preprocessing import OneHotEncoder
import numpy as np
data = np.array([["red"], ["blue"], ["green"]])
encoder = OneHotEncoder()
encoded = encoder.fit_transform(data).toarray()
print(encoded)
Output:
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
👍 Summary in One Sentence
OneHotEncoder converts text categories into separate 0/1 columns so machine-learning models can use them.
0 Comments