Reputation: 145
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)
Upvotes: -1
Views: 1389
Reputation: 615
Three years on, sklearn has a page with information and code on reproducability. So you do not need to define your own function. It covers the two packages numpy and random, which are the underlying random generators. As amirhe said, this classifier has no option to define a random state (see documentation).
import numpy as np
import random
random_seed = 54 # Random Seed at file level
np.random.seed(random_seed) #set random seed for numpy
random.seed(random_seed) #set random seed for random
Upvotes: 0
Reputation: 2341
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.
Upvotes: 1