0
$\begingroup$
from sklearn.naive_bayes import GaussianNB,MultinomialNB
xx = [[1],[1],[1],[2],[2],[3]]
yy = [1,1,1,0,0,0]
clf = GaussianNB()
# clf = MultinomialNB()
clf.fit(xx,yy)
clf.predict(xx)

The expected result is [1,1,1,0,0,0] but code output is [0,0,0,0,0,0].

$\endgroup$
2
  • $\begingroup$ Check the regularization parameter--that's often the problem with questions like these. If it's too high, this can happen. $\endgroup$ Commented Aug 28, 2019 at 6:51
  • $\begingroup$ @SheridanGrant you mean alpha parameter for Laplace smoothing right. the default value for it is 1. $\endgroup$
    – bhanu raja
    Commented Aug 28, 2019 at 7:05

1 Answer 1

0
$\begingroup$

There is only one feature for xx.

For a MultinomialNB, the modelled probability of the feature per class $p(x_i|y)$ is always the same ($\frac{3}{3}=1$ for class $1$ and $\frac{7}{7}=1$ for class $0$). The prediction is therefore the class prior $p(y_i)$ which is also the same here ($p(0) = 0.5$ and $p(1)=0.5$) so by default class $0$. Having one feature does not really makes sense because it gives no information to the model on what gives discriminative power toward the classes.

For a GaussianNB this is different because the modelled probability is given by $P(x_i \mid y) = \frac{1}{\sqrt{2\pi\sigma^2_y}} \exp\left(-\frac{(x_i - \mu_y)^2}{2\sigma^2_y}\right)$ so the mean and variance per class of the single feature give sufficient information.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.