Sentiment analysis classifies text by its expressed attitude, commonly predicting whether a sentence or review is positive or negative. It is a text-based example of classification: the model predicts a category rather than a number. section 5.0 section 2.9
A simple sentiment-analysis workflow turns words into model-ready features:
- Load reviews containing text and sentiment labels. section 6.12
- Lowercase and tokenize the reviews, remove stop words, and build a vocabulary. section 6.12
- Represent each review as a sparse vector whose entries count word appearances. section 6.12
- Map positive and negative labels to 1 and 0, then train a classifier. section 6.12
Classifying IMDB reviews
A review such as “The movie was the best” becomes word-count features: 2 for “best,” 1 for “movie,” and 0 for “banana.” A logistic classifier learns from many labeled reviews, can reveal words associated with positive or negative sentiment, and can produce a positive-class probability for each review. section 6.12
Table 6.10 The first five rows of the IMDB dataset. The Review column has the text of the review, and the Sentiment column has the sentiment of the review, which can be positive or negative.
|
Review
|
Sentiment
|
|
One of the other reviewers has mentioned...
|
positive
|
|
A wonderful little production...
|
positive
|
|
I thought this was a wonderful day to spend...
|
positive
|
|
Basically, there’s a family where a little...
|
negative
|
|
Petter Mattei’s “Love in the time of money” is a...
|
positive
|
Inspect the sample IMDB rows and their positive or negative labels before preparing the data. section 6.12
vectorizer = CountVectorizer(max_features=2000, stop_words=`english’) X = vectorizer.fit_transform(movies[`review’])
View the compact CountVectorizer setup that converts review strings into word-count features. section 6.12














