<jupyter_start><jupyter_text>Mushroom classification using Naive Bayes  Importing the libraries<jupyter_code>import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split<jupyter_output><empty_output><jupyter_text>Building our model<jupyter_code>df = pd.read_csv('/home/yuvraj/Downloads/hid/x/Coding Blocks ML/19-Naive Bayes Classifier/Mushrooms/mushrooms.csv')
df.head()
df.shape<jupyter_output><empty_output><jupyter_text>Encode the Categorial Data into Numerical Data<jupyter_code>le = LabelEncoder()
# Apply transformation on each column
ds = df.apply(le.fit_transform)
print(type(ds))
ds.head()
# Convert this into numpy array
data = ds.values
print(data.shape)
print(type(data))
print(data[:5,:])<jupyter_output>(8124, 23)
<class 'numpy.ndarray'>
[[1 5 2 4 1 6 1 0 1 4 0 3 2 2 7 7 0 2 1 4 2 3 5]
 [0 5 2 9 1 0 1 0 0 4 0 2 2 2 7 7 0 2 1 4 3 2 1]
 [0 0 2 8 1 3 1 0 0 5 0 2 2 2 7 7 0 2 1 4 3 2 3]
 [1 5 3 8 1 6 1 0 1 5 0 3 2 2 7 7 0 2 1 4 2 3 5]
 [0 5 2 3 0 5 1 1 0 4 1 3 2 2 7 7 0 2 1 0 3 0 1]]<jupyter_text>Break the Data into train and test<jupyter_code>data_x = data[:,1:]
data_y = data[:,0]
x_train,x_test,y_train,y_test = train_test_split(data_x,data_y,test_size = 0.2)
print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)
np.unique(y_train)<jupyter_output><empty_output><jupyter_text>Naive Bayes:<jupyter_code>a = np.array([0,0,1,1,1,0,1])
a == 1
np.sum(a==0)
# Compute Prior Probability
# Just for a sample purpose
def prior(y_train, label):
    total_examples = y_train.shape[0]
    class_examples = np.sum(y_train==label)
    return (class_examples)/float(total_examples)

# Just for a sample purpose
y = np.array([0,5,5,1,1,1,1,0,0,0])
prior(y,5)
# Compute Prior Probability
def prior_prob(y_train, label):
    total_examples = y_train.shape[0]
    class_examples = np.sum(y_train==label)
    return (class_examples)/float(total_examples)
# Compute Conditional Probability
def cond_prob(x_train,y_train,feature_col,feature_val,label):
    x_filtered = x_train[y_train==label]
    numerator = np.sum(x_filtered[:,feature_col]==feature_val)
    denominator = np.sum(y_train==label)
    return numerator/float(denominator)<jupyter_output><empty_output><jupyter_text>Next Step : Compute Posterior Prob for each test example and make predictions<jupyter_code># Checking how many classes of mushrooms are there
classes = np.unique(y_train)
print(classes)
def predict(x_train,y_train,xtest):
        """Xtest is a single testing point, n features"""
        
        classes = np.unique(y_train)
        n_features = x_train.shape[1]
        post_probs = [] # List of prob for all classes and given a single testing point
        
        # Compute Posterior for each class
        for label in classes:
            # Post_c = likelihood*prior
            likelihood = 1.0
            # Conditional probability across all the features
            for f in range(n_features):
                cond = cond_prob(x_train,y_train,f,xtest[f],label)
                likelihood *= cond
                
            prior = prior_prob(y_train,label)
            post = likelihood*prior
            post_probs.append(post)
            
        pred = np.argmax(post_probs)
        return pred
# Trying on some sample data input by user
output = predict(x_train,y_train,x_test[3])
print(output)
print(y_test[3])<jupyter_output>1
1<jupyter_text>Measuring the model<jupyter_code>def score(x_train,y_train,x_test,y_test):
    pred = []
    for i in range(x_test.shape[0]):
        pred_label = predict(x_train,y_train,x_test[i])
        pred.append(pred_label)
        
    pred = np.array(pred)
    
    accuracy = np.sum(pred == y_test)/y_test.shape[0]
    
    return accuracy
print(score(x_train,y_train,x_test,y_test))<jupyter_output>0.9987692307692307
{'fraction_non_alphanumeric': 0.10460577673692428, 'fraction_numerical': 0.045797553994275306, 'mean_word_length': 3.823086574654956, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '419069', 'license': 'MIT', 'n_tokens_mistral': 1540, 'n_tokens_neox': 1386, 'n_words': 365, 'path': 'Mushroom classification using Naive Bayes.ipynb', 'repo_name': 'Yuvrajchopra25/Project-7-Mushroom-classification-using-Naive-Bayes', 'size': '3843'}