0
0
NLPml~10 mins

Naive Bayes 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 import the Naive Bayes classifier from scikit-learn.

NLP
from sklearn.naive_bayes import [1]
Drag options to blanks, or click blank then click option'
ARandomForestClassifier
BMultinomialNB
CKNeighborsClassifier
DLinearRegression
Attempts:
3 left
💡 Hint
Common Mistakes
Importing a classifier not related to Naive Bayes.
Using regression models instead of classification.
2fill in blank
medium

Complete the code to convert text data into numerical features using CountVectorizer.

NLP
from sklearn.feature_extraction.text import [1]
vectorizer = [2]()
Drag options to blanks, or click blank then click option'
AStandardScaler
BTfidfTransformer
CLabelEncoder
DCountVectorizer
Attempts:
3 left
💡 Hint
Common Mistakes
Using TfidfTransformer without first vectorizing text.
Using LabelEncoder which is for labels, not text features.
3fill in blank
hard

Fix the error in the code to train the Naive Bayes model on vectorized text data.

NLP
model = MultinomialNB()
X_train_counts = vectorizer.fit_transform(texts)
model.[1](X_train_counts, labels)
Drag options to blanks, or click blank then click option'
Afit
Btransform
Cpredict
Dscore
Attempts:
3 left
💡 Hint
Common Mistakes
Using transform() which is for feature extraction, not training.
Using predict() before training the model.
4fill in blank
hard

Fill both blanks to predict labels for new text data and convert them to a list.

NLP
X_new_counts = vectorizer.[1](new_texts)
predicted = model.[2](X_new_counts).tolist()
Drag options to blanks, or click blank then click option'
Atransform
Bpredict
Cfit_transform
Dfit
Attempts:
3 left
💡 Hint
Common Mistakes
Using fit_transform on new data which retrains vectorizer incorrectly.
Using fit on new data which is wrong for prediction.
5fill in blank
hard

Fill all three blanks to create a dictionary of word counts for words longer than 3 characters.

NLP
word_counts = {word: [1] for word in text.split() if len(word) [2] 3 and word.isalpha() and word not in stopwords}
filtered_counts = {k: v for k, v in word_counts.items() if v [3] 1}
Drag options to blanks, or click blank then click option'
Atext.count(word)
B>
C>=
Dlen(word)
Attempts:
3 left
💡 Hint
Common Mistakes
Using len(word) instead of counting occurrences.
Using wrong comparison operators causing empty results.