“This is the 24th day of my participation in the Gwen Challenge.

Bayes classification is one of the most widely used classification algorithms in machine learning. Naive Bayes is one of the simplest Bayesian models, and its algorithm core is bayes formula as shown below.

P (A) is the probability of event A occurs, P (B) is the probability of event B occurs, P (A | B) said the event B occurs under the condition of the probability of event A occurs, the same P (B | A) represents the event A occurs under the condition of the probability of event B occurs.

A simple example: the known one cold winter the probability of events (A) P (A) is 40%, the probability of events (B) A person sneeze P (B) is 80%, when A person cold sneezes probability P (B | A) is 100%, so if A man start sneezing, he caught A cold the probability P (A | B) for how much? The solution is as follows.

P (A | B) = (100% * 40%) = 50% / 80%

Bayesian model with one-dimensional characteristic variables

How to tell if someone has a cold. Assume that there are five groups of sample data, as shown in the following table.

For the convenience of demonstration, only one characteristic variable “sneezing (X1)” is selected here, whose value is 1, indicating sneezing, and 0, indicating non-sneezing. The target variable is “cold (Y)”, with a value of 1 indicating a cold and 0 indicating no cold.

Now we’re going to use the bayesian formula to predict whether someone has a cold or not. For example, if a person sneezes (X1 = 1), does he or she have a cold? The problem is, in fact, to predict his cold probability P (Y | X1). By substituting characteristic variables and target variables into Bayesian formula, the calculation formula shown below can be obtained.

P(Y|X1)=(P(X1|Y)*P(Y))/P(X1)

From the above data, you can calculate the probability of catching a cold (Y = 1) in the case of sneezing (X1 = 1)

P(Y=1|X1=1)=3/4

From the above data, you can calculate the probability of catching a cold (Y = 1) in the case of sneezing (X1 = 1)

P(Y=0|X1=1)=1/4

And since 3/4 is greater than 1/4, the probability of catching a cold when you sneeze is higher than the probability of not catching a cold. From the above data, you can calculate the probability of catching a cold (Y = 1) in the case of sneezing (X1 = 1)

Bayesian model with two dimensional characteristic variables

Now add another characteristic variable — headache (X2), with a value of 1 for headache and 0 for no headache; The target variable remains cold (Y). Sample data are shown in the table below.

Based on the above data, we still use Bayesian formula to predict whether a person has a cold or not. For example, if a person sneezes and has a headache (X1 = 1, X2 = 1), does he have a cold? The problem is, in fact, to predict his cold probability P (Y | X1, X2). By substituting characteristic variables and target variables into Bayesian formula, the calculation formula shown below can be obtained.

Now to compute and compare the P (Y | = 1 X1, X2) and P (Y = 0 | X1, X2), the size of the known by the above formula, the denominator of both P (X1, X2) is the same, so directly calculate and compare the two molecules P (X1, X2 | Y), P (Y) the size of the can.

Bayesian model with n-dimensional characteristic variables

We can generalize Bayesian formula to n characteristic variables X1, X2… on the basis of 2 characteristic variables. Xn, the formula is as follows.