Java 类名:com.alibaba.alink.operator.stream.classification.NaiveBayesTextPredictStreamOp
Python 类名:NaiveBayesTextPredictStreamOp

功能介绍

训练一个朴素贝叶斯文本分类模型用于多分类任务。

使用方式

该组件是预测组件,需要配合预测组件 NaiveBayesTextTrainBatchOp 使用。

参数说明

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

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

| vectorCol | 向量列名 | 向量列对应的列名 | String | ✓ | 所选列类型为 [DENSE_VECTOR, SPARSE_VECTOR, STRING, VECTOR] | |

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

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

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

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

| modelStreamFilePath | 模型流的文件路径 | 模型流的文件路径 | String | | | null |

| modelStreamScanInterval | 扫描模型路径的时间间隔 | 描模型路径的时间间隔,单位秒 | Integer | | | 10 |

| modelStreamStartTime | 模型流的起始时间 | 模型流的起始时间。默认从当前时刻开始读。使用yyyy-mm-dd hh:mm:ss.fffffffff格式,详见Timestamp.valueOf(String s) | String | | | null |

代码示例

Python 代码

  1. from pyalink.alink import *
  2. import pandas as pd
  3. useLocalEnv(1)
  4. df_data = pd.DataFrame([
  5. ["$31$0:1.0 1:1.0 2:1.0 30:1.0","1.0 1.0 1.0 1.0", '1'],
  6. ["$31$0:1.0 1:1.0 2:0.0 30:1.0","1.0 1.0 0.0 1.0", '1'],
  7. ["$31$0:1.0 1:0.0 2:1.0 30:1.0","1.0 0.0 1.0 1.0", '1'],
  8. ["$31$0:1.0 1:0.0 2:1.0 30:1.0","1.0 0.0 1.0 1.0", '1'],
  9. ["$31$0:0.0 1:1.0 2:1.0 30:0.0","0.0 1.0 1.0 0.0", '0'],
  10. ["$31$0:0.0 1:1.0 2:1.0 30:0.0","0.0 1.0 1.0 0.0", '0'],
  11. ["$31$0:0.0 1:1.0 2:1.0 30:0.0","0.0 1.0 1.0 0.0", '0']
  12. ])
  13. batchData = BatchOperator.fromDataframe(df_data, schemaStr='sv string, dv string, label string')
  14. # stream data
  15. streamData = StreamOperator.fromDataframe(df_data, schemaStr='sv string, dv string, label string')
  16. # train op
  17. ns = NaiveBayesTextTrainBatchOp().setVectorCol("sv").setLabelCol("label")
  18. model = batchData.link(ns)
  19. # predict op
  20. predictor = NaiveBayesTextPredictStreamOp(model).setVectorCol("sv").setReservedCols(["sv", "label"]).setPredictionCol("pred")
  21. predictor.linkFrom(streamData).print()
  22. StreamOperator.execute()

Java 代码

  1. import org.apache.flink.types.Row;
  2. import com.alibaba.alink.operator.batch.BatchOperator;
  3. import com.alibaba.alink.operator.batch.classification.NaiveBayesTextTrainBatchOp;
  4. import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
  5. import com.alibaba.alink.operator.stream.StreamOperator;
  6. import com.alibaba.alink.operator.stream.classification.NaiveBayesTextPredictStreamOp;
  7. import com.alibaba.alink.operator.stream.source.MemSourceStreamOp;
  8. import org.junit.Test;
  9. import java.util.Arrays;
  10. import java.util.List;
  11. public class NaiveBayesTextPredictStreamOpTest {
  12. @Test
  13. public void testNaiveBayesTextPredictStreamOp() throws Exception {
  14. List <Row> df_data = Arrays.asList(
  15. Row.of("$31$0:1.0 1:1.0 2:1.0 30:1.0", "1.0 1.0 1.0 1.0", "1"),
  16. Row.of("$31$0:1.0 1:1.0 2:0.0 30:1.0", "1.0 1.0 0.0 1.0", "1"),
  17. Row.of("$31$0:1.0 1:0.0 2:1.0 30:1.0", "1.0 0.0 1.0 1.0", "1"),
  18. Row.of("$31$0:1.0 1:0.0 2:1.0 30:1.0", "1.0 0.0 1.0 1.0", "1"),
  19. Row.of("$31$0:0.0 1:1.0 2:1.0 30:0.0", "0.0 1.0 1.0 0.0", "0"),
  20. Row.of("$31$0:0.0 1:1.0 2:1.0 30:0.0", "0.0 1.0 1.0 0.0", "0"),
  21. Row.of("$31$0:0.0 1:1.0 2:1.0 30:0.0", "0.0 1.0 1.0 0.0", "0")
  22. );
  23. BatchOperator <?> batchData = new MemSourceBatchOp(df_data, "sv string, dv string, label string");
  24. StreamOperator <?> streamData = new MemSourceStreamOp(df_data, "sv string, dv string, label string");
  25. BatchOperator <?> ns = new NaiveBayesTextTrainBatchOp().setVectorCol("sv").setLabelCol("label");
  26. BatchOperator <?> model = batchData.link(ns);
  27. StreamOperator <?> predictor = new NaiveBayesTextPredictStreamOp(model).setVectorCol("sv").setReservedCols(
  28. "sv",
  29. "label").setPredictionCol("pred");
  30. predictor.linkFrom(streamData).print();
  31. StreamOperator.execute();
  32. }
  33. }

运行结果

| sv | label | pred | | —- | —- | —- |

| “$31$0:1.0 1:1.0 2:1.0 30:1.0” | 1 | 1 |

| “$31$0:1.0 1:1.0 2:0.0 30:1.0” | 1 | 1 |

| “$31$0:1.0 1:0.0 2:1.0 30:1.0” | 1 | 1 |

| “$31$0:1.0 1:0.0 2:1.0 30:1.0” | 1 | 1 |

| “$31$0:0.0 1:1.0 2:1.0 30:0.0” | 0 | 0 |

| “$31$0:0.0 1:1.0 2:1.0 30:0.0” | 0 | 0 |

| “$31$0:0.0 1:1.0 2:1.0 30:0.0” | 0 | 0 |