-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogisticTrain.java
More file actions
232 lines (182 loc) · 8.48 KB
/
LogisticTrain.java
File metadata and controls
232 lines (182 loc) · 8.48 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package LogisticRegression;
import java.util.*;
public class LogisticTrain {
double weightVector[];
double gradientVector[];
private double learningRate;
double lastWeightVector[];
HashMap<String, ArrayList<Double>> modelsMap;
double previousCost;
public LogisticTrain(int length, double learningRate, Data data) {
weightVector = new double[length];
gradientVector = new double[length];
lastWeightVector = new double[length];
this.learningRate = learningRate;
modelsMap = new HashMap<String, ArrayList<Double>>();
}
public void train(ArrayList<Example> dataSet, String label) {
System.out.println("Learning Rate = " + learningRate);
initializeVectorToZERO(gradientVector);
initializeVectorToZERO(weightVector);
int iter = 1;
while (true) {
initializeVectorToZERO(gradientVector);
storeLastWeightVector(lastWeightVector, weightVector);
double cost = 0.0;
for (Example trainingExample : dataSet) {
double costForInstance;
double probabilityPerExample = calculateSigmoid(trainingExample.getValues());
costForInstance = calculateCostForInstance(probabilityPerExample, trainingExample.getLabel());
cost = cost + costForInstance;
double error = trainingExample.getLabel() - probabilityPerExample;
for (int i = 0; i < trainingExample.getValues().length; i++) {
gradientVector[i] += error * trainingExample.values[i];
}
}
cost = cost / dataSet.size();
System.out.println(iter + " " + cost);
updateWeightVector();
double diff = getCostDifference(previousCost, cost);
if (Double.isNaN(cost) || (diff <= 0.00001)) {
break;
}
iter++;
previousCost = cost;
}
ArrayList<Double> list = createList(weightVector);
modelsMap.put(label, list);
initializeVectorToZERO(weightVector);
}
private ArrayList<Double> createList(double[] weightVector) {
ArrayList<Double> list = new ArrayList<Double>();
for (double v : weightVector) {
list.add(v);
}
return list;
}
private double calculateCostForInstance(double probabilityPerExample, int label) {
double penalty = ((-label * Math.log(probabilityPerExample)) - ((1 - label) * Math.log(1 - probabilityPerExample)));
return penalty;
}
private void storeLastWeightVector(double[] lastWeightVector, double[] weightVector) {
for (int i = 0; i < weightVector.length; i++) {
lastWeightVector[i] = weightVector[i];
}
}
private boolean calculateDifference(double[] weightVector, double[] lastWeightVector) {
int count = 0;
for (int i = 0; i < weightVector.length; i++) {
// System.out.println(Math.abs(weightVector[i] - lastWeightVector[i]));
if (Math.abs(weightVector[i] - lastWeightVector[i]) < 0.01) {
count++;
}
}
return (count == weightVector.length * 0.8);
}
private void printVector(int i, double[] weightVector) {
System.out.print("weight => " + i + " ");
for (double aWeightVector : weightVector) {
System.out.print(aWeightVector + ",");
}
System.out.println();
}
private double getCostDifference(double previousCost, double currentCost) {
return Math.abs(currentCost - previousCost);
}
private void updateWeightVector() {
for (int i = 0; i < weightVector.length; i++) {
weightVector[i] += learningRate * gradientVector[i];
}
}
private void initializeVectorToZERO(double[] vector) {
for (int i = 0; i < vector.length; i++) {
vector[i] = 0.0;
}
}
private double calculateSigmoid(double[] values) {
double dotProduct = 0;
for (int i = 0; i < weightVector.length; i++) {
dotProduct += values[i] * weightVector[i];
}
return 1 / (1 + Math.exp(-dotProduct));
}
public double classify(ArrayList<Example> testDataSet, HashMap<String, ArrayList<Double>> model) {
double truePositive = 0;
double trueNegative = 0;
double falsePositive = 0;
double falseNegative = 0;
int count = 0;
for (Example example : testDataSet) {
Map<String, Double> predictionMap = new HashMap<String, Double>();
for (String s : modelsMap.keySet()) {
ArrayList<Double> modelWeights = modelsMap.get(s);
double prediction = predictValue(example.getValues(), modelWeights);
predictionMap.put(s, prediction);
}
Map<String, Double> highestMap = getMax(predictionMap);
Set<String> keys = highestMap.keySet();
for (String key : keys) {
example.setPredictedLabel(key);
}
if (example.predictedLabel.equals(example.actualLabel)) {
count++;
}
System.out.println(example.actualLabel + " " + example.getPredictedLabel());
}
return ((double) count) / testDataSet.size() * 100;
// calculateAccuracy(truePositive, falsePositive, trueNegative, falseNegative);
// createConfusionMatrix(truePositive, falsePositive, trueNegative, falseNegative);
}
private Map<String, Double> getMax(Map<String, Double> predictionMap) {
Map<String, Double> map = new HashMap<String, Double>();
String key = "";
double max = -Double.MIN_VALUE;
for (String s : predictionMap.keySet()) {
if (predictionMap.get(s) > max) {
max = predictionMap.get(s);
key = s;
}
}
map.put(key, max);
return map;
}
private double predictValue(double[] values, ArrayList<Double> modelWeights) {
double dotProduct = 0;
for (int i = 0; i < modelWeights.size(); i++) {
dotProduct += values[i] * modelWeights.get(i);
}
return 1 / (1 + Math.exp(-dotProduct));
}
private void calculateAccuracy(double truePositive, double falsePositive, double trueNegative, double falseNegative) {
double accuracy = (truePositive + trueNegative) / (truePositive + trueNegative + falsePositive + falseNegative) * 100;
System.out.println("Accuracy = " + accuracy);
}
private void createConfusionMatrix(double truePositive, double falsePositive, double trueNegative, double falseNegative) {
// printVector(i, weightVector);
System.out.println("Confusion Matrix ");
System.out.println("--------------------------------------------------");
System.out.println(" Actual ");
System.out.println("--------------------------------------------------");
System.out.println(" 0 | 1 ");
System.out.println("--------------------------------------------------");
System.out.println("| P | | | ");
System.out.println("| R | | | ");
System.out.println("| E | 0 | " + trueNegative + " | " + falseNegative + " ");
System.out.println("| D | | | ");
System.out.println("| I |---|-----------------------------------------");
System.out.println("| C | | | ");
System.out.println("| T | | | ");
System.out.println("| E | 1 | " + falsePositive + " | " + truePositive + " ");
System.out.println("| D | | | ");
}
public void printModels() {
for (String s : modelsMap.keySet()) {
System.out.println(s + " =>");
ArrayList<Double> weights = modelsMap.get(s);
for (Double weight : weights) {
System.out.print(weight + ",");
}
System.out.println("");
}
}
}