Search in sources :

Example 1 with EvaluationResult

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);
}
Also used : VectorUtils(org.apache.ignite.ml.math.primitives.vector.VectorUtils) Evaluator(org.apache.ignite.ml.selection.scoring.evaluator.Evaluator) MetricName(org.apache.ignite.ml.selection.scoring.metric.MetricName) Map(java.util.Map) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) EvaluationResult(org.apache.ignite.ml.selection.scoring.evaluator.EvaluationResult) HashMap(java.util.HashMap) Test(org.junit.Test) IgniteModel(org.apache.ignite.ml.IgniteModel) Assert.assertEquals(org.junit.Assert.assertEquals) HashMap(java.util.HashMap) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) EvaluationResult(org.apache.ignite.ml.selection.scoring.evaluator.EvaluationResult) Test(org.junit.Test)

Example 2 with EvaluationResult

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);
}
Also used : VectorUtils(org.apache.ignite.ml.math.primitives.vector.VectorUtils) Evaluator(org.apache.ignite.ml.selection.scoring.evaluator.Evaluator) MetricName(org.apache.ignite.ml.selection.scoring.metric.MetricName) Map(java.util.Map) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) EvaluationResult(org.apache.ignite.ml.selection.scoring.evaluator.EvaluationResult) HashMap(java.util.HashMap) Test(org.junit.Test) IgniteModel(org.apache.ignite.ml.IgniteModel) Assert.assertEquals(org.junit.Assert.assertEquals) HashMap(java.util.HashMap) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) EvaluationResult(org.apache.ignite.ml.selection.scoring.evaluator.EvaluationResult) Test(org.junit.Test)

Aggregations

HashMap (java.util.HashMap)2 Map (java.util.Map)2 IgniteModel (org.apache.ignite.ml.IgniteModel)2 Vector (org.apache.ignite.ml.math.primitives.vector.Vector)2 VectorUtils (org.apache.ignite.ml.math.primitives.vector.VectorUtils)2 EvaluationResult (org.apache.ignite.ml.selection.scoring.evaluator.EvaluationResult)2 Evaluator (org.apache.ignite.ml.selection.scoring.evaluator.Evaluator)2 MetricName (org.apache.ignite.ml.selection.scoring.metric.MetricName)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Test (org.junit.Test)2