Start learning free

LESSON 103 OF 165 · MACHINE LEARNING: TEXT

Is This Review Happy?

Practice

Now that text can become numbers, you can train a model on it. A text classifier reads a review and gives back a label, here "positive" or "negative". It learns from reviews whose labels you already know: reviews.csv has 240 of them.

Join the two parts in one pipeline with make_pipeline: a TfidfVectorizer turns each text into a row of weights, and a LogisticRegression(max_iter=1000) learns from those rows (max_iter=1000 gives it enough rounds to finish). The pipeline takes raw texts everywhere: fit(texts, labels) learns the vocabulary and trains the model, score(texts, labels) gives back the accuracy (the share of right answers), and predict(texts) gives back an array with one label per text.

spam = make_pipeline(TfidfVectorizer(), LogisticRegression(max_iter=1000))
spam.fit(["win money now", "lunch at noon?", "free money", "see you at noon"], ["spam", "ok", "spam", "ok"])
print(spam.predict(["free money now", "noon lunch"]))   # an array with 2 labels

An accuracy only means something next to a baseline. About 60% of the reviews are positive, so a "model" that always answers positive is right that often. Split the rows with train_test_split(..., stratify=labels) so the test rows keep the same mix of labels as the whole file.

After the two reviews the starting code shows, your script must print:

Always positive: 0.583
Accuracy: 0.983
New reviews: ['positive' 'negative']

Your task: the starting code reads reviews.csv into texts and labels and lists two new_reviews. Split off a stratified quarter for testing with random_state=0, print the always-positive baseline, train the pipeline, then print its accuracy on the test rows and its labels for the two new reviews.


Assignment
Follow the Coding Tutorial to train a classifier that tells happy reviews from unhappy ones.


Hint
The baseline is the mean of y_test == "positive", since True counts as 1. Give the raw texts to fit, score and predict: the pipeline's vectorizer turns them into weights.


STEP 1 OF 6
    Ctrl + Enter to check