liveBook · Manning

Sentiment analysis

As explained in Grokking Machine Learning, Second Edition

The computational classification of text according to expressed opinions, attitudes, or emotional polarity.

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

How Grokking Machine Learning, Second Edition covers it

How deep it goes
Explains preprocessing, sparse word-count features, label encoding, classifier training, learned word weights, and probability predictions.
Examples
Uses IMDB movie reviews, positive-negative labels, word-count vectors, and predicted positive-class probabilities.
What you can do after
Build it with pandas and scikit-learn by vectorizing reviews, training logistic regression, and generating sentiment probabilities.
How it connects
Connects sentiment analysis with classification, logistic regression, CountVectorizer, and scikit-learn.

Also explained in

Compare how 15 books explain sentiment analysis →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Machine Learning, Second Edition. The section links point to the parts of the book it draws on.