Search in sources :

Example 6 with AkSourceBatchOp

use of com.alibaba.alink.operator.batch.source.AkSourceBatchOp 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 7 with AkSourceBatchOp

use of com.alibaba.alink.operator.batch.source.AkSourceBatchOp in project Alink by alibaba.

the class Chap09 method c_5.

static void c_5() throws Exception {
    BatchOperator train_data = new AkSourceBatchOp().setFilePath(DATA_DIR + TRAIN_FILE);
    BatchOperator test_data = new AkSourceBatchOp().setFilePath(DATA_DIR + TEST_FILE);
    for (TreeType treeType : new TreeType[] { TreeType.GINI, TreeType.INFOGAIN, TreeType.INFOGAINRATIO }) {
        BatchOperator<?> model = train_data.link(new DecisionTreeTrainBatchOp().setTreeType(treeType).setFeatureCols(FEATURE_COL_NAMES).setCategoricalCols(FEATURE_COL_NAMES).setLabelCol(LABEL_COL_NAME).lazyPrintModelInfo("< " + treeType.toString() + " >").lazyCollectModelInfo(new Consumer<DecisionTreeModelInfo>() {

            @Override
            public void accept(DecisionTreeModelInfo decisionTreeModelInfo) {
                try {
                    decisionTreeModelInfo.saveTreeAsImage(DATA_DIR + "tree_" + treeType.toString() + ".jpg", true);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }));
        DecisionTreePredictBatchOp predictor = new DecisionTreePredictBatchOp().setPredictionCol(PREDICTION_COL_NAME).setPredictionDetailCol(PRED_DETAIL_COL_NAME);
        predictor.linkFrom(model, test_data);
        predictor.link(new EvalBinaryClassBatchOp().setPositiveLabelValueString("p").setLabelCol(LABEL_COL_NAME).setPredictionDetailCol(PRED_DETAIL_COL_NAME).lazyPrintMetrics("< " + treeType.toString() + " >"));
    }
    BatchOperator.execute();
}
Also used : TreeType(com.alibaba.alink.params.shared.tree.HasIndividualTreeType.TreeType) AkSourceBatchOp(com.alibaba.alink.operator.batch.source.AkSourceBatchOp) Consumer(java.util.function.Consumer) DecisionTreeModelInfo(com.alibaba.alink.operator.common.tree.TreeModelInfo.DecisionTreeModelInfo) IOException(java.io.IOException) DecisionTreePredictBatchOp(com.alibaba.alink.operator.batch.classification.DecisionTreePredictBatchOp) BatchOperator(com.alibaba.alink.operator.batch.BatchOperator) DecisionTreeTrainBatchOp(com.alibaba.alink.operator.batch.classification.DecisionTreeTrainBatchOp) EvalBinaryClassBatchOp(com.alibaba.alink.operator.batch.evaluation.EvalBinaryClassBatchOp)

Example 8 with AkSourceBatchOp

use of com.alibaba.alink.operator.batch.source.AkSourceBatchOp in project Alink by alibaba.

the class Chap09 method c_4_a.

static void c_4_a() throws Exception {
    AkSourceBatchOp train_data = new AkSourceBatchOp().setFilePath(DATA_DIR + TRAIN_FILE);
    AkSourceBatchOp test_data = new AkSourceBatchOp().setFilePath(DATA_DIR + TEST_FILE);
    NaiveBayesTrainBatchOp trainer = new NaiveBayesTrainBatchOp().setFeatureCols(FEATURE_COL_NAMES).setCategoricalCols(FEATURE_COL_NAMES).setLabelCol(LABEL_COL_NAME);
    NaiveBayesPredictBatchOp predictor = new NaiveBayesPredictBatchOp().setPredictionCol(PREDICTION_COL_NAME).setPredictionDetailCol(PRED_DETAIL_COL_NAME);
    train_data.link(trainer);
    predictor.linkFrom(trainer, test_data);
    trainer.lazyPrintModelInfo();
    trainer.lazyCollectModelInfo(new Consumer<NaiveBayesModelInfo>() {

        @Override
        public void accept(NaiveBayesModelInfo naiveBayesModelInfo) {
            StringBuilder sbd = new StringBuilder();
            for (String feature : new String[] { "odor", "spore_print_color", "gill_color" }) {
                HashMap<Object, HashMap<Object, Double>> map2 = naiveBayesModelInfo.getCategoryFeatureInfo().get(feature);
                sbd.append("\nfeature:").append(feature);
                for (Entry<Object, HashMap<Object, Double>> entry : map2.entrySet()) {
                    sbd.append("\n").append(entry.getKey()).append(" : ").append(entry.getValue().toString());
                }
            }
            System.out.println(sbd.toString());
        }
    });
    predictor.lazyPrint(10, "< Prediction >");
    predictor.link(new EvalBinaryClassBatchOp().setPositiveLabelValueString("p").setLabelCol(LABEL_COL_NAME).setPredictionDetailCol(PRED_DETAIL_COL_NAME).lazyPrintMetrics());
    BatchOperator.execute();
}
Also used : HashMap(java.util.HashMap) EvalBinaryClassBatchOp(com.alibaba.alink.operator.batch.evaluation.EvalBinaryClassBatchOp) Entry(java.util.Map.Entry) AkSourceBatchOp(com.alibaba.alink.operator.batch.source.AkSourceBatchOp) NaiveBayesModelInfo(com.alibaba.alink.operator.batch.classification.NaiveBayesModelInfo) NaiveBayesPredictBatchOp(com.alibaba.alink.operator.batch.classification.NaiveBayesPredictBatchOp) NaiveBayesTrainBatchOp(com.alibaba.alink.operator.batch.classification.NaiveBayesTrainBatchOp)

Example 9 with AkSourceBatchOp

use of com.alibaba.alink.operator.batch.source.AkSourceBatchOp in project Alink by alibaba.

the class Chap09 method c_3.

static void c_3() throws Exception {
    CsvSourceBatchOp source = new CsvSourceBatchOp().setFilePath(DATA_DIR + ORIGIN_FILE).setSchemaStr(Utils.generateSchemaString(COL_NAMES, COL_TYPES));
    source.lazyPrint(5, "< origin data >");
    Utils.splitTrainTestIfNotExist(source, DATA_DIR + TRAIN_FILE, DATA_DIR + TEST_FILE, 0.9);
    new AkSourceBatchOp().setFilePath(DATA_DIR + TRAIN_FILE).link(new ChiSqSelectorBatchOp().setSelectorType(SelectorType.NumTopFeatures).setNumTopFeatures(3).setSelectedCols(FEATURE_COL_NAMES).setLabelCol(LABEL_COL_NAME).lazyPrintModelInfo("< Chi-Square Selector >"));
    new AkSourceBatchOp().setFilePath(DATA_DIR + TRAIN_FILE).select("veil_type").distinct().lazyPrint(100);
    BatchOperator.execute();
}
Also used : AkSourceBatchOp(com.alibaba.alink.operator.batch.source.AkSourceBatchOp) ChiSqSelectorBatchOp(com.alibaba.alink.operator.batch.feature.ChiSqSelectorBatchOp) CsvSourceBatchOp(com.alibaba.alink.operator.batch.source.CsvSourceBatchOp)

Example 10 with AkSourceBatchOp

use of com.alibaba.alink.operator.batch.source.AkSourceBatchOp in project Alink by alibaba.

the class Chap11 method c_7.

static void c_7() throws Exception {
    AkSourceBatchOp train_sample = new AkSourceBatchOp().setFilePath(DATA_DIR + TRAIN_SAMPLE_FILE);
    AkSourceBatchOp test_data = new AkSourceBatchOp().setFilePath(DATA_DIR + TEST_FILE);
    String[] featureColNames = ArrayUtils.removeElement(test_data.getColNames(), LABEL_COL_NAME);
    for (TreeType treeType : new TreeType[] { TreeType.GINI, TreeType.INFOGAIN, TreeType.INFOGAINRATIO }) {
        new DecisionTreeClassifier().setTreeType(treeType).setFeatureCols(featureColNames).setLabelCol(LABEL_COL_NAME).setPredictionCol(PREDICTION_COL_NAME).setPredictionDetailCol(PRED_DETAIL_COL_NAME).fit(train_sample).transform(test_data).link(new EvalBinaryClassBatchOp().setPositiveLabelValueString("1").setLabelCol(LABEL_COL_NAME).setPredictionDetailCol(PRED_DETAIL_COL_NAME).lazyPrintMetrics(treeType.toString()));
    }
    BatchOperator.execute();
    new RandomForestClassifier().setNumTrees(20).setMaxDepth(4).setMaxBins(512).setFeatureCols(featureColNames).setLabelCol(LABEL_COL_NAME).setPredictionCol(PREDICTION_COL_NAME).setPredictionDetailCol(PRED_DETAIL_COL_NAME).fit(train_sample).transform(test_data).link(new EvalBinaryClassBatchOp().setLabelCol(LABEL_COL_NAME).setPredictionDetailCol(PRED_DETAIL_COL_NAME).lazyPrintMetrics("RandomForest with Stratified Sample"));
    BatchOperator.execute();
}
Also used : TreeType(com.alibaba.alink.params.shared.tree.HasIndividualTreeType.TreeType) AkSourceBatchOp(com.alibaba.alink.operator.batch.source.AkSourceBatchOp) DecisionTreeClassifier(com.alibaba.alink.pipeline.classification.DecisionTreeClassifier) RandomForestClassifier(com.alibaba.alink.pipeline.classification.RandomForestClassifier) EvalBinaryClassBatchOp(com.alibaba.alink.operator.batch.evaluation.EvalBinaryClassBatchOp)

Aggregations

AkSourceBatchOp (com.alibaba.alink.operator.batch.source.AkSourceBatchOp)66 AkSinkBatchOp (com.alibaba.alink.operator.batch.sink.AkSinkBatchOp)20 EvalBinaryClassBatchOp (com.alibaba.alink.operator.batch.evaluation.EvalBinaryClassBatchOp)18 File (java.io.File)16 EvalMultiClassBatchOp (com.alibaba.alink.operator.batch.evaluation.EvalMultiClassBatchOp)10 Pipeline (com.alibaba.alink.pipeline.Pipeline)10 LogisticRegression (com.alibaba.alink.pipeline.classification.LogisticRegression)10 EvalClusterBatchOp (com.alibaba.alink.operator.batch.evaluation.EvalClusterBatchOp)9 Stopwatch (com.alibaba.alink.common.utils.Stopwatch)8 MemSourceBatchOp (com.alibaba.alink.operator.batch.source.MemSourceBatchOp)7 Row (org.apache.flink.types.Row)6 Test (org.junit.Test)6 BatchOperator (com.alibaba.alink.operator.batch.BatchOperator)5 CsvSourceBatchOp (com.alibaba.alink.operator.batch.source.CsvSourceBatchOp)5 PipelineModel (com.alibaba.alink.pipeline.PipelineModel)5 ArrayList (java.util.ArrayList)4 PluginDownloader (com.alibaba.alink.common.io.plugin.PluginDownloader)3 RegisterKey (com.alibaba.alink.common.io.plugin.RegisterKey)3 LogisticRegressionPredictBatchOp (com.alibaba.alink.operator.batch.classification.LogisticRegressionPredictBatchOp)3 LogisticRegressionTrainBatchOp (com.alibaba.alink.operator.batch.classification.LogisticRegressionTrainBatchOp)3