Search in sources :

Example 1 with BinaryClassMetrics

use of com.alibaba.alink.operator.common.evaluation.BinaryClassMetrics in project Alink by alibaba.

the class Chap08 method c_7.

static void c_7() throws Exception {
    BinaryClassMetrics lr_metrics = new EvalBinaryClassBatchOp().setPositiveLabelValueString("1").setLabelCol(LABEL_COL_NAME).setPredictionDetailCol(PRED_DETAIL_COL_NAME).linkFrom(new AkSourceBatchOp().setFilePath(DATA_DIR + LR_PRED_FILE)).collectMetrics();
    StringBuilder sbd = new StringBuilder();
    sbd.append("< LR >\n").append("AUC : ").append(lr_metrics.getAuc()).append("\t Accuracy : ").append(lr_metrics.getAccuracy()).append("\t Precision : ").append(lr_metrics.getPrecision()).append("\t Recall : ").append(lr_metrics.getRecall()).append("\n");
    System.out.println(sbd.toString());
    System.out.println(lr_metrics);
    lr_metrics.saveRocCurveAsImage(DATA_DIR + "lr_roc.jpg", true);
    lr_metrics.saveRecallPrecisionCurveAsImage(DATA_DIR + "lr_recallprec.jpg", true);
    lr_metrics.saveLiftChartAsImage(DATA_DIR + "lr_lift.jpg", true);
    lr_metrics.saveKSAsImage(DATA_DIR + "lr_ks.jpg", true);
    new AkSourceBatchOp().setFilePath(DATA_DIR + SVM_PRED_FILE).link(new EvalBinaryClassBatchOp().setPositiveLabelValueString("1").setLabelCol(LABEL_COL_NAME).setPredictionDetailCol(PRED_DETAIL_COL_NAME).lazyPrintMetrics().lazyCollectMetrics(new Consumer<BinaryClassMetrics>() {

        @Override
        public void accept(BinaryClassMetrics binaryClassMetrics) {
            try {
                binaryClassMetrics.saveRocCurveAsImage(DATA_DIR + "svm_roc.jpg", true);
                binaryClassMetrics.saveRecallPrecisionCurveAsImage(DATA_DIR + "svm_recallprec.jpg", true);
                binaryClassMetrics.saveLiftChartAsImage(DATA_DIR + "svm_lift.jpg", true);
                binaryClassMetrics.saveKSAsImage(DATA_DIR + "svm_ks.jpg", true);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }));
    BatchOperator.execute();
}
Also used : BinaryClassMetrics(com.alibaba.alink.operator.common.evaluation.BinaryClassMetrics) AkSourceBatchOp(com.alibaba.alink.operator.batch.source.AkSourceBatchOp) Consumer(java.util.function.Consumer) IOException(java.io.IOException) EvalBinaryClassBatchOp(com.alibaba.alink.operator.batch.evaluation.EvalBinaryClassBatchOp)

Example 2 with BinaryClassMetrics

use of com.alibaba.alink.operator.common.evaluation.BinaryClassMetrics in project Alink by alibaba.

the class FmRecommImplicitTest method eval.

private static void eval(BatchOperator<?> pred) {
    pred = pred.select("label, concat('{\"0\":', cast((1-p) as varchar), ',\"1\":', cast(p as varchar), '}') as p_detail");
    EvalBinaryClassBatchOp eval = new EvalBinaryClassBatchOp().setLabelCol("label").setPredictionDetailCol("p_detail").linkFrom(pred);
    BinaryClassMetrics metrics = eval.collectMetrics();
    System.out.println(String.format("auc=%f,acc=%f,f1=%f", metrics.getAuc(), metrics.getAccuracy(), metrics.getF1()));
}
Also used : BinaryClassMetrics(com.alibaba.alink.operator.common.evaluation.BinaryClassMetrics) EvalBinaryClassBatchOp(com.alibaba.alink.operator.batch.evaluation.EvalBinaryClassBatchOp)

Example 3 with BinaryClassMetrics

use of com.alibaba.alink.operator.common.evaluation.BinaryClassMetrics in project Alink by alibaba.

the class EvalBinaryClassBatchOpTest method test.

@Test
public void test() throws Exception {
    Row[] data = new Row[] { Row.of("prefix1", "{\"prefix1\": 0.9, \"prefix0\": 0.1}"), Row.of("prefix1", "{\"prefix1\": 0.8, \"prefix0\": 0.2}"), Row.of("prefix1", "{\"prefix1\": 0.7, \"prefix0\": 0.3}"), Row.of("prefix0", "{\"prefix1\": 0.75, \"prefix0\": 0.25}"), Row.of("prefix0", "{\"prefix1\": 0.6, \"prefix0\": 0.4}"), Row.of("prefix1", "{\"prefix1\": 0.65, \"prefix0\": 0.35}"), Row.of("prefix0", null), Row.of("prefix1", "{\"prefix1\": 0.55, \"prefix0\": 0.45}"), Row.of("prefix0", "{\"prefix1\": 0.4, \"prefix0\": 0.6}"), Row.of("prefix0", "{\"prefix1\": 0.3, \"prefix0\": 0.7}"), Row.of("prefix1", "{\"prefix1\": 0.35, \"prefix0\": 0.65}"), Row.of("prefix0", "{\"prefix1\": 0.2, \"prefix0\": 0.8}"), Row.of("prefix1", "{\"prefix1\": 0.1, \"prefix0\": 0.9}") };
    MemSourceBatchOp source = new MemSourceBatchOp(data, new String[] { "label", "detailInput" });
    BinaryClassMetrics metrics = new EvalBinaryClassBatchOp().setLabelCol("label").setPredictionDetailCol("detailInput").linkFrom(source).collectMetrics();
    Assert.assertEquals(0.769, metrics.getPrc(), 0.01);
    Assert.assertEquals(0.371, metrics.getKs(), 0.01);
    Assert.assertEquals(0.657, metrics.getAuc(), 0.01);
    Assert.assertEquals(0.666, metrics.getAccuracy(), 0.01);
    Assert.assertEquals(0.314, metrics.getMacroKappa(), 0.01);
    Assert.assertEquals(0.666, metrics.getMicroPrecision(), 0.01);
    Assert.assertEquals(0.666, metrics.getWeightedRecall(), 0.01);
    metrics.saveRocCurveAsImage(folder.getRoot().toPath() + "rocCurve.png", true);
    metrics.saveKSAsImage(folder.getRoot().toPath() + "ks.png", true);
    metrics.saveLiftChartAsImage(folder.getRoot().toPath() + "liftchart.png", true);
    metrics.saveRecallPrecisionCurveAsImage(folder.getRoot().toPath() + "recallPrecision.png", true);
    metrics.saveLorenzCurveAsImage(folder.getRoot().toPath() + "lorenzCurve.png", true);
}
Also used : MemSourceBatchOp(com.alibaba.alink.operator.batch.source.MemSourceBatchOp) BinaryClassMetrics(com.alibaba.alink.operator.common.evaluation.BinaryClassMetrics) Row(org.apache.flink.types.Row) Test(org.junit.Test)

Aggregations

BinaryClassMetrics (com.alibaba.alink.operator.common.evaluation.BinaryClassMetrics)3 EvalBinaryClassBatchOp (com.alibaba.alink.operator.batch.evaluation.EvalBinaryClassBatchOp)2 AkSourceBatchOp (com.alibaba.alink.operator.batch.source.AkSourceBatchOp)1 MemSourceBatchOp (com.alibaba.alink.operator.batch.source.MemSourceBatchOp)1 IOException (java.io.IOException)1 Consumer (java.util.function.Consumer)1 Row (org.apache.flink.types.Row)1 Test (org.junit.Test)1