Start learning free

LESSON 101 OF 165 · MACHINE LEARNING: TEXT

Words into Counts

Practice

A model learns from numbers, but a review is text. The simplest way to turn texts into numbers is a bag of words: collect every different word the texts use, then count how many times each text uses each word. Every text becomes one row of counts, with one column per word.

scikit-learn does this with CountVectorizer, from sklearn.feature_extraction.text. Its fit_transform(texts) learns the vocabulary (every different word, lowercased, without punctuation, sorted alphabetically) and gives back the counts. get_feature_names_out() gives back the vocabulary in column order. The counts come as a sparse matrix, which stores only the numbers that aren't 0; .toarray() gives back the full table, one row per text. Later, transform(new_texts) counts new texts with the vocabulary it already learned.

vec = CountVectorizer()
table = vec.fit_transform(["fast delivery", "slow delivery, slow reply"])
print(list(vec.get_feature_names_out()))   # ['delivery', 'fast', 'reply', 'slow']
print(table.toarray())                     # [[1 1 0 0]
                                           #  [1 0 1 2]]

Notice what is lost: the counts say nothing about the order of the words. "slow reply" and "reply slow" get the same row. That is why it is called a bag: the words are poured in and counted.

The five reviews of this lesson use 12 different words. Your script must print:

Vocabulary: ['arrived', 'broken', 'cheap', 'colour', 'great', 'lamp', 'looks', 'love', 'mug', 'teapot', 'the', 'this']
[[0 0 0 0 0 0 0 1 1 0 0 1]
 [1 1 0 0 0 0 0 0 1 0 1 0]
 [0 0 0 1 0 1 0 2 0 0 2 0]
 [0 0 1 0 0 1 1 0 0 0 1 0]
 [0 0 0 0 1 0 0 0 0 1 0 0]]
mug this love: [[0 0 0 0 0 0 0 1 1 0 0 1]]

Your task: the five reviews are already in the list reviews. Make a CountVectorizer, fit it on the reviews, print the vocabulary and the full table of counts, then count the text "mug this love" with the same vocabulary.


Assignment
Follow the Coding Tutorial to turn the five reviews into a table of word counts.


Hint
Wrap get_feature_names_out() in list(...) so it prints like a Python list. transform takes a list of texts, even for a single text: ["mug this love"].


STEP 1 OF 5
    Ctrl + Enter to check