Start learning free

LESSON 104 OF 165 · MACHINE LEARNING: TEXT

The Words That Decide in Python

Practice

Your review classifier works, but which words make it decide? A LogisticRegression learns one number for every column, its weight. After a TfidfVectorizer, every column is one word, so every word gets a weight. A weight above 0 pushes a review toward the second class in model.classes_, here 'positive'; a weight below 0 pushes it toward 'negative'. The further from 0, the stronger the push.

Where the numbers are: you can index a pipeline like a list. model[0] is its first step, the fitted vectorizer: get_feature_names_out() gives back its words in column order. model[-1] is its last step, the LogisticRegression: its coef_ holds one row of weights (two classes need only one), so coef_[0] gives one weight per column, in that same order. pd.Series(values, index=labels) pairs each value with a label, and sort_values() gives back the Series ordered from the lowest value to the highest:

toy = pd.Series([0.9, -1.3, 0.1], index=["great", "late", "the"])
print(toy.sort_values().to_string())
late    -1.3
the      0.1
great    0.9

On a sorted Series, head(5) gives back the first 5 rows, the lowest values, and tail(5) the last 5, with the highest at the very end. To look at one word, reviews["text"].str.contains("love") gives back True or False for each review. Keep the True rows, and value_counts().to_dict() on a column gives back a dict of each value and how many times it appears.

Your code must print:

Classes: ['negative' 'positive']
Pull toward negative:
not            -1.892
of             -1.251
refund         -1.187
disappointed   -1.148
broken         -1.131
Pull toward positive:
exactly      1.090
beautiful    1.134
love         1.150
best         1.226
well         1.379
Reviews with refund: {'negative': 12, 'positive': 1}

Your task: the starting code reads reviews.csv, trains the pipeline on all 240 reviews and prints the classes and two headings. Pair each word with its weight, print the 5 words that pull hardest toward each side, then find out why refund, which is not a feeling at all, pulls toward negative: count the sentiments of the reviews that contain it.


Assignment
Follow the Coding Tutorial to pair the vocabulary with the model's weights, print the strongest words on each side, and count the sentiments of the reviews that mention refund.


Hint
model[0] has the words and model[-1].coef_[0] the weights, in the same column order. After sort_values(), the words that pull most toward negative are at the head and those that pull most toward positive are at the tail.


STEP 1 OF 6
    Ctrl + Enter to check

    Txt Telling Words in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free