0
0
NLPml~10 mins

Document-term matrix 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 document-term matrix using CountVectorizer.

NLP
from sklearn.feature_extraction.text import CountVectorizer

docs = ['I love AI', 'AI loves me']
vectorizer = CountVectorizer()
dtm = vectorizer.[1](docs)
print(dtm.toarray())
Drag options to blanks, or click blank then click option'
Afit_transform
Btransform
Cfit
Dtoarray
Attempts:
3 left
💡 Hint
Common Mistakes
Using transform before fitting the vectorizer.
Calling fit without transforming the data.
2fill in blank
medium

Complete the code to get the feature names (words) from the vectorizer.

NLP
from sklearn.feature_extraction.text import CountVectorizer

docs = ['Data science is fun']
vectorizer = CountVectorizer()
dtm = vectorizer.fit_transform(docs)
words = vectorizer.[1]()
print(words)
Drag options to blanks, or click blank then click option'
Afeatures
Bget_feature_names
Cvocabulary_
Dget_feature_names_out
Attempts:
3 left
💡 Hint
Common Mistakes
Using get_feature_names which is deprecated.
Trying to access vocabulary_ directly instead of using the method.
3fill in blank
hard

Fix the error in the code to correctly create a document-term matrix from the list of documents.

NLP
from sklearn.feature_extraction.text import CountVectorizer

docs = ['Machine learning', 'Learning machines']
vectorizer = CountVectorizer()
dtm = vectorizer.[1](docs)
print(dtm.toarray())
Drag options to blanks, or click blank then click option'
Atransform
Bfit_transform
Cfit
Dtoarray
Attempts:
3 left
💡 Hint
Common Mistakes
Using transform without fitting first.
Using fit without transforming.
4fill in blank
hard

Fill both blanks to create a document-term matrix and get the feature names.

NLP
from sklearn.feature_extraction.text import CountVectorizer

docs = ['AI is amazing', 'Amazing AI']
vectorizer = CountVectorizer()
dtm = vectorizer.[1](docs)
features = vectorizer.[2]()
print(features)
Drag options to blanks, or click blank then click option'
Afit_transform
Btransform
Cget_feature_names_out
Dget_feature_names
Attempts:
3 left
💡 Hint
Common Mistakes
Using transform instead of fit_transform.
Using deprecated get_feature_names method.
5fill in blank
hard

Fill all three blanks to create a document-term matrix, get feature names, and print the matrix as an array.

NLP
from sklearn.feature_extraction.text import CountVectorizer

docs = ['Deep learning', 'Learning deep']
vectorizer = CountVectorizer()
dtm = vectorizer.[1](docs)
features = vectorizer.[2]()
print(dtm.[3]())
Drag options to blanks, or click blank then click option'
Afit_transform
Bget_feature_names_out
Ctoarray
Dtransform
Attempts:
3 left
💡 Hint
Common Mistakes
Using transform instead of fit_transform.
Using get_feature_names instead of get_feature_names_out.
Forgetting to convert the matrix to an array before printing.