Python Key Error

2019-07-22 07:03发布

问题:

I'm having a Python Key Error and haven't been able to sort it out.

The error that is given is shown below

Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
import handling_data_
File "C:\Python27\handling_data_.py", line 453, in <module>
main()
File "C:\Python27\handling_data_.py", line 443, in main
pans.append([row[0],p.classify(row)])
File "C:\Python27\handling_data_.py", line 367, in classify
cgclass = self.greater_class_prob_dist[query[2]]
KeyError: ' Without-pay'

The line in question is

clclass = self.less_class_prob_dist[query[2]]

I amen't sure what wrong with this line as there is another line for greater_class_prob_dist

cgclass = self.greater_class_prob_dist[query[2]]

The class is a naive bayes implementation, it works prefectly if take the entries from the text file which contain ' Without-pay' otherwise it does not.

Does anyone know how to fix this problem?

Here is a link to the whole class and textfiles if people need to look at it or run it to see the problem for themselves Source Code

Any help much appericated

EDIT: print of record contains ' Without-pay'

['tst1249', 62, ' Without-pay', 170114, ' Assoc-acdm', 12, ' Married-civ-spouse', ' Farming-fishing', ' Husband', ' White', ' Male', 0, 0, 50, ' United-States', '?']

回答1:

If you look at all the entries in trainingset.txt, you'll see that all the entries with Without-pay are listed as having <=50k income. As you can see in __init__, the training data is split into two groups:

        for row in data:
              if row[15] == ' >50K':
                    self.greaterThan_data.append(row)
              else:
                    self.lessThan_data.append(row)

Since none of the Without-pay entries have >50k income, none of them go into greaterThan_data.

So when you call...

        self.greater_class_prob_dist = self.getCatProbs(self.greaterThan_data,2)

..the resulting dict lacks that key.