Start learning free

LESSON 106 OF 165 · MACHINE LEARNING: TEXT

Naive Bayes

Practice

A Naive Bayes model sorts texts by counting words. While it trains, it learns, for each topic, how often each word shows up in that topic's texts. For a new text it multiplies, topic by topic, the shares of the text's words and how common the topic is, and the biggest result wins. It treats every word as if it had nothing to do with the others: that shortcut is the "naive" part, and it keeps the model fast, even with little data.

MultinomialNB() from sklearn.naive_bayes is the Naive Bayes made for word counts, so it goes after a CountVectorizer(). It adds 1 to every word count, so a word never seen in a topic makes that topic unlikely, not impossible. Its predict_proba(texts) gives back one row per text with one share per topic, and the shares in a row add up to 1. The topics are in model.classes_, sorted A to Z, in the same order as the shares.

spam_check = make_pipeline(CountVectorizer(), MultinomialNB())
spam_check.fit(["win a free prize", "lunch at noon?", "free prize inside", "see you at lunch"],
               ["spam", "ok", "spam", "ok"])
print(spam_check.classes_)                       # ['ok' 'spam']
print(spam_check.predict_proba(["free lunch"]))  # one row: a share for ok, then one for spam

classification_report(true, predicted) from sklearn.metrics gives back a text table with one row per topic. Precision: of the texts the model gave a topic, the share that really have it. Recall: of the texts that really have a topic, the share the model found. The f1-score mixes the two into one number, and support is how many test texts really have the topic.

This lesson's script prints:

   message_id                                      text    topic
0           1        please cancel and refund my order?   refund
1           2  Hi, I returned it two weeks ago? Thanks.   refund
2           3  Hey, is the stapler metal? It is urgent.  product
              precision    recall  f1-score   support

     account       0.77      1.00      0.87        10
    delivery       1.00      0.64      0.78        11
     product       1.00      1.00      1.00        13
      refund       0.92      1.00      0.96        11

    accuracy                           0.91        45
   macro avg       0.92      0.91      0.90        45
weighted avg       0.93      0.91      0.91        45

Message: Hi, the reset link does not work
account     0.740
delivery    0.240
product     0.018
refund      0.002

Your task: the starting code reads messages.csv (180 support messages in four topics), prints its first three rows, splits it into training and test messages with the topics in the same mix, and sets a new message. Join a CountVectorizer and a MultinomialNB into model, fit it on the training messages and print the classification report for the test messages. Then keep the new message's shares in shares and print them next to their topics, rounded to 3 decimals.


Assignment
Follow the Coding Tutorial to sort the support messages into four topics with Naive Bayes, grade it topic by topic and read the share it gives each topic for one new message.


Hint
predict_proba wants a list of texts, even for one message, and gives back a table: [0] takes its first row. pd.Series(values, index=labels) puts each share next to its topic.


STEP 1 OF 5
    Ctrl + Enter to check