Search in sources :

Example 1 with NaiveBayesPredictBatchOp

use of com.alibaba.alink.operator.batch.classification.NaiveBayesPredictBatchOp 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 2 with NaiveBayesPredictBatchOp

use of com.alibaba.alink.operator.batch.classification.NaiveBayesPredictBatchOp in project Alink by alibaba.

the class Chap09 method c_4_b.

static void c_4_b() 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("odor", "gill_color").setCategoricalCols("odor", "gill_color").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.lazyCollectModelInfo(new Consumer<NaiveBayesModelInfo>() {

        @Override
        public void accept(NaiveBayesModelInfo naiveBayesModelInfo) {
            StringBuilder sbd = new StringBuilder();
            for (String feature : new String[] { "odor", "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 >").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 3 with NaiveBayesPredictBatchOp

use of com.alibaba.alink.operator.batch.classification.NaiveBayesPredictBatchOp in project Alink by alibaba.

the class Chap12 method c_3.

static void c_3() 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).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();
    predictor.lazyPrint(1, "< Prediction >");
    predictor.link(new EvalMultiClassBatchOp().setLabelCol(LABEL_COL_NAME).setPredictionCol(PREDICTION_COL_NAME).setPredictionDetailCol(PRED_DETAIL_COL_NAME).lazyPrintMetrics("NaiveBayes"));
    BatchOperator.execute();
}
Also used : EvalMultiClassBatchOp(com.alibaba.alink.operator.batch.evaluation.EvalMultiClassBatchOp) AkSourceBatchOp(com.alibaba.alink.operator.batch.source.AkSourceBatchOp) NaiveBayesPredictBatchOp(com.alibaba.alink.operator.batch.classification.NaiveBayesPredictBatchOp) NaiveBayesTrainBatchOp(com.alibaba.alink.operator.batch.classification.NaiveBayesTrainBatchOp)

Aggregations

NaiveBayesPredictBatchOp (com.alibaba.alink.operator.batch.classification.NaiveBayesPredictBatchOp)3 NaiveBayesTrainBatchOp (com.alibaba.alink.operator.batch.classification.NaiveBayesTrainBatchOp)3 AkSourceBatchOp (com.alibaba.alink.operator.batch.source.AkSourceBatchOp)3 NaiveBayesModelInfo (com.alibaba.alink.operator.batch.classification.NaiveBayesModelInfo)2 EvalBinaryClassBatchOp (com.alibaba.alink.operator.batch.evaluation.EvalBinaryClassBatchOp)2 HashMap (java.util.HashMap)2 Entry (java.util.Map.Entry)2 EvalMultiClassBatchOp (com.alibaba.alink.operator.batch.evaluation.EvalMultiClassBatchOp)1