-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCost.java
More file actions
28 lines (20 loc) · 771 Bytes
/
Copy pathCost.java
File metadata and controls
28 lines (20 loc) · 771 Bytes
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
package eu.redzoo.ml;
import java.util.List;
import java.util.function.Function;
public class Cost {
public static double cost(Function<Double[], Double> targetFunction, List<Double[]> dataset, List<Double> labels) {
int m = dataset.size();
double sumSquaredErrors = 0;
for (int i = 0; i < m; i++) {
// get the next feature vector
Double[] featureVector = dataset.get(i);
// compute the "gap"
double predicted = targetFunction.apply(featureVector);
double label = labels.get(i);
double gap = predicted - label;
// add it to the sum
sumSquaredErrors += Math.pow(gap, 2);
}
return (1.0 / (2 * m)) * sumSquaredErrors;
}
}