我正在尝试编写朴素贝叶斯分类器,但我不断收到此错误:
Traceback (most recent call last):
File "<pyshell#30>", line 1, in <module>
import naive_assignment
File "C:\Python27\naive_assignment.py", line 655, in <module>
main()
File "C:\Python27\naive_assignment.py", line 650, in main
pans.append(p.classify(row))
File "C:\Python27\naive_assignment.py", line 597, in classify
less50Kcp = less50Kcp + self.less_cat_probs.get(query[4])
TypeError: unsupported operand type(s) for +: 'float' and 'NoneType'
我确定如何修复它,因为那里的大多数修复都说返回一些东西,但已经在代码中。
def classify(self, query):
less50Knp = 0.0
less50Kcp = 0.0
great50Knp = 0.0
great50Kcp = 0.0
less50Knp = less50Knp +self.less_num_prob_dist(float(query[1])/100)
less50Knp = less50Knp + self.less_num_prob_dist(float(query[3])/100)
less50Knp = less50Knp + self.less_num_prob_dist(float(query[5])/100)
less50Knp = less50Knp + self.less_num_prob_dist(float(query[11])/100)
less50Knp = less50Knp + self.less_num_prob_dist(float(query[12])/100)
less50Knp = less50Knp + self.less_num_prob_dist(float(query[13])/100)
less50Kcp = less50Kcp + self.less_cat_probs.get(query[2])
less50Kcp = less50Kcp + self.less_cat_probs.get(query[4])
less50Kcp = less50Kcp + self.less_cat_probs.get(query[6])
less50Kcp = less50Kcp + self.less_cat_probs.get(query[7])
less50Kcp = less50Kcp + self.less_cat_probs.get(query[8])
less50Kcp = less50Kcp + self.less_cat_probs.get(query[9])
less50Kcp = less50Kcp + self.less_cat_probs.get(query[10])
less50Kcp = less50Kcp + self.less_cat_probs.get(query[14])
less50K_prob = less50Kcp * less50Knp
great50Knp = great50Knp + self.great_num_prob_dist(float(query[1])/100)
great50Knp = great50Knp + self.great_num_prob_dist(float(query[3])/100)
great50Knp = great50Knp + self.great_num_prob_dist(float(query[5])/100)
great50Knp = great50Knp + self.great_num_prob_dist(float(query[11])/100)
great50Knp = great50Knp + self.great_num_prob_dist(float(query[12])/100)
great50Knp = great50Knp + self.great_num_prob_dist(float(query[13])/100)
great50Kcp = great50Kcp + self.great_cat_probs.get(query[2])
great50Kcp = great50Kcp + self.great_cat_probs.get(query[4])
great50Kcp = great50Kcp + self.great_cat_probs.get(query[6])
great50Kcp = great50Kcp + self.great_cat_probs.get(query[7])
great50Kcp = great50Kcp + self.great_cat_probs.get(query[8])
great50Kcp = great50Kcp + self.great_cat_probs.get(query[9])
great50Kcp = great50Kcp + self.great_cat_probs.get(query[10])
great50Kcp = great50Kcp + self.great_cat_probs.get(query[14])
great50K_prob = great50Kcp * great50Knp
if less50K_prob > great50K_prob:
return ' <=50K'
elif less50K_prob < great50K_prob:
return ' >50K'
else:
return 'unknown'
我知道这不是最好的编码方式。调用它的主要函数是:
def main():
data = getInputData('./trainingset.txt')
test = getInputData('./queries.txt')
p = nbayes(data)
p.train()
pans = []
for row in test:
pans.append(p.classify(row))
print("n-bayes")
print(pans)
main()
有谁知道如何解决这个问题?