如下所示,下面平衡的一维数据可以通过
sklearn GaussianNB
完美分离。为什么 sklearn ComplementNB
对于相同数据给出的分类全为零?
from sklearn.naive_bayes import GaussianNB
from sklearn.naive_bayes import ComplementNB
import numpy as np
N = 20
np.random.seed(9)
pos = np.random.uniform(size = N, low = 0.7, high = 0.8).reshape(-1, 1)
neg = np.random.uniform(size = N, low = 0.4, high = 0.5).reshape(-1, 1)
X = np.r_[pos, neg]
Y = np.array([1] * N + [0] * N)
gnb = GaussianNB()
cnb = ComplementNB()
gnb.fit(X,Y)
cnb.fit(X,Y)
#predict training data
print(gnb.predict(X))
print(cnb.predict(X))
高斯朴素贝叶斯模型 100% 正确。补朴素贝叶斯模型仅预测零。为什么?
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]