<jupyter_start><jupyter_text>Day 3: Naive Bayes classifierIn this notebook we will implement a naive Bayes classfier<jupyter_code>class NaiveBayes():
    def prior(self,c):
        prior_prob = np.mean(self.y==c)
        return prior_prob
    
    def likelihood(self,mu,var,x):
        eps = 1e-4
        coef = 1.0 / np.sqrt(2.0 * np.pi * var + eps)
        exponent = np.exp(-(np.power(x - mu, 2) / (2 * var + eps)))
        likelihood_prob = coef*exponent
        return likelihood_prob
    
    def fit(self,X,y):
        self.x = X
        self.y = y
        self.classes = np.unique(y)
        
        self.parameters=[]
        
        for i, c in enumerate(self.classes):
            x_given_c = X[np.where(y == c)]
            self.parameters.append([])
            for col in x_given_c.T:
                parameter = {"mean": col.mean(), "var": col.var()}
                self.parameters[i].append(parameter)

    def classify(self,x):
        posteriors = []
        for i, c in enumerate(self.classes):
            posterior_prob = self.prior(c)
            for feature_value, params in zip(x, self.parameters[i]):
                likelihood_prob = self.likelihood(params["mean"], params["var"], feature_value)
                posterior_prob *= likelihood_prob
            posteriors.append(posterior_prob)
        # Return the class with the largest posterior probability
        return self.classes[np.argmax(posteriors)]
    
    def predict(self,X):
        y_pred = np.array([self.classify(sample) for sample in X])
        return y_pred
        
        
random_seed = 21
X, y = make_blobs(n_samples=1000, centers=2,random_state=random_seed)
naive_bayes = NaiveBayes()
naive_bayes.fit(X, y)
y_pred= naive_bayes.predict(X)
print("Accuracy: ", np.mean(y==y_pred))<jupyter_output>Accuracy:  1.0
{'fraction_non_alphanumeric': 0.12086092715231789, 'fraction_numerical': 0.009933774834437087, 'mean_word_length': 2.2903811252268604, 'pattern_counts': {'":': 2, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '169593', 'license': 'MIT', 'n_tokens_mistral': 618, 'n_tokens_neox': 594, 'n_words': 128, 'path': 'day-3-naive-bayes.ipynb', 'repo_name': 'sanketg186/100DaysOfML', 'size': '1812'}