naive bayes classifying reviews

Solutions on MaxInterview for naive bayes classifying reviews by the best coders in the world

showing results for - "naive bayes classifying reviews"
Mario
28 Sep 2019
1# Here's a running history for the past week.
2# For each day, it contains whether or not the person ran, and whether or not they were tired.
3days = [["ran", "was tired"], ["ran", "was not tired"], ["didn't run", "was tired"], ["ran", "was tired"], ["didn't run", "was not tired"], ["ran", "was not tired"], ["ran", "was tired"]]
4
5# Let's say we want to calculate the odds that someone was tired given that they ran, using bayes' theorem.
6# This is P(A).
7prob_tired = len([d for d in days if d[1] == "was tired"]) / len(days)
8# This is P(B).
9prob_ran = len([d for d in days if d[0] == "ran"]) / len(days)
10# This is P(B|A).
11prob_ran_given_tired = len([d for d in days if d[0] == "ran" and d[1] == "was tired"]) / len([d for d in days if d[1] == "was tired"])
12
13# Now we can calculate P(A|B).
14prob_tired_given_ran = (prob_ran_given_tired * prob_tired) / prob_ran
15
16print("Probability of being tired given that you ran: {0}".format(prob_tired_given_ran))