博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
朴素贝叶斯法分类器的Python3 实现
阅读量:4589 次
发布时间:2019-06-09

本文共 3383 字,大约阅读时间需要 11 分钟。

本篇文章是我在读了李航的<统计学习方法>后手写的算法实现之一

原理请参考

代码如下:

# - * - coding: utf - 8 -*-## 作者:田丰# 邮箱:fonttian@163.com# 撰写时间:2017年4月26日# Python版本:3.6.1# CSDN:http://blog.csdn.net/fontthrone## 下一行为符号计算所导入的包# from fractions import Fractionclass NaiveBayesMethod:    '''    NaiveBayesMethod 的内部计算方式现在为数值计算,    符号计算的代码已经注释,如果需要请手动修改    朴素贝叶斯法分类器 当lam=1 时,类分类方式为为贝叶斯估计    实现了拉普拉斯平滑,以此避免出现要计算的概率为0的情况,以免计算错误的累积    具体原理请参考李航的
<统计学习方法>
第四章 lam = 0 时 类分类方式为为极大似然值估计 ''' def __init__(self, inputArray, lam): self.input = inputArray self.lam = lam self.__lenInput = len(self.input) self.__y = self.input[self.__lenInput - 1] self.__onlyy = self.__only(self.__y) self.__county = self.__countList(self.__onlyy) # 计算列表总样本数 return int def __countList(self, list): count = {} for item in list: count[item] = count.get(item, 0) + 1 return len(count) # 检查某列表中时候含有某个元素 def __findy(self, list, y): result = True for i in range(0, len(list)): if list[i] == y: result = False return result # 返回列表种类 def __only(self, list): onlyy = [] for i in range(0, len(list)): if self.__findy(onlyy, list[i]): onlyy.append(list[i]) return onlyy # 统计列表中某元素的个数 def __countKind(self, list, element): return list.count(element) # 通过元素值返回位置索引 def __findOnlyElement(self, list, x): return self.__only(list).index(x) # 先验概率 def __py(self, x): # return Fraction(self.__countKind(self.__y, x) + self.lam, len(self.__y) + self.__county * self.lam) return (self.__countKind(self.__y, x) + self.lam) / (len(self.__y) + self.__county * self.lam) # 返回p(x=?) def __probabilityX(self, list, x): # return Fraction(self.__countKind(list, x) + self.lam, len(list) + self.__countList(list) * self.lam) return (self.__countKind(list, x) + self.lam) / (len(list) + self.__countList(list) * self.lam) def __probabilityYX(self, list, x, yy): xx = self.__findOnlyElement(list, x) yindex = self.__findOnlyElement(self.__y, yy) fz = 0 onlyx = self.__only(list) onlyy = self.__only(self.__y) # 获取 p(y=?|x1=?) 的分子 for i in range(0, len(list)): if list[i] == onlyx[xx] and self.__y[i] == onlyy[yindex]: fz += 1 # return Fraction(fz + self.lam, self.__countKind(list, onlyx[xx]) + self.__countList(list) * self.lam) return (fz + self.lam) / (self.__countKind(list, onlyx[xx]) + self.__countList(list) * self.lam) def fl(self, x, y): ps = [] for i in range(0, len(self.__onlyy)): p1 = self.__probabilityX(self.input[0], x) * self.__probabilityYX(self.input[0], x, 1) * self.__probabilityX( self.input[1], y) * self.__probabilityYX(self.input[1], y, self.__onlyy[i]) / self.__py(1) ps.append(p1) return self.__onlyy[ps.index(max(ps))]# 测试NaiveBayesMethodinput = [[1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3], [1, 2, 2, 1, 1, 1, 2, 2, 3, 3, 3, 2, 2, 3, 3], [-1, -1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, -1]]test = NaiveBayesMethod(input, 1)print(test.fl(2, 1))test.lam = 0print(test.fl(2, 1))
输出结果如下:-1-1

转载于:https://www.cnblogs.com/fonttian/p/9162841.html

你可能感兴趣的文章
Marketing™Series用户手册(Marketing™Series Manual)
查看>>
Java动态代理
查看>>
二维码开源库zbar、zxing使用心得
查看>>
框架设计读书笔记--扩展点设计--组合法
查看>>
Web开发小贴士 -- 全面了解Cookie
查看>>
收藏Javascript中常用的55个经典技巧
查看>>
Arm-linux-gcc-4.3.2安装步骤
查看>>
Java多线程与并发编程学习
查看>>
Support Vector Machine
查看>>
牛客-2018多校算法第五场C-KMP
查看>>
Linux查看文件内容
查看>>
[转]社会生活中十二大著名法则 1 马太效应 2 手表定理 3 不值得定律 4 彼得原理 5 零和游戏原理 6 华盛顿合作规律 7 酒与污水定律 8 水桶定律 9 蘑菇管理 10 奥...
查看>>
浅谈三层与实体
查看>>
cocostudio——js 3 final控件事件
查看>>
Flex 学习笔记 datatip的背景颜色
查看>>
iOS开发中六种手势识别
查看>>
oracle创建临时表没有权限
查看>>
76.数塔问题
查看>>
PHP 透明水印生成代码
查看>>
我就是学习
查看>>