-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweak_voting.R
More file actions
51 lines (46 loc) · 1.25 KB
/
Copy pathweak_voting.R
File metadata and controls
51 lines (46 loc) · 1.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
#referred to weak_voting.r
# demonstration of what happens when we vote using a collection of indpendent classifiers, only slighly better
# than random guessing.
M = 100; # number of indepenent classifiers (odd to avoid tie)
N = 1000; # trials
C = rep(0,M)
error = 0
for (n in 1:N) {
trueclass = sample(c(0,1),1);
if (trueclass == 1) { p = c(.45,.55); }
else { p = c(.55,0.45); }
C = sample(c(0,1),M,replace=T,prob=p);
est = ifelse (sum(C) > M/2,1,0);
if (est != trueclass) error = error+1
}
print("error rate:")
print(error/N)
C = rep(0,M)
#class =C(0,1,2)
error = 0
for (n in 1:N) {
trueclass = sample(c(0,1,2),1);
if (trueclass == 0) {
p = c((1/3)+0.1, (1/3)-0.005, (1/3)-0.005); }
else if (trueclass == 1) {
p = c((1/3)-0.005, (1/3)+0.1, (1/3)-0.005); }
else {
p= c((1/3)-0.005, (1/3)-0.005, (1/3)+0.1);}
C = sample(c(0,1,2),M,replace=T,prob=p);
sum0=length(C[which(C==0)])
sum1= sum(C[which(C==1)])
sum2= sum(C[which(C==2)])/2
if(sum0 >= sum1 & sum0>= sum2)
{
est = 0
} else if(sum1> sum2 & sum1> sum0){
est=1
} else {
est=2
}
#print(est)
#print(trueclass)
if (est != trueclass) error = error+1
}
print("error rate:")
print(error/N)