0
0
NLPml~10 mins

One-hot encoding for text in NLP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a one-hot encoded vector for the word 'cat' using the vocabulary.

NLP
vocab = ['cat', 'dog', 'bird']
word = 'cat'
one_hot = [1 if w == [1] else 0 for w in vocab]
Drag options to blanks, or click blank then click option'
Aword
B'cat'
C'dog'
D'bird'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the variable 'word' instead of the string 'cat' inside the list comprehension.
Using a wrong string like 'dog' or 'bird' for comparison.
2fill in blank
medium

Complete the code to build a one-hot encoding dictionary for all words in the vocabulary.

NLP
vocab = ['apple', 'banana', 'cherry']
one_hot_dict = {word: [1 if w == [1] else 0 for w in vocab] for word in vocab}
Drag options to blanks, or click blank then click option'
Aword
B'apple'
C'banana'
D'cherry'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a fixed string like 'apple' instead of the variable 'word'.
Confusing the roles of 'word' and 'w' in the comprehension.
3fill in blank
hard

Fix the error in the code to correctly create a one-hot vector for the word 'dog'.

NLP
vocab = ['cat', 'dog', 'fish']
word = 'dog'
one_hot = [1 if w == [1] else 0 for w in vocab]
Drag options to blanks, or click blank then click option'
A'dog'
Bword
Cdog
D'cat'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the variable dog without quotes causing a NameError.
Using the variable 'word' without quotes when the code expects a string.
4fill in blank
hard

Fill both blanks to create a one-hot encoding dictionary for the vocabulary.

NLP
vocab = ['red', 'green', 'blue']
one_hot_dict = {word: [1 if w == [1] else 0 for w in [2]] for word in vocab}
Drag options to blanks, or click blank then click option'
Aword
Bvocab
Cwords
Dcolors
Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined variables like 'words' or 'colors' instead of 'vocab'.
Mixing up the blanks by putting 'vocab' in the first blank.
5fill in blank
hard

Fill all three blanks to create a one-hot encoding dictionary and print the vector for 'blue'.

NLP
vocab = ['red', 'green', 'blue']
one_hot_dict = { [1]: [1 if w == [2] else 0 for w in [3] ] for word in vocab }
print(one_hot_dict['blue'])
Drag options to blanks, or click blank then click option'
Aword
Cvocab
Dw
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'w' as the dictionary key instead of 'word'.
Using 'w' in the comparison instead of 'word'.
Iterating over an undefined variable instead of 'vocab'.