Notas de estudio del algoritmo Naive Bayes
Este artículo es solo para estudio y comprensión personal.
Clasificador Naive Bayes (es un método de clasificación)
fórmula de Bayes
PAGS ( UN ∣ segundo ) = PAGS ( UN , segundo ) PAGS ( segundo ) = PAGS ( segundo ∣ UN ) PAGS ( UN ) PAGS ( segundo ) PAGS(A \mid B)=\frac{P(A,B) } {P(B)}=\frac{P(B \mid A) P(A)}{P(B)}PAG ( UN∣segundo )=PAG ( B )PAG ( Un ,b )=PAG ( B )PAG ( segundo∣A ) P ( A )
donde:
P ( A ) P(A)P ( A ) :probabilidad previa
P ( A ∣ B ) P(A \mid B)PAG ( UN∣B ) :probabilidad posterior
P ( B ∣ A ) P(B \mid A)PAG ( segundo∣A ) : La probabilidad de que B ocurra bajo la condición de que ocurra el evento A, es decir,la función de verosimilitud
P ( B ) P(B)P ( B ) : lo mismo para todas las etiquetas de clase, por lo que el factor de evidenciaP ( B ) P(B)PAGS ( segundo )与类标记无关
PAGS ( UN yo ∣ segundo ) = PAGS ( segundo ∣ UN yo ) PAGS ( UN yo ) ∑ j PAGS ( segundo ∣ UN j ) PAGS ( UN j ) PAGS\left(A_{i} \mid B\right)=\frac{P\left(B \mid A_{i}\right) P\left(A_{i}\right)}{\sum_{j} P\left(B \mid A_ {j}\derecha) P\izquierda(A_{j}\derecha)}PAG( Unyo∣segundo )=∑jPAG( B∣Aj)PAG( Unj)PAG( B∣Ayo)PAG( Unyo)
problema de clasificación
Algoritmo bayesiano ingenuo (con explicación de ejemplo)
La fórmula bayesiana se puede expresar como:
P ( y = cn ∣ x = X ) = P ( x = X , y = cn ) P ( x = X ) = P ( x = X ∣ y = cn ) PAGS ( y = cn ) PAGS ( x = X ) PAGS\left(y=c_{n} \mid x=X\right)=\frac{P\left(x=X, y =c_{ n}\right)}{P(x=X)}=\frac{P\left(x=X \mid y=c_{n}\right) P\left(y=c_{n}\ right)} {P(x=X)}PAG( y=Cn∣X=x )=pag ( x=x )PAG( X=x ,y=Cn)=pag ( x=x )PAG( X=X∣y=Cn)PAG( y=Cn)
Suponiendo que cada atributo es independiente entre sí:
P ( x = X ∣ y = cn ) = ∏ i = 1 m P ( xi = ai ∣ y = cn ) P\left(x=X \mid y=c_{n }\right )=\prod_{i=1}^{m} P\left(x^{i}=a_{i} \mid y=c_{n}\right)PAG( X=X∣y=Cn)=yo = 1∏mPAG( Xi=ayo∣y=Cn)
Fórmula bayesiana ingenua: en el conjunto de funcionesxxBajo la condición de x , yyy取不同值的概率。
PAGS ( y = cn ∣ x = X ) = PAGS ( y = cn ) ∏ i = 1 metro PAGS ( xi = ai ∣ y = cn ) PAGS ( x = X ) PAGS\left(y =c_{n} \mid x=X \right)=\frac{P\left(y=c_{n}\right) \prod_{i=1}^{m} P\left(x^{i} =a_{i} \mid y=c_{n}\right)}{P(x=X)}PAG( y=Cn∣X=x )=pag ( x=x )PAG( y=Cn)∏yo = 1mPAG( Xi=ayo∣y=Cn)
maximizará la probabilidad condicional yyy作为预测结果:
f ( x ) = argmax ( P ( y = cn ) ∏ cnm P ( xi = ai ∣ y = cn ) P ( x = X ) ) f(x)=\operatorname{argmax}\left (\frac{P\left(y=c_{n}\right) \prod_{c_{n}}^{m} P\left(x^{i}=a_{i} \mid y=c_{n }\derecho)}{P(x=X)}\derecho)f ( x )=argmax(pag ( x=x )PAG( y=Cn)∏CnmPAG( Xi=ayo∣y=Cn))
(el factor de evidencia es independiente de la etiqueta de clase) es decir,p ( x = X ) p(x=X)pag ( x=X )可以省略
f ( x ) = arg max cn ( PAGS ( y = cn ) ∏ yo = 1 metro PAGS ( xi = ai ∣ y = cn ) ) f(x)=\arg \max _{c_{ n}}\left(P\left(y=c_{n}\right) \prod_{i=1}^{m} P\left(x^{i}=a_{i} \mid y=c_{ n}\derecho)\derecho)f ( x )=ar gCnmáximo( PAG( y=Cn)yo = 1∏mPAG( Xi=ayo∣y=Cn) )
PAGS ( y = cn ) = ∑ yo = 1 NI ( y = cn ) norte , norte = 1 , 2 , ... KP\left(y=c_{n}\right)=\frac{\sum_{i= 1}^{N} I\left(y=c_{n}\right)}{N}, n=1,2, \ldots KPAG( y=Cn)=norte∑yo = 1norteI( y=Cn),norte=1 ,2 ,…K
PAGS ( xi = aj ∣ y = cn ) = ∑ yo = 1 NI ( xij = aj ∣ y = cn ) ∑ yo = 1 NI ( yi = cn ) P\left(x^{i}=a_{j} \mid y=c_{n}\right)=\frac{\sum_{i=1}^{N} I\left(x_{i}^{j}=a_{j} \mid y=c_{n }\right)}{\sum_{i=1}^{N} I\left(y_{i}=c_{n}\right)}PAG( Xi=aj∣y=Cn)=∑yo = 1norteI( yyo=Cn)∑yo = 1norteI( Xij=aj∣y=Cn)
Ejemplo de cálculo 1
Implementación de ejemplo de código del algoritmo bayesiano ingenuo (python)
precio A | Lección B | Ventas C | precio A | Lección B | Ventas C |
---|---|---|---|---|---|
Bajo | muchos | alto | 0 | 2 | 2 |
alto | medio | alto | 2 | 1 | 2 |
Bajo | pocos | alto | 0 | 0 | 2 |
Bajo | medio | Bajo | 0 | 1 | 0 |
medio | medio | medio | 1 | 1 | 1 |
alto | muchos | alto | 2 | 2 | 2 |
Bajo | pocos | medio | 0 | 0 | 1 |
Precio previsto A=2 (alta) horas de clase B=2 (más) horas de ventas
from __future__ import division
from numpy import array
def set_data(price, time, sale):
price_number = []
time_number = []
sale_number = []
for i in price:
if i == "低":
price_number.append(0)
elif i == "中":
price_number.append(1)
elif i == "高":
price_number.append(2)
for j in time:
if j == "少":
time_number.append(0)
elif j == "中":
time_number.append(1)
elif j == "多":
time_number.append(2)
for k in sale:
if k == "低":
sale_number.append(0)
elif k == "中":
sale_number.append(1)
elif k == "高":
sale_number.append(2)
return price_number, time_number, sale_number
price = ["低", "高", "低", "低", "中", "高", "低"]
time = ["多", "中", "少", "中", "中", "多", "少"]
sale = ["高", "高", "高", "低", "中", "高", "中"]
price_number, time_number, sale_number = set_data(price, time, sale)
print(price_number, time_number, sale_number)
PAGS ( C = 0 ∣ x = X ) ∝ PAGS ( C = 0 ) PAGS ( UN = 2 ∣ C = 0 ) PAGS ( segundo = 2 ∣ C = 0 ) PAGS(C=0\mid x=X) \propto P(C=0)P(A=2 \mid C=0)P(B=2 \mid C=0)P ( C=0∣X=x )∝P ( C=0 ) P ( UN=2∣C=0 ) P ( B=2∣C=0 )
PAGS ( C = 1 ∣ x = X ) ∝ PAGS ( C = 1 ) PAGS ( A = 2 ∣ C = 1 ) PAGS ( segundo = 2 ∣ C = 1 ) PAGS(C=1\mid x=X) \proto P(C=1)P(A=2 \mid C=1)P(B=2 \mid C=1)P ( C=1∣X=x )∝P ( C=1 ) P ( UN=2∣C=1 ) P ( B=2∣C=1 )
PAGS ( C = 2 ∣ x = X ) ∝ PAGS ( C = 2 ) PAGS ( UN = 2 ∣ C = 2 ) PAGS ( segundo = 2 ∣ C = 2 ) PAGS(C=2\mid x=X) \proto P(C=2)P(A=2 \mid C=2)P(B=2 \mid C=2)P ( C=2∣X=x )∝P ( C=2 ) P ( UN=2∣C=2 ) P ( B=2∣C=2 )
from __future__ import division
price_number = [0, 2, 0, 0, 1, 2, 0]
time_number = [2, 1, 0, 1, 1, 2, 0]
sale_number = [2, 2, 2, 0, 1, 2, 1]
exprice_number = 2
extime_number = 2
sale0p = sale_number.count(0)
sale1p = sale_number.count(1)
sale2p = sale_number.count(2)
a0 = 0
a1 = 0
a2 = 0
b0 = 0
b1 = 0
b2 = 0
for i in range(0, len(sale_number)):
if price_number[i] == 2:
if sale_number[i] == 0:
a0 = a0 + 1
elif sale_number[i] == 1:
a1 = a1 + 1
elif sale_number[i] == 2:
a2 = a2 + 1
if time_number[i] == 2:
if sale_number[i] == 0:
b0 = b0 + 1
elif sale_number[i] == 1:
b1 = b1 + 1
elif sale_number[i] == 2:
b2 = b2 + 1
pa0 = a0 / sale0p
pa1 = a1 / sale1p
pa2 = a2 / sale2p
pb0 = b0 / sale0p
pb1 = b1 / sale1p
pb2 = b2 / sale2p
pc0 = sale0p / len(sale_number)
pc1 = sale1p / len(sale_number)
pc2 = sale2p / len(sale_number)
pcc0 = pc0 * pa0 * pb0
pcc1 = pc1 * pa1 * pb1
pcc2 = pc2 * pa2 * pb2
indf = (pcc0, pcc1, pcc2)
print(indf)
max_indf = indf.index(max(indf))
if max_indf == 0:
print('销量低')
elif max_indf == 1:
print('销量中')
elif max_indf == 2:
print('销量高')
publicación de blog citada
Ejemplo simple del algoritmo de clasificación Naive Bayesian
Algoritmo Naive Bayesian (con explicación de ejemplo)
Implementación de código del algoritmo Naive Bayesian (python)