我可以为补充朴素贝叶斯设置随机状态吗?

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

我注意到我可以在随机森林 classifier 中设置随机状态,但不能在 Complement Naive Bayes 中设置。我收到错误

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

我假设这是因为 naive_bayes 正在计算每个 class?

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

那是因为模型没有做任何需要随机初始化或生成的事情。有关详细信息,请参阅 source code

万一你想从你自己的那一行重置随机种子,你可以这样做。

import numpy as np
import random

def set_random_seed(seed=0):
  np.random.seed(seed)
  random.seed(seed)

然后您可以在调用您的模型之前重置随机种子

from sklearn.naive_bayes import ComplementNB

set_random_seed(0)
model= ComplementNB()

阅读有关 mechanism of Complement Naive Bayes 的更多信息。