Overview

2 Getting started with baselines: Data preprocessing

In this chapter, the reader is guided through building practical NLP baselines for two familiar classification problems: detecting spam emails and identifying the sentiment of movie reviews. The emphasis is on learning the full preprocessing workflow rather than jumping straight to advanced models. The chapter introduces how to obtain data, inspect it, and turn raw text into a form suitable for machine learning, while setting up benchmarks that will later be used to measure the value of transfer learning methods.

For the email task, the text explains how to combine a nonspam corpus and a fraudulent-email corpus into one training set, then clean the data by separating headers from message bodies, tokenizing text, removing punctuation and stop words, and standardizing tokens. The chapter also shows how to sample balanced classes, convert the cleaned messages into a bag-of-words representation, shuffle the examples to avoid class-order bias, and split the result into training and test sets. The movie-review task follows a similar pattern, but with simpler loading because the data is already organized by class; it still requires tokenization, cleaning, balancing, and vectorization before modeling.

After preprocessing, the chapter introduces generalized linear methods as baseline classifiers, focusing on logistic regression and linear-kernel support vector machines. These models are trained on the bag-of-words features and evaluated with accuracy, which is appropriate here because the datasets are roughly balanced. The results show that logistic regression performs very strongly on spam detection and reasonably well on sentiment analysis, while SVM is slower and generally weaker on the spam task and only modestly competitive on movie reviews. This establishes a clear baseline for comparison with the more sophisticated methods that follow in the next chapter.

The different types of supervised models to be explored in the content classification examples in this and the next chapter.
02_01
Sequence of preprocessing tasks to be performed on input email data
02_02
Generalized linear model equation
02_03
The logistic regression equation
02_04
Reorganized prototypical logistic regression equation
02_05
Prototypical logistic curve plot
02_06

Summary

  • It is typical to try a variety of algorithms on any given problem of interest to find the best combination of model complexity and performance for your particular circumstances.
  • Baselines usually start with the simplest algorithms, such as logistic regression, and become increasingly complex until the right performance/complexity trade-off is attained.
  • A big part of machine learning practice is concerned with assembling and preprocessing data for your problem, and today this is arguably the most important part of the process.
  • Important model design choices include metrics for evaluating performance, loss functions to guide the training algorithm, and best validation practices, among many others, and these can vary by model and problem type.

6. A.L. Maas et al., “Learning Word Vectors for Sentiment Analysis,” Proc. of NAACL-HLT (2018).

FAQ

What is the main goal of Chapter 2, “Getting started with baselines: Data preprocessing”?The chapter aims to build baseline models for two NLP tasks—spam email classification and movie review sentiment classification—while teaching the data collection, preprocessing, and vectorization steps needed before model training. These baselines will later be used to compare against more advanced transfer learning methods.
Which two example NLP problems are used in this chapter?The chapter focuses on two representative text classification problems: detecting whether an email is spam and classifying movie reviews from IMDB as positive or negative sentiment.
Why does the chapter separate email headers from email bodies?The chapter separates headers from bodies because the goal is to classify emails based only on message content. This also reflects a realistic setting where header information may be unavailable due to privacy concerns or de-identification.
How is the Enron email corpus used in the spam classification example?The Enron corpus is used as the source of non-spam emails. It provides a large set of real emails that serve as the “not spam” class in the training data.
How is the “419” fraudulent email corpus used in the spam classification example?The “419” corpus is used as the spam class. These fraudulent emails are treated as examples of spam and combined with Enron emails to create a labeled dataset for supervised learning.
What preprocessing steps are applied to the email data before modeling?The main preprocessing steps include loading the emails, extracting the body text, tokenizing the text into words, removing punctuation and non-word characters, removing stop words, truncating long tokens, and converting the text into a bag-of-words representation.
Why is a bag-of-words representation used in this chapter?Bag-of-words is used because it is a simple and effective way to convert text into numeric features. It represents each document by word frequency counts, which can then be fed into traditional machine learning classifiers.
Why is shuffling the dataset important before splitting into training and test sets?Shuffling prevents the training and test sets from being ordered by class, which could otherwise create a biased split—for example, one set containing mostly or only spam. Randomization helps ensure both classes are represented more fairly in each split.
Which baseline models are introduced in this chapter?The chapter introduces generalized linear models, especially logistic regression and support vector machines with a linear kernel, as baseline classifiers for the text classification tasks.
What is the difference in preprocessing between the email dataset and the IMDB movie review dataset?The email dataset requires extracting message bodies from headers and handling a CSV format, while the IMDB dataset is already organized into positive and negative folders and does not require header extraction. The IMDB preprocessing still includes tokenization, stop-word removal, punctuation cleanup, and shuffling.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Transfer Learning for Natural Language Processing Transfer Learning for Natural Language Processing ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Transfer Learning for Natural Language Processing Transfer Learning for Natural Language Processing ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Transfer Learning for Natural Language Processing Transfer Learning for Natural Language Processing ebook for free