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

功能介绍

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

使用方式

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

参数说明

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

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

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

| 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. ["$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. # train op
  15. ns = NaiveBayesTextTrainBatchOp().setVectorCol("sv").setLabelCol("label")
  16. model = batchData.link(ns)
  17. # predict op
  18. predictor = NaiveBayesTextPredictBatchOp().setVectorCol("sv").setReservedCols(["sv", "label"]).setPredictionCol("pred")
  19. 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.NaiveBayesTextPredictBatchOp;
  4. import com.alibaba.alink.operator.batch.classification.NaiveBayesTextTrainBatchOp;
  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 NaiveBayesTextPredictBatchOpTest {
  10. @Test
  11. public void testNaiveBayesTextPredictBatchOp() throws Exception {
  12. List <Row> df_data = Arrays.asList(
  13. Row.of("$31$0:1.0 1:1.0 2:1.0 30:1.0", "1.0 1.0 1.0 1.0", "1"),
  14. Row.of("$31$0:1.0 1:1.0 2:0.0 30:1.0", "1.0 1.0 0.0 1.0", "1"),
  15. Row.of("$31$0:1.0 1:0.0 2:1.0 30:1.0", "1.0 0.0 1.0 1.0", "1"),
  16. Row.of("$31$0:1.0 1:0.0 2:1.0 30:1.0", "1.0 0.0 1.0 1.0", "1"),
  17. Row.of("$31$0:0.0 1:1.0 2:1.0 30:0.0", "0.0 1.0 1.0 0.0", "0"),
  18. Row.of("$31$0:0.0 1:1.0 2:1.0 30:0.0", "0.0 1.0 1.0 0.0", "0"),
  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. );
  21. BatchOperator <?> batchData = new MemSourceBatchOp(df_data, "sv string, dv string, label string");
  22. BatchOperator <?> ns = new NaiveBayesTextTrainBatchOp().setVectorCol("sv").setLabelCol("label");
  23. BatchOperator model = batchData.link(ns);
  24. BatchOperator <?> predictor = new NaiveBayesTextPredictBatchOp().setVectorCol("sv").setReservedCols("sv",
  25. "label").setPredictionCol("pred");
  26. predictor.linkFrom(model, batchData).print();
  27. }
  28. }

运行结果

| 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 |