-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMajorityClassification.java
More file actions
51 lines (45 loc) · 1.35 KB
/
MajorityClassification.java
File metadata and controls
51 lines (45 loc) · 1.35 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
package cs475;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Iterator;
import java.math.*;
public class MajorityClassification extends Predictor {
private ClassificationLabel predictLabel = new ClassificationLabel(0);
private String preName = "majority";
public void train(List<Instance> instances){
int preLabel =0;
int counter1 = 0 ;
int counter0 = 0;
Iterator<Instance> it = instances.iterator();
while(it.hasNext()){
Instance i = it.next();
ClassificationLabel label = (ClassificationLabel)i.getLabel();
int labelvalue = label.getLabelValue();
if(labelvalue == 1)
{counter1++;}
else
{counter0++;}
}
if (counter1 > counter0)
{preLabel=1;}
if (counter1 < counter0)
{preLabel=0;}
if (counter1 == counter0)
{preLabel = (int)Math.random()>0.5?1:0;}
this.predictLabel = new ClassificationLabel(preLabel);
}
public ClassificationLabel predict(Instance instance){
instance.setLabel(predictLabel);
return predictLabel;
}
public ClassificationLabel getLabel(){
return this.predictLabel;
}
public String getpreName(){
return this.preName;
}
}