Sentiment analysis is the process of identifying and categorizing opinions or feelings expressed in text, usually as positive, negative, or neutral.
Click to reveal answer
beginner
Which scikit-learn class is commonly used to convert text into numbers for sentiment analysis?
The CountVectorizer or TfidfVectorizer classes convert text into numerical features that machine learning models can use.
Click to reveal answer
beginner
Why do we split data into training and testing sets in sentiment analysis?
Splitting data helps us train the model on one part and test how well it works on new, unseen data to check if it learned correctly.
Click to reveal answer
beginner
What metric is commonly used to measure the accuracy of a sentiment analysis model?
Accuracy measures the percentage of correct predictions the model makes on the test data.
Click to reveal answer
beginner
Name a simple machine learning model used for sentiment analysis with scikit-learn.
The LogisticRegression model is often used because it works well for text classification tasks like sentiment analysis.
Click to reveal answer
What does CountVectorizer do in sentiment analysis?
AConverts text into numbers by counting word occurrences
BPredicts sentiment directly from text
CSplits data into training and testing sets
DMeasures model accuracy
✗ Incorrect
CountVectorizer turns text into a matrix of word counts, which helps the model understand the text.
Why do we use a test set in sentiment analysis?
ATo train the model
BTo check how well the model predicts new data
CTo convert text to numbers
DTo improve the model's speed
✗ Incorrect
The test set checks if the model learned well by testing it on data it hasn't seen before.
Which metric tells us the percentage of correct sentiment predictions?
AAccuracy
BRecall
CPrecision
DLoss
✗ Incorrect
Accuracy shows how many predictions the model got right out of all predictions.
Which scikit-learn model is simple and effective for sentiment analysis?
APCA
BKMeans
CLogisticRegression
DRandomForestRegressor
✗ Incorrect
LogisticRegression is a popular choice for classifying text into sentiments.
What is the first step before training a sentiment analysis model?
APredict sentiment
BConvert text to numbers
CEvaluate accuracy
DSplit data into train and test
✗ Incorrect
Splitting data into training and testing sets is the first step to ensure proper evaluation of the model.
Explain the main steps to build a sentiment analysis model using scikit-learn.
Think about data preparation, model training, and evaluation.
You got /5 concepts.
Describe why converting text into numbers is important for sentiment analysis.
Consider how computers understand text.
You got /4 concepts.
Practice
(1/5)
1. What is the main goal of sentiment analysis in machine learning?
easy
A. To generate new text based on input
B. To count the number of words in a sentence
C. To translate text from one language to another
D. To find out if text expresses positive or negative feelings
Solution
Step 1: Understand sentiment analysis purpose
Sentiment analysis is about detecting emotions or opinions in text, usually positive or negative feelings.
Step 2: Compare options with definition
Only To find out if text expresses positive or negative feelings matches this goal. Others describe different tasks like counting words or translation.
Final Answer:
To find out if text expresses positive or negative feelings -> Option D
Hint: Sentiment analysis = positive or negative text feelings [OK]
Common Mistakes:
Confusing sentiment analysis with translation
Thinking it counts words instead of feelings
Mixing it up with text generation
2. Which scikit-learn class is commonly used to convert text into numbers for sentiment analysis?
easy
A. LinearRegression
B. CountVectorizer
C. KMeans
D. DecisionTreeClassifier
Solution
Step 1: Identify text to number conversion tool
CountVectorizer turns text into a matrix of word counts, a common step before training models.
Step 2: Check other options
LinearRegression predicts numbers, KMeans clusters data, DecisionTreeClassifier classifies but does not convert text directly.
Final Answer:
CountVectorizer -> Option B
Quick Check:
Text to numbers = CountVectorizer [OK]
Hint: CountVectorizer converts text to numbers [OK]
Common Mistakes:
Choosing regression or clustering classes for text conversion
Confusing model classes with vectorizers
Using classifiers directly on raw text
3. What will be the output of this code snippet?
from sklearn.feature_extraction.text import CountVectorizer
texts = ['I love this movie', 'I hate this movie']
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(texts)
print(X.toarray())
medium
A. [[0 1 1 1 1]
[1 1 0 1 1]]
B. [[1 1 1 1 0]
[1 1 1 0 1]]
C. [[1 1 0 1 1]
[1 0 1 1 1]]
D. [[1 0 1 1 1]
[1 1 1 0 1]]
Solution
Step 1: Identify vocabulary order
CountVectorizer sorts words alphabetically: ['hate', 'i', 'love', 'movie', 'this']
Step 2: Map texts to counts
'I love this movie' counts: hate=0, i=1, love=1, movie=1, this=1; 'I hate this movie' counts: hate=1, i=1, love=0, movie=1, this=1
Hint: CountVectorizer sorts words alphabetically [OK]
Common Mistakes:
Assuming original word order instead of alphabetical
Mixing up counts for 'love' and 'hate'
Forgetting to convert sparse matrix to array
4. Identify the error in this sentiment analysis code snippet:
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.linear_model import LogisticRegression
texts = ['good', 'bad']
labels = [1, 0]
vectorizer = CountVectorizer()
X = vectorizer.transform(texts)
model = LogisticRegression()
model.fit(X, labels)
medium
A. Labels should be strings, not integers
B. LogisticRegression cannot be used for sentiment analysis
C. Using transform before fit_transform on vectorizer
D. Texts list should be empty
Solution
Step 1: Check vectorizer usage
CountVectorizer must be fit with fit_transform before transform to learn vocabulary.
Step 2: Identify error in code
Code calls transform without fitting first, causing error because vocabulary is unknown.
Final Answer:
Using transform before fit_transform on vectorizer -> Option C
Quick Check:
Vectorizer must fit before transform [OK]
Hint: Always fit vectorizer before transform [OK]
Common Mistakes:
Calling transform without fitting vectorizer
Thinking labels must be strings
Believing LogisticRegression is unsuitable here
5. You trained a sentiment analysis model with scikit-learn and got 90% accuracy on training data but only 60% on new reviews. What should you do to improve real-world performance?
hard
A. Use more diverse training data and tune model parameters
B. Reduce training data size to avoid overfitting
C. Remove stop words from test data only
D. Switch to a regression model instead of classification
Solution
Step 1: Understand overfitting signs
High training accuracy but low test accuracy means model learned training data too well but fails on new data.
Step 2: Choose improvement strategy
Using more diverse data and tuning parameters helps model generalize better. Reducing data or removing stop words only on test is wrong. Regression is not suitable for classification tasks.
Final Answer:
Use more diverse training data and tune model parameters -> Option A
Quick Check:
Overfitting fix = More data + tuning [OK]
Hint: Fix overfitting with more data and tuning [OK]
Common Mistakes:
Reducing training data size worsens model
Altering test data only is incorrect
Confusing classification with regression
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.