我想寫樸素貝葉斯分類器,我不斷收到此錯誤:Python的錯誤不支持的操作類型
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()
任何人都不會有想法如何解決這一問題?