Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Naïve Bayes Classification CS 6243 Machine Learning Modified from the slides by Dr. Raymond J. Mooney

Similar presentations


Presentation on theme: "1 Naïve Bayes Classification CS 6243 Machine Learning Modified from the slides by Dr. Raymond J. Mooney"— Presentation transcript:

1 1 Naïve Bayes Classification CS 6243 Machine Learning Modified from the slides by Dr. Raymond J. Mooney http://www.cs.utexas.edu/~mooney/cs391L/

2 2 Probability Basics Definition (informal) –Probabilities are numbers assigned to events that indicate “how likely” it is that the event will occur when a random experiment is performed –A probability law for a random experiment is a rule that assigns probabilities to the events in the experiment –The sample space S of a random experiment is the set of all possible outcomes

3 3 Probabilistic Calculus All probabilities between 0 and 1 If A, B are mutually exclusive: –P(A  B) = P(A) + P(B) Thus: P(not(A)) = P(A c ) = 1 – P(A) A B S

4 4 Conditional probability The joint probability of two events A and B P(A  B), or simply P(A, B) is the probability that event A and B occur at the same time. The conditional probability of P(A|B) is the probability that A occurs given B occurred. P(A | B) = P(A  B) / P(B) P(A  B) = P(A | B) P(B) P(A  B) = P(B|A) P(A)

5 5 Example Roll a die –If I tell you the number is less than 4 –What is the probability of an even number? P(d = even | d < 4) = P(d = even  d < 4) / P(d < 4) P(d = 2) / P(d = 1, 2, or 3) = (1/6) / (3/6) = 1/3

6 6 Independence A and B are independent iff: Therefore, if A and B are independent: These two constraints are logically equivalent

7 7 Examples Are P(d = even) and P(d < 4) independent? –P(d = even and d < 4) = 1/6 –P(d = even) = ½ –P(d < 4) = ½ –½ * ½ > 1/6 If your die actually has 8 faces, will P(d = even) and P(d < 5) be independent? Are P(even in first roll) and P(even in second roll) independent? Playing card, are the suit and rank independent?

8 8 Theorem of total probability Let B 1, B 2, …, B N be mutually exclusive events whose union equals the sample space S. We refer to these sets as a partition of S. An event A can be represented as: Since B 1, B 2, …, B N are mutually exclusive, then P(A) = P(A  B 1 ) + P(A  B 2 ) + … + P(A  B N ) And therefore P(A) = P(A|B 1 )*P(B 1 ) + P(A|B 2 )*P(B 2 ) + … + P(A|B N )*P(B N ) =  i P(A | B i ) * P(B i ) Exhaustive conditionalization Marginalization

9 9 Example A loaded die: –P(6) = 0.5 –P(1) = … = P(5) = 0.1 Prob of even number? P(even) = P(even | d < 6) * P (d<6) + P(even | d = 6) * P (d=6) = 2/5 * 0.5 + 1 * 0.5 = 0.7

10 10 Another example A box of dice: –99% fair –1% loaded P(6) = 0.5. P(1) = … = P(5) = 0.1 –Randomly pick a die and roll, P(6)? P(6) = P(6 | F) * P(F) + P(6 | L) * P(L) –1/6 * 0.99 + 0.5 * 0.01 = 0.17

