-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassify.h
More file actions
110 lines (101 loc) · 3.25 KB
/
Copy pathclassify.h
File metadata and controls
110 lines (101 loc) · 3.25 KB
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
#ifndef CLASSIFY
#define CLASSIFY
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include "attributes_structure.h"
#include "tree_structure.h"
//function prototypes
void classifyInstances(TreeNode* root, vector<Attribute>& attributes, vector<vector<string>>& test);
void classifyInstances(TreeNode* root, vector<Attribute>& attributes, vector<vector<string>>& test, int treeindex);
void classifyInstances(TreeNode *root, vector<Attribute>& attributes, vector<vector<string>>& test, ABTree& Tree);
char evaluate(vector<string>& instance, TreeNode* node, vector<Attribute>& attributes);
//function to classify all unknown instances and subsequently calculate the efficiency
void classifyInstances(TreeNode* root, vector<Attribute>& attributes, vector<vector<string>>& test)
{
//classify each individual instance
for (int i = 0; i < test.size(); i++)
{
test[i][15] = output_map_ctos[evaluate(test[i], root, attributes)];
}
//calculate efficiency
float p = 0.0;
for (int i = 0; i < test.size(); i++)
{
if (output_map[test[i][14]] == output_map[test[i][15]])
{
p++;
}
}
cout << "Accuracy : " << (p / test.size()) * 100.0 << "%" << "\n";
}
// overloaded classifyIntances function for random forest algorithm
void classifyInstances(TreeNode* root, vector<Attribute>& attributes, vector<vector<string>>& test, int treeindex)
{
//classify each individual instance
for (int i = 0; i < test.size(); i++)
{
test[i][15 + treeindex] = output_map_ctos[evaluate(test[i], root, attributes)];
}
//calculate efficiency
float p = 0.0;
for (int i = 0; i < test.size(); i++)
{
if (output_map[test[i][14]] == output_map[test[i][15 + treeindex]])
{
p++;
}
}
cout << "Accuracy of Tree " << treeindex + 1 << " : " << (p / test.size()) * 100.0 << "%" << "\n";
}
// overloaded classifyIntances function for adaboost algorithm
void classifyInstances(TreeNode *root, vector<Attribute>& attributes, vector<vector<string>>& test, ABTree& Tree)
{
//classify each individual instance
for (int i = 0; i < test.size(); i++)
{
test[i][15 + Tree.index] = output_map_ctos[evaluate(test[i], root, attributes)];
}
//calculate efficiency
float p = 0.0;
for (int i = 0; i < test.size(); i++)
{
if (output_map[test[i][14]] == output_map[test[i][15+Tree.index]])
{
p++;
}
}
float n = test.size() - p;
Tree.accuracy = (p / test.size())*100.0;
cout << "Accuracy of Tree " << Tree.index+1 << " : " << (p / test.size())*100.0 << "%" << "\n";
float e = (n / test.size());
Tree.alpha = (0.5)*log((1.0 - e) / e);
if (Tree.accuracy < 95.0)
{
cout << "Alpha of " << "Tree " << Tree.index + 1 << " : " << Tree.alpha << "\n";
}
}
//evaluates an instance as '+' or '-' and returns its evaluation
char evaluate(vector<string>& instance, TreeNode* node, vector<Attribute>& attributes)
{
if (node->branch.empty())
{
return node->label;
}
else
{
char c;
Attribute attr = attributes[attributes_map_ctoi[node->label]];
for (int i = 0; i < node->branch.size(); i++)
{
if (attr.map[instance[attr.index]] == node->branch[i]->label)
{
c = evaluate(instance, node->branch[i]->child, attributes);
return c;
}
}
}
}
#endif