Python Machine Learning

Naive Bayes Classifiers

Xác suất điều kiện (Bayes)

$$P(y│X)=\frac{P(X│y)P(y)}{P(X)}, X = (x_1, x_2, x_3, …, x_n)$$

Categorical Naive Bayes

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.

Cho Dataset

ColorSizeShapeClassify
AquaSmallTwisted1
BlueSmallPointed0
DuneLargeRounded0
DuneSmallRounded1
CyanLargeRounded0
AquaSmallRounded1
AquaSmallRounded0
CyanSmallPointed1
CyanSmallPointed1
DuneSmallRounded1
DuneSmallRounded0
DuneSmallRounded1
DuneSmallRounded1
CyanSmallPointed1
DuneSmallRounded1
DuneLargeRounded0
CyanSmallTwisted1
BlueSmallRounded0
AquaSmallPointed1
AquaSmallPointed1
DuneSmallTwisted0
BlueSmallRounded0
DuneSmallRounded0
BlueSmallTwisted0
DuneSmallRounded0
AquaLargePointed1
DuneLargeRounded0
DuneSmallRounded0
DuneSmallRounded0
CyanLargeRounded0
DuneSmallTwisted0
DuneLargeTwisted0
DuneSmallRounded0
DuneSmallRounded0
DuneLargeRounded0
AquaLargeRounded1
AquaSmallRounded0
AquaSmallRounded1
DuneSmallRounded0
BlueSmallRounded0

Data Raw

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)

Yêu cầu

Tạo thống kê theo yêu cầu sau

Classify
01P(0)P(1)
241624/4016/40
Color
01P(0)P(1)
Aqua272/247/16
Blue505/240/16
Dune15515/245/16
Cyan242/244/16
Tổng2416100%100%
Size
01P(0)P(1)
Small171417/2414/16
Large727/242/16
Tổng2416100%100%
Shape
01P(0)P(1)
Pointed161/246/16
Rounded19819/248/16
Twisted424/242/16
Tổng2416100%100%

Dự đoán dòng dữ liệu (Cyan, Small, Twisted) thuộc lớp (0 hay 1)

Bước tính xác suất điều kiện

$$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$$

Bước chuẩn hóa

$$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$$

Phân lớp

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

Làm trơn Laplace

$$P(x_i|y=c;\alpha)=\frac{N_{tic}+\alpha}{N_c+\alpha n_i}$$

Color
01P(0)P(1)
Aqua27$$\frac{2+\alpha}{24+4*\alpha}$$$$\frac{7+\alpha}{16+4*\alpha}$$
Blue50$$\frac{5+\alpha}{24+4*\alpha}$$$$\frac{0+\alpha}{16+4*\alpha}$$
Dune155$$\frac{15+\alpha}{24+4*\alpha}$$$$\frac{5+\alpha}{16+4*\alpha}$$
Cyan24$$\frac{2+\alpha}{24+4*\alpha}$$$$\frac{4+\alpha}{16+4*\alpha}$$
Tổng2416100%100%