统计学习方法学习笔记3——朴素贝叶斯模型

朴素贝叶斯属于:概率模型、参数化模型、和生成模型


目录

1.朴素贝叶斯基本方法

2.后验概率最大化的含义

3.朴素贝叶斯算法:

朴素贝叶斯python实现4.1:

朴素贝叶斯sklearn实现作业4.1

贝叶斯的优缺点:


1.朴素贝叶斯基本方法

2.后验概率最大化的含义

3.朴素贝叶斯算法:

朴素贝叶斯python实现4.1:

class Navie_Bayes():
    def __init__(self, x_train, y_train, x_test, y_test=None):
        self.x, self.y, self.xs, self.ys = x_train, y_train, x_test, y_test 
    
    def fit(self):
        a, b = self.x.shape  
        tmp_x_0, tmp_y_0, tmp_x_1, tmp_y_1 = [], [], [], []
        for i in range(a):
            if self.y[i] == 1:
                tmp_x_0.append(self.x[i])
                tmp_y_0.append(self.y[i])
            else:
                tmp_x_1.append(self.x[i])
                tmp_y_1.append(self.y[i])
                
        # 求先验概率
        p_ck1 = len(tmp_y_0) / a
        p_ck0 = 1 - p_ck1 
        
        # 求y=1条件概率
        tmp_1, tmp_2, tmp_3 = [], [], []
        tmp_S, tmp_M, tmp_L = [], [], []
        for j in range(len(tmp_y_0)): 
            if int(tmp_x_0[j][0]) == 1:
                tmp_1.append(tmp_x_0[j])
            elif int(tmp_x_0[j][0]) == 2:
                tmp_2.append(tmp_x_0[j])
            else:
                tmp_3.append(tmp_x_0[j])
        for v in range(len(tmp_y_0)): 
            if tmp_x_0[v][1] == 'S':
                tmp_S.append(tmp_x_0[v])
            elif tmp_x_0[v][1] == 'M':
                tmp_M.append(tmp_x_0[v])
            else:
                tmp_L.append(tmp_x_0[v])
        # 求y=-1条件概率
        tmp_1_, tmp_2_, tmp_3_ = [], [], []
        tmp_S_, tmp_M_, tmp_L_ = [], [], []
        for j_ in range(a-len(tmp_y_0)): 
            if int(tmp_x_1[j_][0]) == 1:
                tmp_1_.append(tmp_x_1[j_])
            elif int(tmp_x_1[j_][0]) == 2:
                tmp_2_.append(tmp_x_1[j_])
            else:
                tmp_3_.append(tmp_x_1[j_])
        for v_ in range(a-len(tmp_y_0)): 
            if tmp_x_1[v_][1] == 'S':
                tmp_S_.append(tmp_x_1[v_])
            elif tmp_x_1[v_][1] == 'M':
                tmp_M_.append(tmp_x_1[v_])
            else:
                tmp_L_.append(tmp_x_1[v_])
        # 后验概率
        p_y1 = p_ck1 * (len(tmp_2)/len(tmp_y_0)) * (len(tmp_S)/len(tmp_y_0))
        p_y2 = p_ck0 * (len(tmp_2_)/(a-len(tmp_y_0))) * (len(tmp_S_)/(a-len(tmp_y_0)))
        if p_y1 > p_y2:
            print('预测结果为:',1)
            return 1
        else:
            print('预测结果为:',-1)
            return -1
def main():
    x_train = np.array([[1, 'S'], [1, 'M'], [1, 'M'], [1, 'S'], [1, 'S'], [2, 'S'], [2, 'M'], [2, 'M'],
                       [2, 'L'], [2, 'L'], [3, 'L'], [3, 'M'], [3, 'M'], [3, 'L'], [3, 'L']])
    y_train = np.array([-1, -1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, -1])
    x_test = np.array([2, 'S'])
    navie_bayes = Navie_Bayes(x_train, y_train, x_test)
    navie_bayes.fit()

结果:

朴素贝叶斯sklearn实现作业4.1

# 令s=1,m=2, l=3
x_train = np.array([[1, 1], [1, 2], [1, 2], [1, 1], [1, 1], [2, 1], [2, 2], [2, 2],
                       [2, 3], [2, 3], [3, 3], [3, 2], [3, 2], [3, 3], [3, 3]])
y_train = np.array([-1, -1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, -1])
x_test = np.array([2, 2])
navie_bayes_gauss = GaussianNB()
navie_bayes_multi = MultinomialNB(alpha=2.0, class_prior=None, fit_prior=True)
navie_bayes_bou = BernoulliNB(alpha=2.0,binarize = 3.0,fit_prior=True)
tt = [navie_bayes_gauss, navie_bayes_multi, navie_bayes_bou]
tt_str = ['GaussianNB', 'BernoulliNB','MultinomialNB']
for items, name in zip(tt, tt_str):
    items.fit(x_train, y_train)
    xx = items.predict_proba(x_test.reshape(1, -1))
    print('%s 模型的预测概率为:'%name, xx)

贝叶斯的优缺点:

由于贝叶斯是对条件概率分布的条件独立性假设,那么在数据集比较小的时候它会有比较好的效果,但是当数据集比较大的时候,误差会随意数据集的增大而增大,这个时候它的效果可能还不如逻辑回归等模型。

在小型数据集上,朴素贝叶斯更容易产生偏差,这可以防止其拟合噪声。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值