-
Notifications
You must be signed in to change notification settings - Fork 0
/
naivebayes_test.py
executable file
·84 lines (64 loc) · 2.68 KB
/
naivebayes_test.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
74
75
76
77
78
79
80
81
82
83
84
import math
import unittest
import numpy as np
from sklearn.naive_bayes import GaussianNB
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import KBinsDiscretizer
from sklearn.metrics import accuracy_score
from sklearn.dummy import DummyClassifier
import naivebayes
class TestNaiveBayesClassifier(unittest.TestCase):
def test_wine_dummy(self):
X, y = datasets.load_wine(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2)
c = DummyClassifier(strategy='uniform')
c.fit(X_train, y_train)
y_pred = c.predict(X_test)
print("Dummy Wine Accuracy: ", accuracy_score(y_test, y_pred))
def test_wine_gaussian(self):
X, y = datasets.load_wine(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2)
c = GaussianNB()
c.fit(X_train, y_train)
y_pred = c.predict(X_test)
print("GaussianNB Wine Accuracy: ", accuracy_score(y_test, y_pred))
def test_wine_probabilistic(self):
X, y = datasets.load_wine(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2)
t = KBinsDiscretizer(n_bins=3, encode='onehot-dense', strategy='kmeans')
X_train = t.fit_transform(X_train)
X_test = t.transform(X_test)
c = naivebayes.NaiveBayesClassifer()
c.fit(X_train, y_train)
y_pred = c.predict(X_test)
print("Wine Accuracy: ", accuracy_score(y_test, y_pred))
def test_digits_dummy(self):
X, y = datasets.load_digits(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2)
c = DummyClassifier(strategy='uniform')
c.fit(X_train, y_train)
y_pred = c.predict(X_test)
print("Dummy Digits Accuracy: ", accuracy_score(y_test, y_pred))
def test_digits_gaussian(self):
X, y = datasets.load_digits(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2)
c = GaussianNB()
c.fit(X_train, y_train)
y_pred = c.predict(X_test)
print("GaussianNB Digits Accuracy: ", accuracy_score(y_test, y_pred))
def test_digits(self):
X, y = datasets.load_digits(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2)
c = naivebayes.NaiveBayesClassifer()
c.fit(X_train, y_train)
y_pred = c.predict(X_test)
print("Digits Accuracy: ", accuracy_score(y_test, y_pred))
if __name__ == '__main__':
unittest.main()