Repository navigation
Expand file tree
/
Copy pathBoostingAlgorithm.java
More file actions
146 lines (118 loc) · 5.03 KB
/
Copy pathBoostingAlgorithm.java
File metadata and controls
146 lines (118 loc) · 5.03 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import edu.princeton.cs.algs4.Point2D;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.Stopwatch;
import java.util.LinkedList;
public class BoostingAlgorithm {
private double[] weights; // double array of corresponding weights
private int[][] reducedInput; // 2D int array of reduced dimensions
private int[] labels; // int array of signs
private LinkedList<WeakLearner> list; // list to store WeakLearner objects
private Clustering cluster; // cluster object
// create the clusters and initialize your data structures
public BoostingAlgorithm(int[][] input, int[] labels, Point2D[] locations, int k) {
if (input == null || locations == null || labels == null) {
throw new IllegalArgumentException();
}
int m = locations.length;
int n = input.length;
if ((input[0].length != m) || (input.length != labels.length)) {
throw new IllegalArgumentException();
}
if (k < 1 || k > m) throw new IllegalArgumentException();
for (int i = 0; i < labels.length; i++) {
if (labels[i] != 0 && labels[i] != 1) {
throw new IllegalArgumentException();
}
}
weights = new double[n];
this.labels = labels.clone();
list = new LinkedList<>();
reducedInput = new int[n][k];
// normalize weight array so sum of elements equals 1
for (int i = 0; i < n; i++) {
weights[i] = 1.0 / n;
}
cluster = new Clustering(locations, k);
// populate reduced dimension array with reduced dimensions of input
for (int i = 0; i < input.length; i++) {
reducedInput[i] = cluster.reduceDimensions(input[i]);
}
}
// return the current weight of the ith point
public double weightOf(int i) {
return weights[i];
}
// apply one step of the boosting algorithm
public void iterate() {
double totalWeight = 0;
// create a WeakLearner object using current weights and input
WeakLearner weaklearner = new WeakLearner(reducedInput, weights, labels);
// double weight if weak learner mislabels it
for (int i = 0; i < weights.length; i++) {
if (weaklearner.predict(reducedInput[i]) != labels[i]) {
weights[i] *= 2;
}
// update total weight
totalWeight += weights[i];
}
// renormalize weight array to 1 again
for (int j = 0; j < weights.length; j++) {
weights[j] /= totalWeight;
}
// add WeakLearner object to linked list
list.add(weaklearner);
}
// return the prediction of the learner for a new sample
public int predict(int[] sample) {
if (sample == null) throw new IllegalArgumentException();
// keeps track of 0 and 1 counts
int zeroCount = 0;
int oneCount = 0;
// develop clusters of sample array
int[] reducedSample = cluster.reduceDimensions(sample);
// update the counts of 1 and 0 depending on their frequency
for (WeakLearner x : list) {
if (x.predict(reducedSample) == 0) zeroCount++;
else oneCount++;
}
// output the label that was predicted the most
if (oneCount > zeroCount) return 1;
else if (zeroCount > oneCount) return 0;
else return 0;
}
// unit testing (required)
public static void main(String[] args) {
// read in the terms from a file
DataSet training = new DataSet(args[0]);
DataSet testing = new DataSet(args[1]);
int k = Integer.parseInt(args[2]);
int x = Integer.parseInt(args[3]);
int[][] trainingInput = training.getInput();
int[][] testingInput = testing.getInput();
int[] trainingLabels = training.getLabels();
int[] testingLabels = testing.getLabels();
Point2D[] trainingLocations = training.getLocations();
Stopwatch watch = new Stopwatch();
// train the model
BoostingAlgorithm model = new BoostingAlgorithm(trainingInput, trainingLabels,
trainingLocations, k);
for (int t = 0; t < x; t++)
model.iterate();
// calculate the training data set accuracy
double trainingAccuracy = 0;
for (int i = 0; i < training.getN(); i++)
if (model.predict(trainingInput[i]) == trainingLabels[i])
trainingAccuracy += 1;
trainingAccuracy /= training.getN();
// calculate the test data set accuracy
double testAccuracy = 0;
for (int i = 0; i < testing.getN(); i++)
if (model.predict(testingInput[i]) == testingLabels[i])
testAccuracy += 1;
testAccuracy /= testing.getN();
StdOut.println("time:" + watch.elapsedTime());
StdOut.println("Training accuracy of model: " + trainingAccuracy);
StdOut.println("Test accuracy of model: " + testAccuracy);
StdOut.println(model.weightOf(1));
}
}