Laplace smoothing in Naïve Bayes algorithm(拉普拉斯平滑)

2023-11-18

在这里转载只是为了让不能够科学搜索的同学们看到好文章而已,个人无收益只是分享知识(顺手做个记录罢了)

原网址:https://towardsdatascience.com/laplace-smoothing-in-na%C3%AFve-bayes-algorithm-9c237a8bdece

Naïve Bayes is a probabilistic classifier based on Bayes theorem and is used for classification tasks. It works well enough in text classification problems such as spam filtering and the classification of reviews as positive or negative. The algorithm seems perfect at first, but the fundamental representation of Naïve Bayes can create some problems in real-world scenarios.

This article is built upon the assumption that you have a basic understanding of Naïve Bayes. I have written an article on Naïve Bayes. Feel free to check it out.

Understanding Naïve Bayes algorithm

A probabilistic classifier

towardsdatascience.com

Example

Let’s take an example of text classification where the task is to classify whether the review Is positive or negative. We build a likelihood table based on the training data. While querying a review, we use the Likelihood table values, but what if a word in a review was not present in the training dataset?

Query review = w1 w2 w3 w’

We have four words in our query review, and let’s assume only w1, w2, and w3 are present in training data. So, we will have a likelihood for those words. To calculate whether the review is positive or negative, we compare P(positive|review) and P(negative|review).

In the likelihood table, we have P(w1|positive), P(w2|Positive), P(w3|Positive), and P(positive). Oh, wait, but where is P(w’|positive)?

If the word is absent in the training dataset, then we don’t have its likelihood. What should we do?

Approach1- Ignore the term P(w’|positive)

Ignoring means that we are assigning it a value of 1, which means the probability of w’ occurring in positive P(w’|positive) and negative review P(w’|negative) is 1. This approach seems logically incorrect.

Approach 2- In a bag of words model, we count the occurrence of words. The occurrences of word w’ in training are 0. According to that

P(w’|positive)=0 and P(w’|negative)=0, but this will make both P(positive|review) and P(negative|review) equal to 0 since we multiply all the likelihoods. This is the problem of zero probabilitySo, how to deal with this problem?

Image by Pixabay, from Pexels

Laplace Smoothing

Laplace smoothing is a smoothing technique that handles the problem of zero probability in Naïve Bayes. Using Laplace smoothing, we can represent P(w’|positive) as

Here,
alpha represents the smoothing parameter,
K represents the number of dimensions (features) in the data, and
represents the number of reviews with y=positive

If we choose a value of alpha!=0 (not equal to 0), the probability will no longer be zero even if a word is not present in the training dataset.

Interpretation of changing alpha

Let’s say the occurrence of word w is 3 with y=positive in training data. Assuming we have 2 features in our dataset, i.e., K=2 and N=100 (total number of positive reviews).

Case 1- when alpha=1

P(w’|positive) = 3/102

Case 2- when alpha = 100

P(w’|positive) = 103/300

Case 3- when alpha=1000

P(w’|positive) = 1003/2100

As alpha increases, the likelihood probability moves towards uniform distribution (0.5). Most of the time, alpha = 1 is being used to remove the problem of zero probability.

Conclusion

Laplace smoothing is a smoothing technique that helps tackle the problem of zero probability in the Naïve Bayes machine learning algorithm. Using higher alpha values will push the likelihood towards a value of 0.5, i.e., the probability of a word equal to 0.5 for both the positive and negative reviews. Since we are not getting much information from that, it is not preferable. Therefore, it is preferred to use alpha=1.

Thanks for reading!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Laplace smoothing in Naïve Bayes algorithm(拉普拉斯平滑) 的相关文章

