Search in sources :

Example 1 with NaiveBayesModelInfo

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

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

the class NaiveBayesModelDataConverter method loadModelInfo.

public NaiveBayesModelInfo loadModelInfo(List<Row> rows) {
    NaiveBayesModelInfo modelInfo = JsonConverter.fromJson((String) rows.get(0).getField(1), NaiveBayesModelInfo.class);
    modelInfo.stringIndexerModelSerialized = rows.subList(1, rows.size());
    return modelInfo;
}
Also used : NaiveBayesModelInfo(com.alibaba.alink.operator.batch.classification.NaiveBayesModelInfo)

Example 3 with NaiveBayesModelInfo

use of com.alibaba.alink.operator.batch.classification.NaiveBayesModelInfo 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)

Aggregations

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