-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImprovedKNNForest.py
47 lines (35 loc) · 1.79 KB
/
ImprovedKNNForest.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
"""
ImprovedKNNForest Algorithm
"""
from KNNForest import KNNForest, BEST_N, BEST_P, BEST_K
from utils import Examples, TRAIN_PATH, TEST_PATH, CommitteeWrapper, euclidean_distance, insort
BEST_AREA_LENGTH = 26.1
BEST_VOTE_WEIGHT = 2
""""""""""""""""""""""""""""""""""""""""""" ImprovedKNNForest """""""""""""""""""""""""""""""""""""""""""
class ImprovedKNNForest(KNNForest):
def __init__(self, train_path: str, area_length: float = BEST_AREA_LENGTH, vote_weight: int = BEST_VOTE_WEIGHT,
num_trees: int = BEST_N, p: float = BEST_P, num_chosen_trees: int = BEST_K):
super().__init__(train_path, num_trees, p, num_chosen_trees)
self._area_length, self._vote_weight = area_length, vote_weight
def classify_one(self, test_example: Examples) -> int:
committee = []
for tree in self._forest:
insort(committee, CommitteeWrapper(tree.classifier, euclidean_distance(tree.centroid, test_example[1:])))
votes_num, vote_for, vote_against, distance_range, vote_weight = 0, 0, 0, 1, self._vote_weight
for classifier in committee:
if votes_num >= self._num_chosen_trees:
break
if classifier.distance > distance_range:
vote_weight /= self._vote_weight
distance_range = classifier.distance + self._area_length
if classifier.classification_or_classifier.classify_one(test_example) == 1:
vote_for += vote_weight
else:
vote_against += vote_weight
votes_num += 1
return 1 if vote_for >= vote_against else 0
""""""""""""""""""""""""""""""""""""""""""" Main """""""""""""""""""""""""""""""""""""""""""
def main():
print(ImprovedKNNForest(TRAIN_PATH).classify(TEST_PATH))
if __name__ == '__main__':
main()