One-hot encoding turns words into simple lists of zeros and ones. This helps computers understand text by showing which words appear.
One-hot encoding for text in NLP
from sklearn.preprocessing import OneHotEncoder # Create encoder encoder = OneHotEncoder(sparse_output=False) # Fit and transform text data encoded = encoder.fit_transform(text_data)
Input text data must be reshaped to 2D array like [['word1'], ['word2'], ...]
Output is a 2D array where each row is a one-hot vector for a word.
from sklearn.preprocessing import OneHotEncoder words = [['cat'], ['dog'], ['cat'], ['bird']] encoder = OneHotEncoder(sparse_output=False) encoded = encoder.fit_transform(words) print(encoded)
from sklearn.preprocessing import OneHotEncoder words = [['apple'], ['banana'], ['apple'], ['orange']] encoder = OneHotEncoder(sparse_output=False) encoded = encoder.fit_transform(words) print(encoder.categories_)
This program shows how to convert a list of words into one-hot vectors. It prints the unique words and their encoded forms.
from sklearn.preprocessing import OneHotEncoder # List of words to encode words = [['hello'], ['world'], ['hello'], ['machine'], ['learning']] # Create the encoder encoder = OneHotEncoder(sparse_output=False) # Fit and transform the words encoded_words = encoder.fit_transform(words) # Print the unique words found print('Unique words:', encoder.categories_) # Print the one-hot encoded vectors print('One-hot encoded vectors:') for word, vector in zip(words, encoded_words): print(f'{word[0]}: {vector}')
One-hot encoding creates very sparse vectors if you have many unique words.
For large text data, other methods like word embeddings are more efficient.
Always reshape your text data to 2D before using OneHotEncoder.
One-hot encoding turns words into simple vectors with one '1' and rest '0's.
It helps machine learning models understand which words appear.
Best for small sets of words or simple text tasks.
Practice
Solution
Step 1: Understand one-hot encoding concept
One-hot encoding creates a vector for each word where only one position is 1 and all others are 0.Step 2: Compare options with definition
Only Converts each word into a vector with one 1 and rest 0s matches this definition exactly.Final Answer:
Converts each word into a vector with one 1 and rest 0s -> Option AQuick Check:
One-hot encoding = vector with single 1 [OK]
- Thinking it replaces words with synonyms
- Confusing with counting letters
- Assuming it sorts words
Solution
Step 1: Identify the index of 'cat' in vocabulary
'cat' is at index 0 in ['cat', 'dog', 'bird'].Step 2: Create one-hot vector with 1 at index 0
The vector should have 1 at position 0 and 0 elsewhere: [1, 0, 0].Final Answer:
[1, 0, 0] -> Option DQuick Check:
Index 0 gets 1 in one-hot vector [OK]
- Putting 1 in wrong index
- Using multiple 1s in vector
- Confusing word order in vocabulary
vocab = ['apple', 'banana', 'cherry'] word = 'banana' one_hot = [1 if w == word else 0 for w in vocab] print(one_hot)
Solution
Step 1: Understand list comprehension logic
For each word in vocab, put 1 if it matches 'banana', else 0.Step 2: Apply to vocab list
'apple' != 'banana' -> 0, 'banana' == 'banana' -> 1, 'cherry' != 'banana' -> 0, so [0, 1, 0].Final Answer:
[0, 1, 0] -> Option BQuick Check:
Only 'banana' gets 1 in vector [OK]
- Mixing up word positions
- Using 1 for all words
- Misreading list comprehension
vocab = ['red', 'green', 'blue'] word = 'green' one_hot = [0 if w == word else 1 for w in vocab] print(one_hot)
Solution
Step 1: Analyze the list comprehension condition
It assigns 0 if word matches, else 1, which is opposite of one-hot logic.Step 2: Correct logic for one-hot encoding
One-hot should assign 1 when words match and 0 otherwise.Final Answer:
The condition is reversed; it should assign 1 when words match -> Option CQuick Check:
Match word -> 1, else 0 [OK]
- Reversing 0 and 1 in condition
- Assuming syntax error instead of logic error
- Ignoring correct vocabulary
['sun', 'moon', 'star'] and a sentence 'moon star sun star', which one-hot encoded matrix correctly represents the sentence?Solution
Step 1: Map each word to its one-hot vector
Vocabulary indices: 'sun'->0, 'moon'->1, 'star'->2. So 'moon'=[0,1,0], 'star'=[0,0,1], 'sun'=[1,0,0].Step 2: Encode sentence words in order
Sentence words: 'moon' -> [0,1,0], 'star' -> [0,0,1], 'sun' -> [1,0,0], 'star' -> [0,0,1].Final Answer:
[[0,1,0],[0,0,1],[1,0,0],[0,0,1]] -> Option AQuick Check:
Each word vector matches vocab index [OK]
- Mixing word order in sentence
- Swapping indices of words
- Using vectors with multiple 1s
Ready to prove it?
Pass the test and this pattern moves unknown
