<jupyter_start><jupyter_text>Importing Packages<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Reading the data<jupyter_code>data=pd.read_csv('mushrooms.csv')
pd.set_option('display.max_columns', 500)
data.head()
data.columns<jupyter_output><empty_output><jupyter_text>Encoding the Categorial data<jupyter_code>from sklearn.preprocessing import LabelEncoder
le=LabelEncoder()
df=data.apply(le.fit_transform)
df.head()<jupyter_output><empty_output><jupyter_text>Splitting the data into training and testing data<jupyter_code>X=df.iloc[:,[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22]].values
Y=df.iloc[:,0].values
from sklearn.model_selection import train_test_split as tt
X_train, X_test , Y_train, Y_test=tt(X,Y,test_size=0.2,random_state=100)
X_train.shape
Y.shape<jupyter_output><empty_output><jupyter_text>Building the Naive Bayes Model<jupyter_code>def prior_prob(y_train,label):
    numer = np.sum(y_train==label)
    denom = y_train.shape[0]
    return numer/float(denom)
def livelihood(x_train,y_train,feature_col,feature_val,label):
    denom = np.sum(y_train==label)
    X_new = x_train[y_train==label]
    numer = np.sum(X_new[:,feature_col]==feature_val)
    return numer/float(denom)
def posterior_prob(x_train,y_train,x_test):
    prediction=[]
    for i_data in np.unique(y_train):
        live=1.0
        for i_col in range(x_train.shape[1]):
            live *= livelihood(x_train,y_train,i_col,x_test[i_col],i_data)
        prior = prior_prob(y_train,i_data)
        predict = prior * live
        prediction.append(predict)
#         prediction=np.array(prediction)
#         prediction.ravel()
    return np.argmax(prediction)    
# [[764  62]
#  [ 57 742]]
def score(x_train,y_train,x_test,y_test):
    y_pred = []
    for i_data in range(x_test.shape[0]):
        pred = posterior_prob(x_train,y_train,x_test[i_data])
        y_pred.append(pred)
    y_pred = np.array(y_pred)
    ans = (np.sum(y_pred==y_test))/float(y_test.shape[0])
    return ans
score(X_train,Y_train,X_test,Y_test)<jupyter_output><empty_output><jupyter_text>Model using sklearn<jupyter_code>from sklearn.naive_bayes import GaussianNB
nb = GaussianNB()
nb.fit(X_train,Y_train)

import statsmodels.api as sm
X_train = sm.add_constant(X_train)
nbm = sm.GLM(Y_train, X_train , family=sm.families.Binomial()).fit()
print(nbm.summary())
from statsmodels.stats.outliers_influence import variance_inflation_factor
def VIF(input_data, dependent_col):
    X = input_data.drop([dependent_col], axis=1)
    # For each X, calculate VIF and save in dataframe
    vif = pd.DataFrame(columns = ['Var', 'Vif'])
    vif["Var"] = X.columns
    vif["Vif"] = [variance_inflation_factor(X.values, i) for i in range(X.shape[1])]
    return vif.sort_values(by = 'Vif', axis=0, ascending=False, inplace=False).round(2)
VIF(df,'class')
from sklearn.naive_bayes import GaussianNB
nb = GaussianNB()
nb.fit(X_train,Y_train)
X_test = sm.add_constant(X_test,has_constant= 'add')
y_pred = nb.predict(X_test)<jupyter_output><empty_output><jupyter_text>Prediction<jupyter_code>from sklearn.metrics import classification_report , confusion_matrix , accuracy_score
cm = confusion_matrix(Y_test,y_pred)
print(cm)
classification_report(Y_test,y_pred)
accuracy_score(Y_test , y_pred)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12760952660982064, 'fraction_numerical': 0.018817994707438987, 'mean_word_length': 6.162105263157895, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '394539', 'license': 'MIT', 'n_tokens_mistral': 1349, 'n_tokens_neox': 1307, 'n_words': 201, 'path': 'MushroomClassifier.ipynb', 'repo_name': 'Sparsh-Bansal/MushroomClassifier_NaiveBayes-', 'size': '3401'}