<jupyter_start><jupyter_text>![title](Header__0002_8.png)___ Chapter 8 - Basic Algorithmic Learning Segment 3 - Naive Bayes Classifiers<jupyter_code>import numpy as np
import pandas as pd

import urllib

import sklearn
from sklearn.naive_bayes import BernoulliNB
from sklearn.naive_bayes import GaussianNB
from sklearn.naive_bayes import MultinomialNB
from sklearn.cross_validation import train_test_split
from sklearn import metrics
from sklearn.metrics import accuracy_score<jupyter_output><empty_output><jupyter_text>Naive Bayes Using Naive Bayes to predict spam<jupyter_code>url = "https://archive.ics.uci.edu/ml/machine-learning-databases/spambase/spambase.data"
raw_data = urllib.urlopen(url)
dataset = np.loadtxt(raw_data, delimiter=",")
print dataset[0]
X = dataset[:,0:48]

y = dataset[:, -1]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.33, random_state=17)
BernNB = BernoulliNB(binarize=True)
BernNB.fit(X_train, y_train)
print(BernNB)

y_expect = y_test
y_pred = BernNB.predict(X_test)
print accuracy_score(y_expect, y_pred)
MultiNB = MultinomialNB()

MultiNB.fit(X_train, y_train)
print(MultiNB)

y_pred = MultiNB.predict(X_test)
print accuracy_score(y_expect, y_pred)
GausNB = GaussianNB()
GausNB.fit(X_train, y_train)
print(GausNB)

y_pred = GausNB.predict(X_test)
print accuracy_score(y_expect, y_pred)
BernNB = BernoulliNB(binarize=0.1)
BernNB.fit(X_train, y_train)
print(BernNB)

y_expect = y_test
y_pred = BernNB.predict(X_test)
print accuracy_score(y_expect, y_pred)<jupyter_output>BernoulliNB(alpha=1.0, binarize=0.1, class_prior=None, fit_prior=True)
0.895325872284
{'fraction_non_alphanumeric': 0.10242085661080075, 'fraction_numerical': 0.021725636250775917, 'mean_word_length': 7.760869565217392, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 1, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '205704', 'license': 'MIT', 'n_tokens_mistral': 681, 'n_tokens_neox': 626, 'n_words': 122, 'path': 'Ch08/08_03/08_03.ipynb', 'repo_name': 'desaiankitb/Python-for-Data-Science-Essential', 'size': '1611'}