-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_unit.py
138 lines (106 loc) · 4.73 KB
/
test_unit.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import unittest
import os
import csv
from utils import *
from ID3 import *
from KNN import *
from CostSensitiveKNN import *
from KNNForest import *
from ImprovedKNNForest import *
""""""""""""""""""""""""""""""""""""""" Useful Methods """""""""""""""""""""""""""""""""""""""
def create_num_test(num_examples: int, num_features: int, new_path: str = "try"):
temp_path = "./test_csv/temp.csv"
actual_path = "./test_csv/" + new_path + ".csv"
df = pd.DataFrame(
[["M" if randint(0, 1) == 1 else "B"] + [randint(0, 100) / randint(1, 100) for _ in range(num_features)] for _
in range(num_examples)])
df.to_csv(temp_path)
row_count = 0
with open(temp_path, "r") as source:
reader = csv.reader(source)
with open(actual_path, "w", newline='') as result:
writer = csv.writer(result)
for row in reader:
row_count += 1
for col_index in [0]:
del row[col_index]
writer.writerow(row)
os.remove(temp_path)
return actual_path
""""""""""""""""""""""""""""""""""""""""" Tests """""""""""""""""""""""""""""""""""""""""
class TestID3(unittest.TestCase):
def test_continuous(self):
test_path = "./test_csv/continuous.csv"
self.assertTrue(ID3ContinuousFeatures(test_path).classify(test_path) == 1)
def test_medium_continuous(self):
test_path = "./test_csv/medium_continuous.csv"
self.assertTrue(ID3ContinuousFeatures(test_path).classify(test_path) == 1)
def test_randomly(self):
for _ in range(20):
test_path = create_num_test(100, 100)
self.assertTrue(ID3ContinuousFeatures(test_path).classify(test_path) > 0.95)
os.remove("./test_csv/try.csv")
def test_final_answer(self):
self.assertTrue(ID3ContinuousFeatures(TRAIN_PATH).classify(TEST_PATH) == 0.9469026548672567)
class TestKNN(unittest.TestCase):
def test_continuous(self):
test_path = "./test_csv/continuous.csv"
self.assertTrue(KNN(test_path).classify(test_path) == 1)
self.assertFalse(KNN(test_path).classify_and_get_loss(test_path))
def test_medium_continuous(self):
test_path = "./test_csv/medium_continuous.csv"
self.assertTrue(KNN(test_path).classify(test_path) == 1)
self.assertFalse(KNN(test_path).classify_and_get_loss(test_path))
def test_randomly(self):
for _ in range(10):
test_path = create_num_test(100, 100)
self.assertTrue(KNN(test_path).classify(test_path) == 1)
self.assertFalse(KNN(test_path).classify_and_get_loss(test_path))
os.remove("./test_csv/try.csv")
def test_final_answer(self):
self.assertTrue(KNN(TRAIN_PATH).classify(TEST_PATH) == 0.9646017699115044)
self.assertFalse(KNN(TRAIN_PATH).classify_and_get_loss(TRAIN_PATH))
self.assertFalse(KNN(TEST_PATH).classify_and_get_loss(TEST_PATH))
class TestCostSensitiveKNN(unittest.TestCase):
def test_final_answer(self):
self.assertTrue(CostSensitiveKNN(TRAIN_PATH).classify(TEST_PATH) == 0.003539823008849558)
class TestKNNForest(unittest.TestCase):
def test_continuous(self):
train_path = "./test_csv/continuous.csv"
i = 0
for _ in range(1000):
a = KNNForest(train_path, 1, 1, 1).classify(train_path)
if a != 1:
i += 1
self.assertFalse(i)
def test_final_answer(self):
self.assertTrue(KNNForest(TRAIN_PATH, 1, 1, 1).classify(TEST_PATH) == 0.9469026548672567)
def test_randomly_simple(self):
path = create_num_test(100, 100)
self.assertTrue(KNNForest(path, 1, 1, 1).classify(path) == 1)
os.remove(path)
def test_randomly_complex(self):
path = create_num_test(100, 100)
self.assertTrue(KNNForest(path, 4, randint(3, 8) / 10, 2).classify(path))
os.remove(path)
class TestImprovedKNNForest(unittest.TestCase):
def test_continuous(self):
train_path = "./test_csv/continuous.csv"
i = 0
for _ in range(1000):
a = ImprovedKNNForest(train_path, 1, 1, 1, 1, 1).classify(train_path)
if a != 1:
i += 1
self.assertFalse(i)
def test_final_answer(self):
self.assertTrue(ImprovedKNNForest(TRAIN_PATH, 1, 1, 1, 1, 1).classify(TEST_PATH) == 0.9469026548672567)
def test_randomly_simple(self):
path = create_num_test(100, 100)
self.assertTrue(ImprovedKNNForest(path, 1, 1, 1, 1, 1).classify(path) == 1)
os.remove(path)
def test_randomly_complex(self):
path = create_num_test(100, 100)
self.assertTrue(ImprovedKNNForest(path, 3, 8, 4, randint(3, 8) / 10, 2).classify(path))
os.remove(path)
if __name__ == '__main__':
unittest.main()