首页 > 编程语言 >朴素贝叶斯 python 实现

朴素贝叶斯 python 实现

时间:2023-01-20 10:00:17浏览次数:71  
标签:outlook temp python 贝叶斯 wind humidity class 朴素 cls


​百度文库​​​
​​​文库2​

机器学习实战的朴素贝叶斯的代码太复杂

"""
Created on Thu Aug 10 15:08:59 2017

@author: luogan
"""

#coding=gbk

#Naive Bayes

#Calculate the Prob. of class:cls
def P(data,cls_val,cls_name="class"):
cnt = 0.0
for e in data:
if e[cls_name] == cls_val:
cnt += 1

return cnt/len(data)

#Calculate the Prob(attr|cls)
def PT(data,cls_val,attr_name,attr_val,cls_name="class"):
cnt1 = 0.0
cnt2 = 0.0
for e in data:
if e[cls_name] == cls_val:
cnt1 += 1
if e[attr_name] == attr_val:
cnt2 += 1

return cnt2/cnt1

#Calculate the NB
def NB(data,test,cls_y,cls_n):
PY = P(data,cls_y)
PN = P(data,cls_n)
for key,val in test.items():
print (key,val)
PY *= PT(data,cls_y,key,val)
PN *= PT(data,cls_n,key,val)
return {cls_y:PY,cls_n:PN}


if __name__ == "__main__":

#data
data = [
{"outlook":"sunny", "temp":"hot", "humidity":"high", "wind":"weak", "class":"no" },
{"outlook":"sunny", "temp":"hot", "humidity":"high", "wind":"strong", "class":"no" },
{"outlook":"overcast", "temp":"hot", "humidity":"high", "wind":"weak", "class":"yes" },
{"outlook":"rain", "temp":"mild", "humidity":"high", "wind":"weak", "class":"yes" },
{"outlook":"rain", "temp":"cool", "humidity":"normal", "wind":"weak", "class":"yes" },
{"outlook":"rain", "temp":"cool", "humidity":"normal", "wind":"strong", "class":"no" },
{"outlook":"overcast", "temp":"cool", "humidity":"normal", "wind":"strong", "class":"yes" },
{"outlook":"sunny", "temp":"mild", "humidity":"high", "wind":"weak", "class":"no" },
{"outlook":"sunny", "temp":"cool", "humidity":"normal", "wind":"weak", "class":"yes" },
{"outlook":"rain", "temp":"mild", "humidity":"normal", "wind":"weak", "class":"yes" },
{"outlook":"sunny", "temp":"mild", "humidity":"normal", "wind":"strong", "class":"yes" },
{"outlook":"overcast", "temp":"mild", "humidity":"high", "wind":"strong", "class":"yes" },
{"outlook":"overcast", "temp":"hot", "humidity":"normal", "wind":"weak", "class":"yes" },
{"outlook":"rain", "temp":"mild", "humidity":"high", "wind":"strong", "class":"no" },
]

#calculate
print (NB(data,{"outlook":"sunny","temp":"cool","humidity":"high","wind":"strong"},"yes","no"))


标签:outlook,temp,python,贝叶斯,wind,humidity,class,朴素,cls
From: https://blog.51cto.com/u_15202985/6020515

相关文章

  • Python学习笔记-常用模块介绍--subprocess函数
    1.什么是subprocess?是英文翻译过来,是子进程的意思。sub儿子;process进程2.示例1:importsubprocess#sub是儿子的意思,这个函数是开启了子进程。importtime......
  • Python学习笔记-常用模块介绍--configparser函数
    1.常见的配置有哪些?ini/cnfyaml2.示例#注释1;注释2这是config.ini的文件[mysqld]k1=v1k2:v2user=egonage=18is_admin=truesalary=31[client]k1=......
  • Python学习笔记-常用模块介绍--random随机数函数
    1.random模块基本使用importrandomprint(random.random())#(0,1)----float大于0且小于1之间的小数print(random.randint(1,3))#[1,3]大于等于1且小......
  • Python学习笔记-常用模块介绍--hashlib哈希函数
    1.hash算法是什么?传入的值一样,结果一样。一种哈希算法算出来的结果,长度固定。不能反解。2.(扩展知识)文件下载完成后,进度显示100%,为什么不能立马打开?下载完成后,会进......
  • Python入门之变量
    """变量:在内存中存储数据"""#语法:#变量名称=对象#例如:name="张无忌"print(name)#语义:内存图#变量名:真实内存地址的别名#见名知意#......
  • Python爬虫之58租房+高德地图
    任务分析用Python爬虫在58同城网上爬取房源信息,保存为本地CSV文件申请高德开发者Key,用JSAPI示例编写Index.html,在web上设置工作地点,导入CSV房源信息文件,显示工作地点及......
  • Python-构建自适应线性神经元
    Python-构建自适应线性神经元1自适应线性神经元自适应性神经元(Adaline)可以视为感知器的优化和改进。该算法说明了定义最小化连续性代价函数的关键概念。这为理解如逻辑......
  • 将Python文件.py打包成.exe可执行程序
    点我~原创-最优教程.exe是文件扩展名,带有.exe扩展名的文件名按下Enter键就可运行。这么个意思不就是:不需要装python即可运行python程序了,岂不美哉(_)v这里说一下,本文只......
  • python pandas dataframe读取超大数据集
    前言最近在搞一个根因分析相关的项目,内部用到一个原因模拟器,自动生成各种问题可能导致的告警现象,算是大数据的边缘,一提到大数据,数据量就大了,项目大概需要模拟3000+个根源......
  • Python3.9+torch1.7.1+cuda11.0+cudnn8.0+Anaconda3安装
    前言想要安装pytorch,至少得先安装Anaconda、python!!!必要的不想用cpu要用gpu的还需要cuda11.0+cudnn11.0!!!一、安装python3.9二、安装Anaconda3三、安装cuda11.0+cudnn8.0四、......