<jupyter_start><jupyter_text>AIM --> To find posterior probablity of each class & maximum is our answer  Importing Libraries & getting Data<jupyter_code>import numpy as np 
import pandas as pd
import seaborn as sns
df = pd.read_csv('./dataset/mushrooms.csv')
df.head()
df.shape<jupyter_output><empty_output><jupyter_text>Encoding<jupyter_code>from sklearn.preprocessing import LabelEncoder 
le = LabelEncoder()
# df.apply means that function will be applied to each column
ds = df.apply(func=le.fit_transform)
ds.head()
data = ds.values
X = data[:,1:]
y = data[: ,0]
X.shape , y.shape
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)<jupyter_output><empty_output><jupyter_text>Custom Implementation of Naive Bayes<jupyter_code>class CustomNaiveBayes:

    def fit(self,X,y):
        self.X_train = X
        self.y_train = y

    # label is the class for which we want prior-probablity 
    def prior_prob(self,label):
        total = self.y_train.shape[0]
        class_Examples = np.sum(self.y_train==label)
        return class_Examples/float(total)

    # for a single word
    def conditional_prob(self ,feature_col ,feature_val ,label):
        # removing all the X that doesn't belong in that class i.e consists of all examples in class label
        X_filtered  =self.X_train[self.y_train == label]
        numerator = np.sum(X_filtered[: , feature_col] == feature_val)
        denominator = len(X_filtered)
        return numerator/denominator


    # now apply abve func to every word in every single example 
    def predict_pt(self,X_test):
        # X_test is a single example with n features.
        # classes are 0 & 1 in this case
        classes = np.unique(self.y_train)
        # 22 in this case
        n_features = self.X_train.shape[1]


        # post_prb in each class
        # post_prob = prior_prob * likelihood
        post_prob =[]
        for label in classes:
            # initially
            likelihood = 1.0
            for feature in range(n_features):
                conditional = self.conditional_prob(feature ,X_test[feature] , label)
                likelihood*=conditional

            prior =self.prior_prob(label)

            posterior = prior * likelihood
            post_prob.append(posterior)

        # final answer is max value from all labels
        # argmax will return index of the largest value in array
        return np.argmax(post_prob)


    def predict(self ,X_test):
        result = []
        for pt in X_test:
            result.append(self.predict_pt(pt))
        return np.array(result)


    def accuracy(self, X_test ,y_test):
        return (self.predict(X_test) == y_test).mean()

model = CustomNaiveBayes()
model.fit(X_train , y_train)
y_pred = model.predict(X_test)
model.predict(X_test[:10])
y_test[:10]<jupyter_output><empty_output><jupyter_text>Model Evaluation<jupyter_code>from sklearn.metrics import confusion_matrix , accuracy_score
print('Accuracy of model : ',accuracy_score(y_test ,y_pred))
cf_matrix = confusion_matrix(y_test , y_pred)
sns.heatmap(cf_matrix ,annot=True ,cmap='Reds',fmt='.4g')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09104796822487014, 'fraction_numerical': 0.006110601894286587, 'mean_word_length': 2.987819732034105, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 18, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '398647', 'license': 'MIT', 'n_tokens_mistral': 1087, 'n_tokens_neox': 1022, 'n_words': 264, 'path': 'Custom Implementation of ML-algos/Naive Bayes/naivebayes_custom.ipynb', 'repo_name': 'faizanxmulla/data-science-portfolio', 'size': '3273'}