coding utf8 gaussian naive bayes binary problem nclasses 2 def fitself x ynone self setupinputx y check target labels assert listnp uniquey 0 1 mean and variance for each class and feature combination self mean np zerosself nclasses self nfeatures dtypenp float64 self var np zerosself nclasses self nfeatures dtypenp float64 self priors np zerosself nclasses dtypenp float64 for c in rangeself nclasses filter features by class xc xy c calculate mean variance prior for each class self meanc xc meanaxis0 self varc xc varaxis0 self priorsc xc shape0 floatx shape0 def predictself xnone apply predictproba for each row predictions np applyalongaxisself predictrow 1 x normalize probabilities so that each row will sum up to 1 0 return softmaxpredictions def predictrowself x calculate gaussian pdf for each feature mean self meannclass var self varnclass numerator np expx mean 2 2 var denominator np sqrt2 np pi var return numerator denominator coding utf 8 gaussian naive bayes binary problem check target labels mean and variance for each class and feature combination filter features by class calculate mean variance prior for each class apply _predict_proba for each row normalize probabilities so that each row will sum up to 1 0 predict log likelihood for given row calculate gaussian pdf for each feature
import numpy as np

from mla.base import BaseEstimator
from mla.neuralnet.activations import softmax


class NaiveBayesClassifier(BaseEstimator):
    

    
    n_classes = 2

    def fit(self, X, y=None):
        self._setup_input(X, y)
        
        assert list(np.unique(y)) == [0, 1]

        
        self._mean = np.zeros((self.n_classes, self.n_features), dtype=np.float64)
        self._var = np.zeros((self.n_classes, self.n_features), dtype=np.float64)

        self._priors = np.zeros(self.n_classes, dtype=np.float64)

        for c in range(self.n_classes):
            
            X_c = X[y == c]

            
            self._mean[c, :] = X_c.mean(axis=0)
            self._var[c, :] = X_c.var(axis=0)
            self._priors[c] = X_c.shape[0] / float(X.shape[0])

    def _predict(self, X=None):
        
        predictions = np.apply_along_axis(self._predict_row, 1, X)

        
        return softmax(predictions)

    def _predict_row(self, x):
        
        output = []
        for y in range(self.n_classes):
            prior = np.log(self._priors[y])
            posterior = np.log(self._pdf(y, x)).sum()
            prediction = prior + posterior

            output.append(prediction)
        return output

    def _pdf(self, n_class, x):
        

        mean = self._mean[n_class]
        var = self._var[n_class]

        numerator = np.exp(-(x - mean) ** 2 / (2 * var))
        denominator = np.sqrt(2 * np.pi * var)
        return numerator / denominator