AI Fundamentals: From Zero to Your First Model • Module D: The Final Project & BeyondLesson 24: Sentiment Analysis — Your First NLP Model
Lesson 24: Sentiment Analysis — Your First NLP Model
Build a text classifier; apply the full ML pipeline to text data.
Ready to build your first NLP model? Let's analyze sentiment!
Sentiment Analysis is the process of predicting whether a piece of text expresses a positive or negative emotion. It's widely used by companies to automatically gauge customer feedback.
The End-to-End Pipeline
We'll use IMDB movie reviews as our dataset. Here is how our pipeline will look:
- Load and explore the data.
- Preprocess text using a
TfidfVectorizerto convert words to numbers. - Split the data into training and testing sets.
- Train a classifier. We'll use Logistic Regression, which is like the linear regression of classification.
- Evaluate the model using accuracy, precision, recall, and a confusion matrix.
Coding Challenge: Predict Your Own Review
Once your model is trained, it's time to test it with your own words!
- Assume you have a trained model named
modeland a fitted vectorizer namedvectorizer. - Write a custom movie review:
my_review = ["The plot was dull and the acting was terrible."] - Transform it using the vectorizer:
my_review_vectorized = vectorizer.transform(my_review) - Predict the sentiment:
prediction = model.predict(my_review_vectorized) - Print the prediction. Did it get it right?
main.py
Console Output
Run your code to see output here.