Back to course overview

Naive Bayes in Python - ML From Scratch 05

In this Machine Learning from Scratch Tutorial, we are going to implement the Naive Bayes algorithm, using only built-in Python modules and numpy. We will also learn about the concept and the math behind this popular ML algorithm.

All algorithms from this course can be found on GitHub together with example tests.

Further readings:

https://towardsdatascience.com/naive-bayes-classifier-81d512f50a7c

Implementation

import numpy as np class NaiveBayes: def fit(self, X, y): n_samples, n_features = X.shape self._classes = np.unique(y) n_classes = len(self._classes) # calculate mean, var, and prior for each class self._mean = np.zeros((n_classes, n_features), dtype=np.float64) self._var = np.zeros((n_classes, n_features), dtype=np.float64) self._priors = np.zeros(n_classes, dtype=np.float64) for idx, c in enumerate(self._classes): X_c = X[y==c] self._mean[idx, :] = X_c.mean(axis=0) self._var[idx, :] = X_c.var(axis=0) self._priors[idx] = X_c.shape[0] / float(n_samples) def predict(self, X): y_pred = [self._predict(x) for x in X] return np.array(y_pred) def _predict(self, x): posteriors = [] # calculate posterior probability for each class for idx, c in enumerate(self._classes): prior = np.log(self._priors[idx]) posterior = np.sum(np.log(self._pdf(idx, x))) posterior = prior + posterior posteriors.append(posterior) # return class with highest posterior probability return self._classes[np.argmax(posteriors)] def _pdf(self, class_idx, x): mean = self._mean[class_idx] var = self._var[class_idx] numerator = np.exp(- (x-mean)**2 / (2 * var)) denominator = np.sqrt(2 * np.pi * var) return numerator / denominator

FREE VS Code / PyCharm Extensions I Use

🪁 Code faster with Kite, AI-powered autocomplete: Link *

✅ Write cleaner code with Sourcery, instant refactoring suggestions: Link *

* These are affiliate links. By clicking on it you will not have any additional costs, instead you will support me and my project. Thank you! 🙏

Check out my Courses