随机推荐

  • python实现广义线性模型

    广义线性模型 核心就是最小二乘法 最小二乘法简而言之就是求较小值 在极小值的时候值最小 一阶导数为0 import matplotlib pyplot as plt import numpy as np from sklearn impor
  • Stream流体系

    员工属性类 package Java project 1 public class Employee private String name 姓名 private char sex 性别 private double salary 薪水 p
  • CTF练习WP(week1)之二

    目录 1 flag in your hand1 2 HCTF 2018 WarmUp 1 flag in your hand1 题目链接 题目 xctf org cn 打开附件里的html 在网页上输入token获取flag 会发现每次输入
  • [Vue warn]: Error in render: “TypeError: Cannot read properties of undefined(reading“toString“)

    描述 在我们写了大量的标签但是实际上却出现了无任何东西 一查看控制台就出现了这样的错误提示 解决思路 渲染错误 TypeError 无法读取未定义的属性 读取 toString 全局搜友toSrtring 无变量toString 但是有一个
  • 数据链路层相关协议

    网络类型 根据数据链路层协议进行划分 MA 多点接入网络 BMA广播型 NBMA非广播型 P2P 点到点的网络 以太网协议 需要使用MAC地址对不同的主机设备进行区分和标识 主要因为利用以太网组件的二层网络可以包含 两个和两个以上 的接口
  • 学完责任链之后,逻辑思维上升了一个段位,我马上写了一个月薪3万的简历,HR看了让我去上班

    经过上一篇的文章 我们学习了责任链模式和策略模式 设计模式相对重要 对架构 项目拓展性 移植性要求比较高 下面我会说到简历 对于开发来说 简历是程序员的第二生命 技术是第一生命 简历第二生命 学历第三生命 简历到底是什么 简历是你的第二生命
  • js密码验证

    js密码验证
  • Paper Reading:《LISA: Reasoning Segmentation via Large Language Model》

    目录 简介 目标 创新点 方法 训练 实验 总结 简介 LISA Reasoning Segmentation via Large Language Model 基于大型语言模型的推理分割 日期 2023 8 1 v1 单位 香港中文大学
  • python函数参数里面带*是什么意思

    文章参考 https blog csdn net jiangkejkl article details 121346940 1 函数参数定义中使用独立的符号 在函数定义时 使用了一个独立的符号 这表示在符号后面的参数 调用函数时 必须使用k
  • NAPI机制分析

    NAPI机制分析 NAPI 的核心在于 在一个繁忙网络 每次有网络数据包到达时 不需要都引发中断 因为高频率的中断可能会影响系统的整体效率 假象一个场景 我们此时使用标准的 100M 网卡 可能实际达到的接收速率为 80MBits s 而此
  • 解决 IDEA中springboot项目 修改页面无法生效问题

    解决 IDEA中springboot项目 修改页面无法生效问题 之前网上找了很多解决办法 都是无效的 所以找到解决办法后 先发个博客说一下 至此就完成了springboot 无需重启则对html修改生效 如出现偶尔无效时 请刷新浏览器 之前
  • Linux下使用Git上传和更新代码

    一 上传代码 1 去github上根据网站的提示来创建自己的远程Repository 仓库 2 建立本地git仓库 git init 注意 此指令本地源码根目录执行 执行成功后 会在当前目录生成一个隐藏的名字为 git 的目录 所有对本地仓
  • 【ClickHouse数据库】如何在Win10的Ubuntu上通过ClickHouse存取行情数据

    如何在Win10的Ubuntu上通过ClickHouse存取行情数据 前言 一 ClickHouse是什么 二 如何在Ubuntu上安装ClickHouse 三 添加用户并设置密码 四 使用 1 使用DBeaver操作数据库 2 向Clic
  • 计算机图形学方向和前景&&3D

    我是刚入坑计算机图形学的小菜鸟 在百度上搜索计算机图形学方向和前景和3D 几乎不能搜到什么有用的东西 google还能搜到些有用的 但是需要翻墙 恰好前几天山大承办的games 北京大学陈宝权老师提出了图形学的新疆界 10个左右的国内图形学
  • vue 如何获取input中光标位置,并且点击按钮在当前光标后追加内容

    1 第一步 监听输入框的鼠标失焦事件
  • (原创)c++11中的日期和时间库

    c 11提供了日期时间相关的库chrono 通过chrono相关的库我们可以很方便的处理日期和时间 c 11还提供了字符串的宽窄转换功能 也提供了字符串和数字的相互转换的库 有了这些库提供的便利的工具类 我们能方便的处理日期和时间相关的转换
  • linux服务器管理与维护,linux服务器管理与维护速训..ppt

    linux服务器管理与维护速训 入门级命令 1990年秋天 Linus在芬兰首都赫尔辛基大学学习操作系统课程 因为上机需要排队等待 Linus买了台PC机 开发了第一个程序 程序包括两个进程 分别向屏幕上写字母A和B 然后用定时器来切换进程
  • mysql必考知识_可能是全网最好的MySQL重要知识点 !面试必备

    标题有点标题党的意思 但希望你在看了文章之后不会有这个想法 这篇文章是作者对之前总结的 MySQL 知识点做了完善后的产物 可以用来回顾MySQL基础知识以及备战MySQL常见面试问题 Python资源共享群 484031800 什么是My
  • 在GPU上实现光线跟踪

    include cuda h include book h include cpu bitmap h define DIM 1024 生成图像的大小 DIM DIM define SPHERES 20 生成的图像中球体的个数 define
  • Laplace smoothing in Naïve Bayes algorithm(拉普拉斯平滑)

    在这里转载只是为了让不能够科学搜索的同学们看到好文章而已 个人无收益只是分享知识 顺手做个记录罢了 原网址 https towardsdatascience com laplace smoothing in na C3 AFve bayes