"""
A Naive Bayes classifier is a probabilistic
machine learning model that’s used for
classification task. The crux of the classifier is based on the Bayes theorem.

P(A|B) = P(B|A).P(A) / P(B)
There could be cases where the classification could be multivariate.
 Therefore, we need to find the class y with maximum probability.
Since the way the values are present in the dataset changes,
 the formula for conditional probability changes to

 P(Xi|y) = 1/sqrt(2*pi*var)*exp(-(Xi - mean)**2/2 var)

 Programmed by @Shivam Chhetry
 * 10-08-2021
"""
# Imports Libraries
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn import datasets


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



if __name__ == '__main__':
    X, y = datasets.make_classification(
        n_samples=1000, n_features=10, n_classes=2, random_state=123
    )
    X_train, X_test, y_train, y_test = train_test_split(
        X, y, test_size=0.2, random_state=123
    )
    def accuracy(y_true, y_pred):
        acu = np.sum(y_true == y_pred) / len(y_true)
        return acu*100
    nb = NaiveBayes()
    nb.fit(X_train, y_train)
    predictions = nb.predict(y_test)

    print(f'Naive Bayes classification Accuracy : {accuracy(y_test, predictions)}')
