taspinar / siml

Machine Learning algorithms implemented from scratch
http://www.ataspinar.com
MIT License
484 stars 259 forks source link

error in naive_bayes #8

Closed Sandy4321 closed 4 years ago

Sandy4321 commented 4 years ago

error in https://github.com/taspinar/siml/blob/master/siml/naive_bayes.py File "e:\Baysian\code\siml-master see naive_bayes py plain basyesian\siml-master\siml\naive_bayes.py", line 131, in predicted_Y = nbc.classify(X_test[:100]) File "e:\Baysian\code\siml-master see naive_bayes py plain basyesian\siml-master\siml\naive_bayes.py", line 119, in classify prediction = self.classify_single_elem(X_elem) File "e:\Baysian\code\siml-master see naive_bayes py plain basyesian\siml-master\siml\naive_bayes.py", line 112, in classify_single_elem return self.get_max_value_key(Y_dict) File "e:\Baysian\code\siml-master see naive_bayes py plain basyesian\siml-master\siml\naive_bayes.py", line 17, in get_max_value_key max_value_index = values.index(max(values))

builtins.AttributeError: 'dict_values' object has no attribute 'index'

Sandy4321 commented 4 years ago

I did this fix def get_max_value_key(self, d1): values = d1.values() keys = d1.keys()

feb25 max_value_index = values.index(max(values)) #feb25

    max_value_index = list(values).index(max(values)) #feb25
    #feb 25 max_key = keys[max_value_index]
    max_key = list(keys)[max_value_index] #feb25
    return max_key

is it working correctly now? output is training naive bayes trained F1-score on the test-set for class neg is: 0.8764044943820225 F1-score on the test-set for class neu is: 0 F1-score on the test-set for class pos is: 0 training naive bayes trained F1-score on the test-set for class <=50K is: 0.8951494570913512 F1-score on the test-set for class >50K is: 0.7126779972439136 F1-score on the test-set for class >50K. is: 0

taspinar commented 4 years ago

Hi @Sandy4321 Thank you for this correction. The original code was written in Python 2.7, where the .keys() and .values() method of a dictionary returns a list type object. This is no longer so in Python 3+ so we need to make it a list explicitely. I have updated the code in naive_bayes.py

Sandy4321 commented 4 years ago

Great, will your code work when there is observations in test that are not seen in training data?