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', '?']
If you look at all the entries in
trainingset.txt
, you'll see that all the entries withWithout-pay
are listed as having<=50k
income. As you can see in__init__
, the training data is split into two groups:Since none of the
Without-pay
entries have>50k
income, none of them go intogreaterThan_data
.So when you call...
..the resulting dict lacks that key.