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();
}
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;
}
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();
}
Aggregations