编程设计一个基于条件风险最小的Bayes分类器:
要求:
- 混淆矩阵维度可任意设定
- 先验概率基于训练样本集自动求得
- 样本属性数量可任意输入设定
朴素贝叶斯求条件风险最小公式:
训练数据集:
代码:
import numpy as np
import pandas as pd
XY = pd.read_excel('d1.xls')
dset = np.array(XY)
def calc_ent(dset):
r = np.shape(dset)[0]
c = np.shape(dset)[1]
l_count = {}
for i in range(r):
label = dset[i][c-1]
if label not in l_count:
l_count[label] = 0
l_count[label] += 1
for i in l_count.keys():
l_count[i] = l_count[i]/r
return l_count
def pXy(dset, X, y):
row = np.shape(dset)[0] # 行数
clm = np.shape(dset)[1] # 列数
dlect = np.empty([0, clm], dtype=type(dset))
for i in range(row):
if (dset[i][clm - 1] == y):
dlect = np.append(dlect, [dset[i]], axis=0)
PXy = 1
num = 0
n = len(dlect)
for j in range(clm - 1):
for i in range(n):
if (isinstance(dlect[i][j], str)):
if (dlect[i][j] == X[j]):
num += 1
else:
s = str(dlect[i][j])
if (s == X[j]):
num += 1
PXy = PXy * (num / n)
# print(f"{X[j]}num的值为{num}")
num = 0
return PXy
X = np.array(['大' ,'红','圆'])
py = calc_ent(dset)
bn = len(py)
hx = np.empty([bn,bn], dtype = int)
for j in range(bn):
for i in range(bn):
if(i == j):
hx[j][i] = 0
else:
hx[j][i] = 1
R_y_X = 0
j = 0
for i in range(bn):
for key in py.keys():
if(j == i):
keyi = key
R_y_X += hx[i][j] * py[key] * pXy(dset, X, key)
j += 1
if(i == 0):
min_RyX = R_y_X
min_key = keyi
else:
if(R_y_X < min_RyX):
min_RyX = R_y_X
min_key = keyi
print(f"{X}被分为{keyi}的条件风险是:{R_y_X}")
R_y_X = 0
j = 0
print("\n")
print(f"所以{X}被分为{min_key}的条件风险最小,为{min_RyX}")