Java 类名:com.alibaba.alink.operator.batch.classification.NaiveBayesPredictBatchOp
Python 类名:NaiveBayesPredictBatchOp

功能介绍

使用朴素贝叶斯模型用于多分类任务的预测。

使用方式

该组件是预测组件,需要配合训练组件 NaiveBayesTrainBatchOp 使用。

参数说明

| 名称 | 中文名称 | 描述 | 类型 | 是否必须? | 取值范围 | 默认值 | | —- | —- | —- | —- | —- | —- | —- |

| predictionCol | 预测结果列名 | 预测结果列名 | String | ✓ | | |

| modelFilePath | 模型的文件路径 | 模型的文件路径 | String | | | null |

| predictionDetailCol | 预测详细信息列名 | 预测详细信息列名 | String | | | |

| reservedCols | 算法保留列名 | 算法保留列 | String[] | | | null |

| numThreads | 组件多线程线程个数 | 组件多线程线程个数 | Integer | | | 1 |

代码示例

Python 代码

  1. from pyalink.alink import *
  2. import pandas as pd
  3. useLocalEnv(1)
  4. df_data = pd.DataFrame([
  5. [1.0, 1.0, 0.0, 1.0, 1],
  6. [1.0, 0.0, 1.0, 1.0, 1],
  7. [1.0, 0.0, 1.0, 1.0, 1],
  8. [0.0, 1.0, 1.0, 0.0, 0],
  9. [0.0, 1.0, 1.0, 0.0, 0],
  10. [0.0, 1.0, 1.0, 0.0, 0],
  11. [0.0, 1.0, 1.0, 0.0, 0],
  12. [1.0, 1.0, 1.0, 1.0, 1],
  13. [0.0, 1.0, 1.0, 0.0, 0]
  14. ])
  15. batchData = BatchOperator.fromDataframe(df_data, schemaStr='f0 double, f1 double, f2 double, f3 double, label int')
  16. colnames = ["f0","f1","f2", "f3"]
  17. ns = NaiveBayesTrainBatchOp().setFeatureCols(colnames).setLabelCol("label")
  18. model = batchData.link(ns)
  19. predictor = NaiveBayesPredictBatchOp().setPredictionCol("pred")
  20. predictor.linkFrom(model, batchData).print()

Java 代码

  1. import org.apache.flink.types.Row;
  2. import com.alibaba.alink.operator.batch.BatchOperator;
  3. import com.alibaba.alink.operator.batch.classification.NaiveBayesPredictBatchOp;
  4. import com.alibaba.alink.operator.batch.classification.NaiveBayesTrainBatchOp;
  5. import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
  6. import org.junit.Test;
  7. import java.util.Arrays;
  8. import java.util.List;
  9. public class NaiveBayesPredictBatchOpTest {
  10. @Test
  11. public void testNaiveBayesPredictBatchOp() throws Exception {
  12. List <Row> df_data = Arrays.asList(
  13. Row.of(1.0, 1.0, 0.0, 1.0, 1),
  14. Row.of(1.0, 0.0, 1.0, 1.0, 1),
  15. Row.of(1.0, 0.0, 1.0, 1.0, 1),
  16. Row.of(0.0, 1.0, 1.0, 0.0, 0),
  17. Row.of(0.0, 1.0, 1.0, 0.0, 0),
  18. Row.of(0.0, 1.0, 1.0, 0.0, 0),
  19. Row.of(0.0, 1.0, 1.0, 0.0, 0),
  20. Row.of(1.0, 1.0, 1.0, 1.0, 1),
  21. Row.of(0.0, 1.0, 1.0, 0.0, 0)
  22. );
  23. BatchOperator <?> batchData = new MemSourceBatchOp(df_data,
  24. "f0 double, f1 double, f2 double, f3 double, label int");
  25. BatchOperator <?> ns = new NaiveBayesTrainBatchOp().setFeatureCols("f0", "f1", "f2", "f3").setLabelCol(
  26. "label");
  27. BatchOperator model = batchData.link(ns);
  28. BatchOperator <?> predictor = new NaiveBayesPredictBatchOp().setPredictionCol("pred");
  29. predictor.linkFrom(model, batchData).print();
  30. }
  31. }

运行结果

| f0 | f1 | f2 | f3 | label | pred | | —- | —- | —- | —- | —- | —- |

| 1.0 | 1.0 | 0.0 | 1.0 | 1 | 1 |

| 1.0 | 0.0 | 1.0 | 1.0 | 1 | 1 |

| 1.0 | 0.0 | 1.0 | 1.0 | 1 | 1 |

| 0.0 | 1.0 | 1.0 | 0.0 | 0 | 0 |

| 0.0 | 1.0 | 1.0 | 0.0 | 0 | 0 |

| 0.0 | 1.0 | 1.0 | 0.0 | 0 | 0 |

| 0.0 | 1.0 | 1.0 | 0.0 | 0 | 0 |

| 1.0 | 1.0 | 1.0 | 1.0 | 1 | 1 |

| 0.0 | 1.0 | 1.0 | 0.0 | 0 | 0 |