use of org.apache.ignite.ml.selection.scoring.evaluator.EvaluationResult in project ignite by apache.
the class BinaryClassificationMetricsTest method testCalculation.
/**
*/
@Test
public void testCalculation() {
Map<Vector, Double> xorset = new HashMap<Vector, Double>() {
{
put(VectorUtils.of(0., 0.), 0.);
put(VectorUtils.of(0., 1.), 1.);
put(VectorUtils.of(1., 0.), 1.);
put(VectorUtils.of(1., 1.), 0.);
}
};
IgniteModel<Vector, Double> xorFunction = v -> {
if (Math.abs(v.get(0) - v.get(1)) < 0.01)
return 0.;
else
return 1.;
};
IgniteModel<Vector, Double> andFunction = v -> {
if (Math.abs(v.get(0) - v.get(1)) < 0.01 && v.get(0) > 0)
return 1.;
else
return 0.;
};
IgniteModel<Vector, Double> orFunction = v -> {
if (v.get(0) > 0 || v.get(1) > 0)
return 1.;
else
return 0.;
};
EvaluationResult xorResult = Evaluator.evaluateBinaryClassification(xorset, xorFunction, Vector::labeled);
assertEquals(1., xorResult.get(MetricName.ACCURACY), 0.01);
assertEquals(1., xorResult.get(MetricName.PRECISION), 0.01);
assertEquals(1., xorResult.get(MetricName.RECALL), 0.01);
assertEquals(1., xorResult.get(MetricName.F_MEASURE), 0.01);
EvaluationResult andResult = Evaluator.evaluateBinaryClassification(xorset, andFunction, Vector::labeled);
assertEquals(0.25, andResult.get(MetricName.ACCURACY), 0.01);
// there is no TP
assertEquals(0., andResult.get(MetricName.PRECISION), 0.01);
// there is no TP
assertEquals(0., andResult.get(MetricName.RECALL), 0.01);
// // there is no TP and zero in denominator
assertEquals(Double.NaN, andResult.get(MetricName.F_MEASURE), 0.01);
EvaluationResult orResult = Evaluator.evaluateBinaryClassification(xorset, orFunction, Vector::labeled);
assertEquals(0.75, orResult.get(MetricName.ACCURACY), 0.01);
// there is no TP
assertEquals(0.66, orResult.get(MetricName.PRECISION), 0.01);
// there is no TP
assertEquals(1., orResult.get(MetricName.RECALL), 0.01);
// // there is no TP and zero in denominator
assertEquals(0.8, orResult.get(MetricName.F_MEASURE), 0.01);
}
use of org.apache.ignite.ml.selection.scoring.evaluator.EvaluationResult in project ignite by apache.
the class RegressionMetricsTest method testCalculation.
/**
*/
@Test
public void testCalculation() {
Map<Vector, Double> linearSet = new HashMap<Vector, Double>() {
{
put(VectorUtils.of(0.), 0.);
put(VectorUtils.of(1.), 1.);
put(VectorUtils.of(2.), 2.);
put(VectorUtils.of(3.), 3.);
}
};
IgniteModel<Vector, Double> linearModel = v -> v.get(0);
IgniteModel<Vector, Double> squareModel = v -> Math.pow(v.get(0), 2);
EvaluationResult linearRes = Evaluator.evaluateRegression(linearSet, linearModel, Vector::labeled);
assertEquals(0., linearRes.get(MetricName.MAE), 0.01);
assertEquals(0., linearRes.get(MetricName.MSE), 0.01);
assertEquals(0., linearRes.get(MetricName.R2), 0.01);
assertEquals(0., linearRes.get(MetricName.RSS), 0.01);
assertEquals(0., linearRes.get(MetricName.RMSE), 0.01);
EvaluationResult squareRes = Evaluator.evaluateRegression(linearSet, squareModel, Vector::labeled);
assertEquals(2., squareRes.get(MetricName.MAE), 0.01);
assertEquals(10., squareRes.get(MetricName.MSE), 0.01);
assertEquals(8., squareRes.get(MetricName.R2), 0.01);
assertEquals(40., squareRes.get(MetricName.RSS), 0.01);
assertEquals(Math.sqrt(10), squareRes.get(MetricName.RMSE), 0.01);
}
Aggregations