朴素贝叶斯法的参数估计——极大似然估计及其Python实现

乖乖的函数 2020-11-13 02:53:23
参数 极大 朴素 估计 参数估计


统计学习方法——朴素贝叶斯法原理

1. 朴素贝叶斯法的极大似然估计

在这里插入图片描述

2. 朴素贝叶斯极大似然学习及分类算法

算法过程:
在这里插入图片描述

2. Python实现

def priorProbability(labelList): # 计算先验概率
labelSet = set(labelList) # 得到类别的值
labelCountDict = {
} # 利用一个字典来存储训练集中各个类别的实例数
for label in labelList:
if label not in labelCountDict:
labelCountDict[label] = 0
labelCountDict[label] += 1
priorProbabilityDict = {
}
for label in labelSet: # 计算不同的类别对应的先验概率
priorProbabilityDict[label] = labelCountDict[label]/len(labelList)
return priorProbabilityDict
def conditionProbability(dataSet,labelList): # 计算条件概率
dimNum = len(dataSet[0]) # 得到特征数
characterVal = []
# 利用一个数组来存储训练数据集中不同特征的不同特征值。
# 每一个不同特征的特征值都要需要另一个数组来存储,这样 characterVal实际上是一个二维数组
for i in range(dimNum):
temp = []
for j in range(len(dataSet)):
if dataSet[j][i] not in temp:
temp.append(dataSet[j][i])
characterVal.append(temp)
probability = [] # 数组来存储最后的所有的条件概率
labelSet = list(set(labelList))
for dim in range(dimNum): # 学习条件概率,需要计算K*S1*...*Sj个概率
tempMemories = {
} # 对于每一个特征,利用一个字点来存储这个特征所有的取值对应的条件概率
for val in characterVal[dim]:
for label in labelSet:
labelCount = 0 # 记录每一类的个数
mixCount = 0 # 记录当前特征值为这个数,且类别为这个类别的实例个数
for i in range(len(labelList)):
if labelList[i] == label:
labelCount += 1
if dataSet[i][dim] == val:
mixCount += 1
tempMemories[str(val) + "|" + str(label)] = mixCount/labelCount
# key表示哪一个特征值和类别,键表示对应的条件概率
probability.append(tempMemories) # 计算完一个特征,填充一个
return probability # 返回条件概率
def naiveBayes(x,dataSet,labelList): # 贝叶斯分类
priorProbabilityDict = priorProbability(labelList)
probability = conditionProbability(dataSet,labelList)
bayesProbability = {
} # 计算所有类所对应的后验概率
labelSet = list(set(labelList))
for label in labelSet:
tempProb = priorProbabilityDict[label]
for dim in range(len(x)):
tempProb *= probability[dim][str(x[dim])+"|"+str(label)]
bayesProbability[label] = tempProb
result = sorted(bayesProbability.items(),key= lambda x:x[1],reverse=True)# 排序
return result[0][0]# 返回后验概率最大的类
dataSet = ([[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"]])
labelList = [-1,-1,1,1,-1,-1,-1,1,1,1,1,1,1,1,-1]
print(naiveBayes([2,"s"],dataSet,labelList))
## 返回结果为-1,即归为-1类。
这个实现过程和书上的不太一样,这里每一个特征的取值范围和类的取值范围是根据数据集中的数来进行确定,即每一个特征的取值范围不考虑那些没有出现在训练数据集中的特征值。而书上的算法,每一个特征的取值范围是事先给出的,在这个取值范围中的特征值,可能会出现在训练数据集中,可能不出现。但在估计先验概率和条件概率的时候,过程是一样的。这是这个实现过程的一个不足。
版权声明
本文为[乖乖的函数]所创,转载请带上原文链接,感谢
https://blog.csdn.net/ggdhs/article/details/94554917

  1. 利用Python爬虫获取招聘网站职位信息
  2. Using Python crawler to obtain job information of recruitment website
  3. Several highly rated Python libraries arrow, jsonpath, psutil and tenacity are recommended
  4. Python装饰器
  5. Python实现LDAP认证
  6. Python decorator
  7. Implementing LDAP authentication with Python
  8. Vscode configures Python development environment!
  9. In Python, how dare you say you can't log module? ️
  10. 我收藏的有关Python的电子书和资料
  11. python 中 lambda的一些tips
  12. python中字典的一些tips
  13. python 用生成器生成斐波那契数列
  14. python脚本转pyc踩了个坑。。。
  15. My collection of e-books and materials about Python
  16. Some tips of lambda in Python
  17. Some tips of dictionary in Python
  18. Using Python generator to generate Fibonacci sequence
  19. The conversion of Python script to PyC stepped on a pit...
  20. Python游戏开发,pygame模块,Python实现扫雷小游戏
  21. Python game development, pyGame module, python implementation of minesweeping games
  22. Python实用工具,email模块,Python实现邮件远程控制自己电脑
  23. Python utility, email module, python realizes mail remote control of its own computer
  24. 毫无头绪的自学Python,你可能连门槛都摸不到!【最佳学习路线】
  25. Python读取二进制文件代码方法解析
  26. Python字典的实现原理
  27. Without a clue, you may not even touch the threshold【 Best learning route]
  28. Parsing method of Python reading binary file code
  29. Implementation principle of Python dictionary
  30. You must know the function of pandas to parse JSON data - JSON_ normalize()
  31. Python实用案例,私人定制,Python自动化生成爱豆专属2021日历
  32. Python practical case, private customization, python automatic generation of Adu exclusive 2021 calendar
  33. 《Python实例》震惊了,用Python这么简单实现了聊天系统的脏话,广告检测
  34. "Python instance" was shocked and realized the dirty words and advertisement detection of the chat system in Python
  35. Convolutional neural network processing sequence for Python deep learning
  36. Python data structure and algorithm (1) -- enum type enum
  37. 超全大厂算法岗百问百答(推荐系统/机器学习/深度学习/C++/Spark/python)
  38. 【Python进阶】你真的明白NumPy中的ndarray吗?
  39. All questions and answers for algorithm posts of super large factories (recommended system / machine learning / deep learning / C + + / spark / Python)
  40. [advanced Python] do you really understand ndarray in numpy?
  41. 【Python进阶】Python进阶专栏栏主自述:不忘初心,砥砺前行
  42. [advanced Python] Python advanced column main readme: never forget the original intention and forge ahead
  43. python垃圾回收和缓存管理
  44. java调用Python程序
  45. java调用Python程序
  46. Python常用函数有哪些?Python基础入门课程
  47. Python garbage collection and cache management
  48. Java calling Python program
  49. Java calling Python program
  50. What functions are commonly used in Python? Introduction to Python Basics
  51. Python basic knowledge
  52. Anaconda5.2 安装 Python 库(MySQLdb)的方法
  53. Python实现对脑电数据情绪分析
  54. Anaconda 5.2 method of installing Python Library (mysqldb)
  55. Python implements emotion analysis of EEG data
  56. Master some advanced usage of Python in 30 seconds, which makes others envy it
  57. python爬取百度图片并对图片做一系列处理
  58. Python crawls Baidu pictures and does a series of processing on them
  59. python链接mysql数据库
  60. Python link MySQL database