$$P(y│X)=\frac{P(X│y)P(y)}{P(X)}, X = (x_1, x_2, x_3, …, x_n)$$
CategoricalNB implements the categorical naive Bayes algorithm for categorically distributed data. It assumes that each feature, which is described by the index i, has its own categorical distribution.
For each feature i in the training set X, CategoricalNB estimates a categorical distribution for each feature i of X conditioned on the class y. The index set of the samples is defined as J = {1, 2, ..., m}, with m as the number of samples.
The probability of category t in feature i given class c is estimated as:
$$P(x_i=t|y=c;\alpha)=\frac{N_{tic} + \alpha}{N_c+\alpha n_i}$$
where Ntic=j ∈ J|xij=t, yj=c}| is the number of times category t appears in the samples xi, which belong to class c
Nc = |{j ∈ J|yj=c}| is the number of samples with class c
α is a smoothing parameter
ni is the number of available categories of feature i.
| Color | Size | Shape | Classify |
|---|---|---|---|
| Aqua | Small | Twisted | 1 |
| Blue | Small | Pointed | 0 |
| Dune | Large | Rounded | 0 |
| Dune | Small | Rounded | 1 |
| Cyan | Large | Rounded | 0 |
| Aqua | Small | Rounded | 1 |
| Aqua | Small | Rounded | 0 |
| Cyan | Small | Pointed | 1 |
| Cyan | Small | Pointed | 1 |
| Dune | Small | Rounded | 1 |
| Dune | Small | Rounded | 0 |
| Dune | Small | Rounded | 1 |
| Dune | Small | Rounded | 1 |
| Cyan | Small | Pointed | 1 |
| Dune | Small | Rounded | 1 |
| Dune | Large | Rounded | 0 |
| Cyan | Small | Twisted | 1 |
| Blue | Small | Rounded | 0 |
| Aqua | Small | Pointed | 1 |
| Aqua | Small | Pointed | 1 |
| Dune | Small | Twisted | 0 |
| Blue | Small | Rounded | 0 |
| Dune | Small | Rounded | 0 |
| Blue | Small | Twisted | 0 |
| Dune | Small | Rounded | 0 |
| Aqua | Large | Pointed | 1 |
| Dune | Large | Rounded | 0 |
| Dune | Small | Rounded | 0 |
| Dune | Small | Rounded | 0 |
| Cyan | Large | Rounded | 0 |
| Dune | Small | Twisted | 0 |
| Dune | Large | Twisted | 0 |
| Dune | Small | Rounded | 0 |
| Dune | Small | Rounded | 0 |
| Dune | Large | Rounded | 0 |
| Aqua | Large | Rounded | 1 |
| Aqua | Small | Rounded | 0 |
| Aqua | Small | Rounded | 1 |
| Dune | Small | Rounded | 0 |
| Blue | Small | Rounded | 0 |
Aqua,Small,Twisted,1 Blue,Small,Pointed,0 Dune,Large,Rounded,0 Dune,Small,Rounded,1 Cyan,Large,Rounded,0 Aqua,Small,Rounded,1 Aqua,Small,Rounded,0 Cyan,Small,Pointed,1 Cyan,Small,Pointed,1 Dune,Small,Rounded,1 Dune,Small,Rounded,0 Dune,Small,Rounded,1 Dune,Small,Rounded,1 Cyan,Small,Pointed,1 Dune,Small,Rounded,1 Dune,Large,Rounded,0 Cyan,Small,Twisted,1 Blue,Small,Rounded,0 Aqua,Small,Pointed,1 Aqua,Small,Pointed,1 Dune,Small,Twisted,0 Blue,Small,Rounded,0 Dune,Small,Rounded,0 Blue,Small,Twisted,0 Dune,Small,Rounded,0 Aqua,Large,Pointed,1 Dune,Large,Rounded,0 Dune,Small,Rounded,0 Dune,Small,Rounded,0 Cyan,Large,Rounded,0 Dune,Small,Twisted,0 Dune,Large,Twisted,0 Dune,Small,Rounded,0 Dune,Small,Rounded,0 Dune,Large,Rounded,0 Aqua,Large,Rounded,1 Aqua,Small,Rounded,0 Aqua,Small,Rounded,1 Dune,Small,Rounded,0 Blue,Small,Rounded,0
Dự đoán dòng dữ liệu (Cyan, Small, Twisted) thuộc lớp (0 hay 1)
| 0 | 1 | P(0) | P(1) |
| 24 | 16 | 24/40 | 16/40 |
| 0 | 1 | P(0) | P(1) | |
|---|---|---|---|---|
| Aqua | 2 | 7 | 2/24 | 7/16 |
| Blue | 5 | 0 | 5/24 | 0/16 |
| Dune | 15 | 5 | 15/24 | 5/16 |
| Cyan | 2 | 4 | 2/24 | 4/16 |
| Tổng | 24 | 16 | 100% | 100% |
| 0 | 1 | P(0) | P(1) | |
|---|---|---|---|---|
| Small | 17 | 14 | 17/24 | 14/16 |
| Large | 7 | 2 | 7/24 | 2/16 |
| Tổng | 24 | 16 | 100% | 100% |
| 0 | 1 | P(0) | P(1) | |
|---|---|---|---|---|
| Pointed | 1 | 6 | 1/24 | 6/16 |
| Rounded | 19 | 8 | 19/24 | 8/16 |
| Twisted | 4 | 2 | 4/24 | 2/16 |
| Tổng | 24 | 16 | 100% | 100% |
$$P(0│Cyan, Small, Twisted)=P(Cyan│0)*P(Small│0)*P(Twisted│0)*P(0)=2/24*17/24*4/24*24/40= 0.005903$$
$$P(1│Cyan, Small, Twisted)=P(Cyan│1)*P(Small│1)*P(Twisted│1)*P(1)=4/16*14/16*2/16*16/40 = 0.010938$$
$$P(0│Cyan, Small, Twisted)=\frac{0.005903}{0.005903+0.010938}=0.350515$$
$$P(1│Cyan, Small, Twisted)=\frac{0.010938}{0.005903+0.010938}=0.649485$$
Nếu P(1|Cyan, Small, Twisted) > P(0|Cyan, Small, Twisted) nên (Cyan, Small, Twisted) thuộc lớp 1 và ngược lại
$$P(x_i|y=c;\alpha)=\frac{N_{tic}+\alpha}{N_c+\alpha n_i}$$
| 0 | 1 | P(0) | P(1) | |
|---|---|---|---|---|
| Aqua | 2 | 7 | $$\frac{2+\alpha}{24+4*\alpha}$$ | $$\frac{7+\alpha}{16+4*\alpha}$$ |
| Blue | 5 | 0 | $$\frac{5+\alpha}{24+4*\alpha}$$ | $$\frac{0+\alpha}{16+4*\alpha}$$ |
| Dune | 15 | 5 | $$\frac{15+\alpha}{24+4*\alpha}$$ | $$\frac{5+\alpha}{16+4*\alpha}$$ |
| Cyan | 2 | 4 | $$\frac{2+\alpha}{24+4*\alpha}$$ | $$\frac{4+\alpha}{16+4*\alpha}$$ |
| Tổng | 24 | 16 | 100% | 100% |