# -*- coding: cp936 -*-
#朴素贝叶斯分类器的实现
#运行
#reload(docclass)
#c1=docclass.naivebayes(docclass.getwords)
#docclass.sampletrain(c1)
#c1.classify('quick rabbit',default='unknown')
#构建训练样本
def sampletrain(c1):
c1.train('Nobody owns the water.','good')
c1.train('the quick rabbit jumps fences','good')
c1.train('buy pharmaceuticals now','bad')
c1.train('make quick money at the online casino','bad')
c1.train('the quick brown fox jumps','good')
import re
import math
#从文本中提取特征
def getwords(doc):
splitter=re.compile('\\W*')
#根据非字母字典进行单词拆分
words=[s.lower() for s in splitter.split(doc) if len(s)>2 and len(s)<20]
#只返回一组不重复的单词
return dict([(w,1) for w in words])
#分类器
class classifier:
def __init__(self,getfeatures):
self.fc={}
self.cc={}
self.getfeatures=getfeature
朴素贝叶斯算法Python实现
最新推荐文章于 2024-07-18 07:05:52 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)