-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcls_bayes_least_risk.h
executable file
·81 lines (68 loc) · 1.58 KB
/
cls_bayes_least_risk.h
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
#include "lib_import.h"
class cls_bayes_least_risk
:public cls_bayes
{
protected:
cls_matrix<double> & mat_loss;
public:
cls_bayes_least_risk(int num_pat,int num_feature,cls_matrix<double> & a_lose)
:cls_bayes(num_pat,num_feature),
mat_loss(a_lose)
{
for(int cnt=0;cnt<num_pat;cnt++)
{
add_unit();
}
}
virtual void add_unit()
{
units.push_back(new cls_bayes_unit_least_error(get_feature_num()));
}
virtual int get_output_index( double* input )
{
vector<double> re;
for(int cnt=0;cnt<num_feature;cnt++)
{
re.push_back(input[cnt]);
}
return get_output_index(re);
}
virtual int get_output_index(const vector<double>& input )
{
vector<double> d_pw(num_pat);
vector<double> d_pw_loss(num_pat);
#ifdef __USING_GCC__
typename vector<cls_bayes_unit*>::iterator itor;
typename vector<double>::iterator itor_pat;
#else
vector<cls_bayes_unit*>::iterator itor;
vector<double>::iterator itor_pat;
#endif
for(itor=units.begin(),itor_pat=d_pw.begin()
;itor_pat!=d_pw.end() && itor!=units.end();
itor_pat++,itor++)
{
*itor_pat = (*itor)->get_output(input);
}
for(int cnt=0;cnt<num_pat;cnt++)
{
d_pw_loss[cnt] = 0.0;
for(int i=0;i<num_pat;i++)
{
d_pw_loss[cnt] += d_pw[i] * mat_loss(cnt,i);
}
}
double max_val = -1;
int max_indx = -1;
int cnt = 0;
for(itor_pat=d_pw_loss.begin();itor_pat!=d_pw_loss.end();itor_pat++,cnt++)
{
if (max_val < *itor_pat)
{
max_val = *itor_pat;
max_indx = cnt;
}
}
return max_indx;
}
};