<jupyter_start><jupyter_text>Naive Bayes Algorithm When dealing with continuous data, a typical assumption is that the continuous values associated with each class are distributed according to a Normal distribution. For example, suppose the training data contains a continuous attribute, $x$. We first segment the data by the class, and then compute the mean and variance of $x$ in each class. Let $\mu_c$ be the mean of the values in $x$ associated with class c, and let $\sigma^2_c$ be the variance of the values in $x$ associated with class c. Suppose we have collected some observation value $v$. Then, the probability distribution of $v$ given a class $c$, $p(x=v \mid c)$, can be computed by plugging $v$ into the equation for a Normal distribution parameterized by $\mu_c$ and $\sigma^2_c$. That is,$$p(x=v \mid c)=\frac{1}{\sqrt{2\pi\sigma^2_c}}\,e^{ -\frac{(v-\mu_c)^2}{2\sigma^2_c} }$$Another common technique for handling continuous values is to use binning  the feature values, to obtain a new set of Bernoulli-distributed features; some literature in fact suggests that this is necessary to apply naive Bayes, but it is not, and the discretization may throw away discriminative information<jupyter_code>%matplotlib inline 

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from scipy import stats
import seaborn as sns
import matplotlib.pyplot as plt
import warnings
import random
from sklearn.cross_validation import KFold, cross_val_score, train_test_split
from sklearn import metrics
import statsmodels.api as sm
import scipy.stats as stats 
from sklearn.linear_model import LogisticRegression


from datetime import datetime
random.seed(datetime.now())
warnings.filterwarnings('ignore')

# Make plots larger
plt.rcParams['figure.figsize'] = (10, 6)


import pandas as pd
voice=pd.read_csv("F://ADS//Assignment//voice-data.csv",encoding='latin-1')
voice.head()
voice1 = voice.rename(columns={'label': 'gender'})
voice1.columns.values
def genderToInt(gender):
    if gender == 'male':
        return 0
    else:
        return 1
    
voice1.gender = voice1.gender.apply(genderToInt)
print(voice.head())
print(voice.tail())
X = voice1.drop('gender', axis=1)
y = voice1.gender
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)
from sklearn.naive_bayes import MultinomialNB, GaussianNB
mnb = MultinomialNB()
mnb.fit(X_train, y_train)
y_pred_class = mnb.predict(X_test)
print (metrics.accuracy_score(y_test, y_pred_class))
# testing accuracy of Gaussian Naive Bayes
gnb = GaussianNB()
gnb.fit(X_train, y_train)
y_pred_class = gnb.predict(X_test)
print (metrics.accuracy_score(y_test, y_pred_class))<jupyter_output>0.876262626263
{'fraction_non_alphanumeric': 0.09385474860335195, 'fraction_numerical': 0.01303538175046555, 'mean_word_length': 5.395238095238096, 'pattern_counts': {'":': 0, '<': 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': '392862', 'license': 'MIT', 'n_tokens_mistral': 852, 'n_tokens_neox': 823, 'n_words': 306, 'path': 'Notebook 9 - Naive Bayes Algorithm.ipynb', 'repo_name': 'balaji-mudaliyar/balaji-mudaliyar', 'size': '2685'}