scikit-learnnaivebayes

Can I set a random state for the Complement Naive Bayes?


I note I can set the random state in random forest classifier, but not in Complement Naive Bayes. I get error

TypeError: __init__() got an unexpected keyword argument 'random_state'

I assume this is because naive_bayes is computing the probability of each class?

from sklearn.naive_bayes import ComplementNB
model= ComplementNB(random_state=0)

Solution

  • That's because the model doesn't do anything needed to be randomly initialized or generated. see the source code for more details.

    Be in case you want to reset the random seed in that line from your own side, you can do something like this.

    import numpy as np
    import random
    
    def set_random_seed(seed=0):
      np.random.seed(seed)
      random.seed(seed)
    

    Then you can reset the random seed right before calling your model

    from sklearn.naive_bayes import ComplementNB
    
    set_random_seed(0)
    model= ComplementNB()
    

    Read more about the mechanism of Complement Naive Bayes.