我正在使用朴素贝叶斯多项式模型。我应该使用在train方法中看到的伪代码。所以这些是我的问题:
1)我已经放入了大部分代码,但我主要在提取词汇表、计算类中文档的数量以及连接类中所有文档的文本方面遇到了一些问题。
2)我还注意到我需要的训练方法只需要文档(也就是train_doc)。因此,我不知道如何调整以获得C类。
def train(self, documents):
# TRAINMULTINOMIALNB(C,D)
# 1 V <-- EXTRACTVOCABULARY(D)
# 2 N <-- COUNTDOCS(D)
# 3 for each c in C
# 4 do Nc <-- COUNTDOCSINCLASS(D, c)
# 5 prior[c] <-- Nc/N
# 6 textc <-- CONCATENATETEXTOFALLDOCSINCLASS(D, c)
# 7 for each t in V
# 8 do Tct <-- COUNTTOKENSOFTERM(textc, t)
# 9 for each t in V
# 10 do condprob[t][c] <-- Tct+1
# 11 return V, prior, condprob
"""
prior={}
N = len(documents)
#Vocab
V = Counter()
for d in documents:
V.update(doc[***])
#COUNTDOCSINCLASS(C,D)
cdic = Counter(C)
for d2 in documents:
for label in C:
cdic.update({label:int(math.ceil(float(doc[***])))})
#CONCATENATETEXTOFALLDOCSINCLASS(documents,C)
ctoadic = defaultdict(Counter)
for d3 in document:
for label2 in C:
if(float(***)>0):
ctoadic[label].update(doc[***])
#used to get term by class it is in
tii = defaultdict(Counter)
for label,word in ctoadic.iteritems():
for w in word:
tii[w].update({l:word[w]})
#getCondProb(tii,ctofadic,C)
gcp = defaultdict(lambda: defaultdict(float))
tnw ={} #total number of words in that label
for l,v inctofadic.iteritems():
tnwl[l] = sum(v.values())
for w,count in tii.iteritems():
#for 0 occurences
z = [zeroo for zeroo in C if zeroo not in count.keys()]
for ling in z:
gcp[w[ling]=1.0/(len(ctofadic[ling])+tnw[ling])
for ling,val in count.iteritems():
gcp[w][ling]=float(val+1)/(len(ctofadic[ling])+tnw[ling])
#Prior
for c in C:
prior[c] = cdic[c] / float(N)
return V,prior,gcp发布于 2015-09-16 05:04:59
对于问题1
词汇表的
l=label1,W=word1计数
l=label1,W=word2计数
。
l=label2,W=word3计数
。
l=label3,W=word1 count
等等,还添加了如下内容:-
Vocab,word1 count
Vocab,word2 count
Vocab,word3 count在这里,words1,word2,word3是训练文档中遇到的所有单词,但都是独一无二的。将它们存储在hashmap中并刷新。
然后在分类器中,每当你遇到" vocab“时,加1。总数将是vocab。
对于问题2
https://stackoverflow.com/questions/29376070
复制相似问题