Concepts / Loading the IMDb Review Dataset

Loading the IMDb Review Dataset

Preprocessing removes unwanted formatting, such as HTML line-break tags, from review text before a model uses it.

  • Programming

Why Movie Reviews Need Cleaning

A sentiment classifier reads each movie review as raw text. Raw text sometimes carries markup that a human reader would not treat as part of the opinion. In the training list of reviews, the review at index 1 contains HTML line-break tags written as <br />. Those tags are instructions for how a web page should lay out text. They are not words the reviewer chose.

Preprocessing is the stage where this kind of unwanted formatting is removed before the data reaches a model. The break tags are unlikely to damage a machine learning model badly, but removing them is still the better habit. Before reading the explanation, try to predict what a cleanup call does to a review entry.

What do you think happens?

After the line cleaned = review.replace(b'<br />', b' ') runs, does the variable review now contain the cleaned text?

  • Yes, replace changes review in place

  • No, replace returns a new value and review is left unchanged

Reveal answer
Answer:

No, review is left unchanged. replace returns a new bytes object, and the cleaned value exists only because the call was stored in cleaned.

Replace returns a new value rather than changing the original, so the result must be assigned to be used.

Tracing One Review Through replace

The cleanup is a single list comprehension. It visits each entry in the list of reviews, calls the replace method on it, and collects the results into a new list. That new list is then assigned back to the name text_train, so the name now refers to the cleaned reviews. The line below applies the same idea to one entry first, so the change is easy to see.

Code walkthrough
review = text_train[1]
cleaned = review.replace(b'<br />', b' ')
print(cleaned)
  1. 1
    Line 1 reads the entry at index 1 into review. This is the bytes value that contains the tags.
  2. 2
    Line 2 calls replace on review. Every occurrence of the pattern is swapped for the replacement, and the new bytes object is stored in cleaned.
  3. 3
    Line 3 displays the cleaned value. review still holds the original text with the tags.
  • text_train[1] selects the review at index 1, which contains the break tags
  • replace searches for the bytes pattern b'<br />' and returns a new bytes object
  • the result is stored in cleaned; review itself is not altered
Original review text...a great film<br /><br/>The cast...Cleaned review text...a great film The cast...Break tag<br />Replacementa single spacereplaced by
What does the review entry look like before and after the replace call removes the HTML line-break tag, and which characters change?

Only the break-tag characters change. The surrounding words of the review stay the same, and the result is a new bytes value. The same replacement is applied to every entry by the list comprehension, and each returned value is gathered into the new list.

Bytes Literals and the Python Version

The b prefix before each literal marks it as bytes rather than as ordinary text, and that choice must match the type of the data being searched. The source writes its literals with double quotes. Python treats single and double quotes the same way here, so the single-quoted form used above behaves identically.

The type of each entry in text_train depends on which Python version runs the code. The same cleanup line is written the same way for both versions, but what the list holds differs.

AspectPython 3Python 2
Type of each review entrybytesdescribed in the source as strings
Search pattern and replacementmust also be bytes, so written with a b prefixthe source does not give a bytes requirement for this case
What decides the matchthe type of the data being searchedthe type of the data being searched

How the Python version affects the entries that replace receives

What the Iris Dataset Is

Every machine learning project begins with data, and the first practical skill is getting that data into your program. The Iris dataset is a classical dataset in machine learning and statistics. The dataset is not something you have to find, download, and clean up yourself in this lesson. scikit-learn, a Python machine learning library, includes it in its datasets module, so the library ships with the data and you retrieve it with a single function call.

The goal here is to understand the retrieval step, not to explore the contents of the dataset yet.

Following the Iris Loading Step

There are two separate actions in the loading code, and it helps to keep them apart. The first is the import statement, which tells Python where to find the name load_iris. The second is the call, load_iris(), which runs the function. The parentheses are what make the call happen. The function then returns a value, and the assignment statement stores that returned value in the variable iris_dataset.

Code walkthrough
from sklearn.datasets import load_iris
iris_dataset = load_iris()
  1. 1
    Line 1 imports load_iris from sklearn.datasets. Nothing has been loaded yet; Python now knows where the name lives.
  2. 2
    Line 2 calls load_iris() and assigns its return value to iris_dataset. The parentheses trigger the call, and the result is the dataset.
  • from sklearn.datasets import load_iris finds the function in the datasets module
  • load_iris() with parentheses runs the function
  • iris_dataset stores the returned Bunch object
sklearn.datasetsmodule shipped with thelibraryload_iris()called with parenthesesBunch objectreturned valueiris_datasetvariable name in yourprogramprovidesreturnsassigned to
How does the call to load_iris move data from the scikit-learn package into a Bunch object in my program?

Where Loading and Cleaning Go Wrong

  • Common mistake

    Dropping the parentheses from load_iris

    What it looks like
    iris_dataset = load_iris
    Why it fails

    Without parentheses, the name refers to the function itself and the call never runs, so no dataset is produced.

    Do this instead
    iris_dataset = load_iris()
  • Common mistake

    Calling replace but not assigning the result

    What it looks like
    review.replace(b'<br />', b' ')  followed by using review
    Why it fails

    replace returns a new value rather than changing the original, so the cleaned text is discarded and review still contains the tags.

    Do this instead
    cleaned = review.replace(b'<br />', b' ')
  • Common mistake

    Using a str pattern on bytes data

    What it looks like
    review.replace('<br />', ' ') when review is bytes
    Why it fails

    On Python 3 the text entries are bytes, so the search pattern and replacement must also be bytes; the mismatch causes an error.

    Do this instead

    Write the pattern and replacement with the b prefix: b'<br />' and b' '

  • Common mistake

    Assuming the Iris dataset must be downloaded first, when scikit-learn already includes it in its datasets module

When a result is not what you expected, trace the line where the value is created or stored. For a loading problem, that is usually the line with the call and its parentheses. For a cleanup problem, that is the line where replace is called and its return value is stored.

Practice Tracing the Mistake

EASY

A colleague writes the following two lines and reports that iris_dataset does not hold the data. Which line is the first place where the code's actual behavior differs from what was expected, and why?

from sklearn.datasets import load_iris
iris_dataset = load_iris
Hints
  • Compare what the second line stores with what load_iris() would store.
  • The parentheses are what make the call happen.
MEDIUM

Given review = text_train[1], write one line that produces a cleaned version of review without changing review, then state which line you would check first if the cleaned value still shows the break tags.

Hints
  • Store the replace result in a new name.
  • Check the line where replace is called and whether its result is assigned.

Key Takeaways

  • Preprocessing removes unwanted formatting such as HTML line-break tags from review text before a sentiment model uses it.
  • bytes replace returns a new value, so its result must be assigned, and its pattern and replacement must match the bytes type of the entries on Python 3.
  • load_iris is imported from sklearn.datasets and must be called with parentheses; the returned Bunch object holds keys and values, much like a dictionary.
  • When something goes wrong, trace the line where the value is created or stored and compare what it does with what was expected.

Related concepts

More concepts starting with L