Gaussian Naive Bayes

medium
Unit 10 · Classification II0/2 to milestone

Gaussian Naive Bayes models each feature, within each class, as an independent normal distribution:

log⁡p(c∣x)=log⁡p(c)+∑jlog⁡N(xj; μcj,σcj2)−log⁡Z\log p(c \mid x) = \log p(c) + \sum_j \log \mathcal{N}(x_j;\, \mu_{cj}, \sigma^2_{cj}) - \log Z

Implement a class GaussianNB(var_smoothing=1e-9):

  • fit(X, y): store classes_ (sorted), priors_ (class frequencies), means_ and vars_ of shape (classes, features), using the population variance (ddof=0) plus var_smoothing * max(variance of each feature over all of X), as scikit-learn does. Return self.
  • predict_log_proba(X): normalised log-posteriors, using a stable log-sum-exp
  • predict(X): the most probable class labels
#classification#probabilistic-models#bayes2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Gaussian Naive Bayes · TechGradify