11 11 Bayes theorem P(A  B) = P(B) * P(A | B) = P(A) * P(B | A) AP BP ABP )( )( )|( = => Posterior probability Prior of A (Normalizing constant) BAP)|( Prior of B Conditional probability (likelihood) This is known as Bayes Theorem or Bayes Rule, and is (one of) the most useful relations in probability and statistics Bayes Theorem is definitely the fundamental relation in Statistical Pattern Recognition

12 12 Bayes theorem (cont’d) Given B 1, B 2, …, B N, a partition of the sample space S. Suppose that event A occurs; what is the probability of event B j ? P(B j | A) = P(A | B j ) * P(B j ) / P(A) = P(A | B j ) * P(B j ) /  j P(A | B j )*P(B j ) B j : different models / hypotheses In the observation of A, should you choose a model that maximizes P(B j | A) or P(A | B j )? Depending on how much you know about B j ! Posterior probability Likelihood Prior of B j Normalizing constant (theorem of total probabilities)

13 13 Example A test for a rare disease claims that it will report positive for 99.5% of people with disease, and negative 99.9% of time for those without. The disease is present in the population at 1 in 100,000 What is P(disease | positive test)? –P(D|+) = P(+|D)P(D)/P(+) = 0.01 What is P(disease | negative test)? –P(D|-) = P(-|D)P(D)/P(-) = 5e-8

14 14 Joint Distribution The joint probability distribution for a set of random variables, X 1,…,X n gives the probability of every combination of values (an n- dimensional array with v n values if all variables are discrete with v values, all v n values must sum to 1): P(X 1,…,X n ) The probability of all possible conjunctions (assignments of values to some subset of variables) can be calculated by summing the appropriate subset of values from the joint distribution. Therefore, all conditional probabilities can also be calculated. circlesquare red0.200.02 blue0.020.01 circlesquare red0.050.30 blue0.20 positive negative

15 15 Probabilistic Classification Let Y be the random variable for the class which takes values {y 1,y 2,…y m }. Let X be the random variable describing an instance consisting of a vector of values for n features, let x k be a possible value for X and x ij a possible value for X i. For classification, we need to compute P(Y=y i | X=x k ) for i=1…m However, given no other assumptions, this requires a table giving the probability of each category for each possible instance in the instance space, which is impossible to accurately estimate from a reasonably-sized training set. –Assuming Y and all X i are binary, we need 2 n entries to specify P(Y=pos | X=x k ) for each of the 2 n possible x k ’s since P(Y=neg | X=x k ) = 1 – P(Y=pos | X=x k ) –Compared to 2 n+1 – 1 entries for the joint distribution P(Y,X 1,X 2 …X n )

16 16 Bayesian Categorization Determine category of x k by determining for each y i P(X=x k ) can be determined since categories are complete and disjoint.

17 17 Bayesian Categorization (cont.) Need to know: –Priors: P(Y=y i ) –Conditionals: P(X=x k | Y=y i ) P(Y=y i ) are easily estimated from data. –If n i of the examples in D are in y i then P(Y=y i ) = n i / |D| Too many possible instances (e.g. 2 n for binary features) to estimate all P(X=x k | Y=y i ). Need to make some sort of independence assumptions about the features to make learning tractable.

18 18 Conditional independence Chain rule: P(x 1, x 2, x 3 ) = P(x 1, x 2, x 3 ) / P(x 2, x 3 ) * P(x 2, x 3 ) / P(x 3 ) * P(x 3 ) = P(x 1 | x 2, x 3 ) P(x 2 | x 3 ) P(x 3 ) P(x 1, x 2, x 3 | Y) = P(x 1 | x 2, x 3, Y) P(x 2 | x 3, Y) P(x 3 | Y) = P(x 1 | Y) P(x 2 | Y) P(x 3, Y) assuming conditional independence

19 19 Generative Probabilistic Models Assume a simple (usually unrealistic) probabilistic method by which the data was generated. For categorization, each category has a different parameterized generative model that characterizes that category. Training: Use the data for each category to estimate the parameters of the generative model for that category. –Maximum Likelihood Estimation (MLE): Set parameters to maximize the probability that the model produced the given training data. –If M λ denotes a model with parameter values λ and D k is the training data for the kth class, find model parameters for class k (λ k ) that maximize the likelihood of D k : Testing: Use Bayesian analysis to determine the category model that most likely generated a specific test instance.

20 20 Naïve Bayes Generative Model Size Color Shape Positive Negative pos neg pos neg sm med lg med sm med lg red blue grn circ sqr tri circ sqr tri sm lg med sm lg med lg sm blue red grn blue grn red grn blue circ sqr tri circ sqr circ tri Category

21 21 Naïve Bayes Inference Problem Size Color Shape Positive Negative pos neg pos neg sm med lg med sm med lg red blue grn circ sqr tri circ sqr tri sm lg med sm lg med lg sm blue red grn blue grn red grn blue circ sqr tri circ sqr circ tri Category lg red circ ??

22 22 Naïve Bayesian Categorization If we assume features of an instance are independent given the category (conditionally independent). Therefore, we then only need to know P(X i | Y) for each possible pair of a feature-value and a category. If Y and all X i and binary, this requires specifying only 2n parameters: –P(X i =true | Y=true) and P(X i =true | Y=false) for each X i –P(X i =false | Y) = 1 – P(X i =true | Y) Compared to specifying 2 n parameters without any independence assumptions.

23 23 Naïve Bayes Example Probabilitypositivenegative P(Y)0.5 P(small | Y)0.4 P(medium | Y)0.10.2 P(large | Y)0.50.4 P(red | Y)0.90.3 P(blue | Y)0.050.3 P(green | Y)0.050.4 P(square | Y)0.050.4 P(triangle | Y)0.050.3 P(circle | Y)0.90.3 Test Instance:

24 24 Naïve Bayes Example Probabilitypositivenegative P(Y)0.5 P(medium | Y)0.10.2 P(red | Y)0.90.3 P(circle | Y)0.90.3 P(positive | X) = P(positive)*P(medium | positive)*P(red | positive)*P(circle | positive) / P(X) 0.5 * 0.1 * 0.9 * 0.9 = 0.0405 / P(X) P(negative | X) = P(negative)*P(medium | negative)*P(red | negative)*P(circle | negative) / P(X) 0.5 * 0.2 * 0.3 * 0.3 = 0.009 / P(X) P(positive | X) + P(negative | X) = 0.0405 / P(X) + 0.009 / P(X) = 1 P(X) = (0.0405 + 0.009) = 0.0495 = 0.0405 / 0.0495 = 0.8181 = 0.009 / 0.0495 = 0.1818 Test Instance:

25 25 Estimating Probabilities Normally, probabilities are estimated based on observed frequencies in the training data. If D contains n k examples in category y k, and n ijk of these n k examples have the jth value for feature X i, x ij, then: However, estimating such probabilities from small training sets is error-prone. If due only to chance, a rare feature, X i, is always false in the training data,  y k :P(X i =true | Y=y k ) = 0. If X i =true then occurs in a test example, X, the result is that  y k : P(X | Y=y k ) = 0 and  y k : P(Y=y k | X) = 0

26 26 Probability Estimation Example Probabilitypositivenegative P(Y)0.5 P(small | Y)0.5 P(medium | Y)0.0 P(large | Y)0.5 P(red | Y)1.00.5 P(blue | Y)0.00.5 P(green | Y)0.0 P(square | Y)0.0 P(triangle | Y)0.00.5 P(circle | Y)1.00.5 ExSizeColorShapeCategory 1smallredcirclepositive 2largeredcirclepositive 3smallredtrianglenegitive 4largebluecirclenegitive Test Instance X: P(positive | X) = 0.5 * 0.0 * 1.0 * 1.0 / P(X) = 0 P(negative | X) = 0.5 * 0.0 * 0.5 * 0.5 / P(X) = 0

27 27 Smoothing To account for estimation from small samples, probability estimates are adjusted or smoothed. Laplace smoothing using an m-estimate assumes that each feature is given a prior probability, p, that is assumed to have been previously observed in a “virtual” sample of size m. For binary features, p is simply assumed to be 0.5.

28 28 Laplace Smothing Example Assume training set contains 10 positive examples: –4: small –0: medium –6: large Estimate parameters as follows (if m=1, p=1/3) –P(small | positive) = (4 + 1/3) / (10 + 1) = 0.394 –P(medium | positive) = (0 + 1/3) / (10 + 1) = 0.03 –P(large | positive) = (6 + 1/3) / (10 + 1) = 0.576 –P(small or medium or large | positive) = 1.0

29 29 Missing values Training: instance is not included in frequency count for attribute value-class combination Classification: attribute will be omitted from calculation Example: OutlookTemp.HumidityWindyPlay ?CoolHighTrue? Likelihood of “yes” = 3/9  3/9  3/9  9/14 = 0.0238 Likelihood of “no” = 1/5  4/5  3/5  5/14 = 0.0343 P(“yes”) = 0.0238 / (0.0238 + 0.0343) = 41% P(“no”) = 0.0343 / (0.0238 + 0.0343) = 59% witten&eibe

30 30 Continuous Attributes If X i is a continuous feature rather than a discrete one, need another way to calculate P(X i | Y). Assume that X i has a Gaussian distribution whose mean and variance depends on Y. During training, for each combination of a continuous feature X i and a class value for Y, y k, estimate a mean, μ ik, and standard deviation σ ik based on the values of feature X i in class y k in the training data. During testing, estimate P(X i | Y=y k ) for a given example, using the Gaussian distribution defined by μ ik and σ ik.

31 31 Statistics for weather data Example density value: OutlookTemperatureHumidityWindyPlay YesNoYesNoYesNoYesNoYesNo Sunny2364, 68,65, 71,65, 70,70, 85,False6295 Overcast4069, 70,72, 80,70, 75,90, 91,True33 Rainy3272, …85, …80, …95, … Sunny2/93/5  =73  =75  =79  =86 False6/92/59/145/14 Overcast4/90/5  =6.2  =7.9  =10.2  =9.7 True3/93/5 Rainy3/92/5 witten&eibe

32 32 Classifying a new day A new day: Missing values during training are not included in calculation of mean and standard deviation OutlookTemp.HumidityWindyPlay Sunny6690true? Likelihood of “yes” = 2/9  0.0340  0.0221  3/9  9/14 = 0.000036 Likelihood of “no” = 3/5  0.0291  0.0380  3/5  5/14 = 0.000136 P(“yes”) = 0.000036 / (0.000036 + 0. 000136) = 20.9% P(“no”) = 0.000136 / (0.000036 + 0. 000136) = 79.1% witten&eibe

33 33 *Probability densities Relationship between probability and density: But: this doesn’t change calculation of a posteriori probabilities because  cancels out Exact relationship: witten&eibe

34 34 Naïve Bayes: discussion Naïve Bayes works surprisingly well (even if independence assumption is clearly violated) –Experiments show it to be quite competitive with other classification methods on standard UCI datasets. Why? Because classification doesn’t require accurate probability estimates as long as maximum probability is assigned to correct class However: adding too many redundant attributes will cause problems (e.g. identical attributes) Note also: many numeric attributes are not normally distributed (  kernel density estimators) Does not perform any search of the hypothesis space. Directly constructs a hypothesis from parameter estimates that are easily calculated from the training data. Typically handles noise well since it does not even focus on completely fitting the training data. witten&eibe

35 35 Naïve Bayes Extensions Improvements: –select best attributes (e.g. with greedy search) –often works as well or better with just a fraction of all attributes Bayesian Networks witten&eibe


Download ppt "1 Naïve Bayes Classification CS 6243 Machine Learning Modified from the slides by Dr. Raymond J. Mooney"

Similar presentations


Ads by Google