-
Notifications
You must be signed in to change notification settings - Fork 3
/
nbc.py
73 lines (49 loc) · 1.54 KB
/
nbc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sb
from pylab import rcParams
import urllib
import sklearn
from sklearn.naive_bayes import BernoulliNB
from sklearn.naive_bayes import GaussianNB
from sklearn.naive_bayes import MultinomialNB
from sklearn.model_selection import train_test_split
from sklearn import metrics
from sklearn.metrics import accuracy_score
# from UC IRVINE data
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]) # huge matrix.
X = dataset[:,0:48]
y = dataset[:,-1]
# test/training set
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=.33, random_state=17)
# Try bernoulli method, change frequencies to binaries
BernNB = BernoulliNB(binarize=True)
# FIT
BernNB.fit(X_train,y_train)
y_expect = y_test
y_pred = BernNB.predict(X_test)
print(accuracy_score(y_expect, y_pred))
# .855 prediction score.
# ---- Multinomial Naive Bayes ---- #
MultiNB = MultinomialNB()
MultiNB.fit(X_train, y_train)
y_pred = MultiNB.predict(X_test)
accuracy_score(y_expect, y_pred)
# .877 accuracy score.
#--- Guassian ---#
GausNB = GaussianNB()
GausNB.fit(X_train, y_train)
y_pred = GausNB.predict(X_test)
accuracy_score(y_expect, y_pred)
# 81% accurate
## ----- Binarize value to 0.1
BernNB = BernoulliNB(binarize=0.1)
# FIT
BernNB.fit(X_train,y_train)
y_expect = y_test
y_pred = BernNB.predict(X_test)
print(accuracy_score(y_expect, y_pred)) # 89.5 % accuracy.