Java 类名:com.alibaba.alink.operator.batch.nlp.DocHashCountVectorizerPredictBatchOp
Python 类名:DocHashCountVectorizerPredictBatchOp

功能介绍

根据文本中词语的特征信息,将每条文本转化为固定长度的稀疏向量。
在转换时,每个词语会通过哈希函数映射到稀疏向量的一个索引值,映射到同一个索引值的多个词语将看作同一个词语来统计特征信息。

使用方式

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

参数说明

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

| selectedCol | 选中的列名 | 计算列对应的列名 | String | ✓ | 所选列类型为 [STRING] | |

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

| outputCol | 输出结果列 | 输出结果列列名,可选,默认null | String | | | null |

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

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

代码示例

Python 代码

  1. from pyalink.alink import *
  2. import pandas as pd
  3. useLocalEnv(1)
  4. df = pd.DataFrame([
  5. [0, u'二手旧书:医学电磁成像'],
  6. [1, u'二手美国文学选读( 下册 )李宜燮南开大学出版社 9787310003969'],
  7. [2, u'二手正版图解象棋入门/谢恩思主编/华龄出版社'],
  8. [3, u'二手中国糖尿病文献索引'],
  9. [4, u'二手郁达夫文集( 国内版 )全十二册馆藏书']
  10. ])
  11. inOp1 = BatchOperator.fromDataframe(df, schemaStr='id int, text string')
  12. segment = SegmentBatchOp().setSelectedCol("text").linkFrom(inOp1)
  13. train = DocHashCountVectorizerTrainBatchOp().setSelectedCol("text").linkFrom(segment)
  14. predictBatch = DocHashCountVectorizerPredictBatchOp().setSelectedCol("text").linkFrom(train, segment)
  15. train.lazyPrint(-1)
  16. predictBatch.print()

Java 代码

  1. import org.apache.flink.types.Row;
  2. import com.alibaba.alink.operator.batch.BatchOperator;
  3. import com.alibaba.alink.operator.batch.nlp.DocHashCountVectorizerPredictBatchOp;
  4. import com.alibaba.alink.operator.batch.nlp.DocHashCountVectorizerTrainBatchOp;
  5. import com.alibaba.alink.operator.batch.nlp.SegmentBatchOp;
  6. import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
  7. import org.junit.Test;
  8. import java.util.Arrays;
  9. import java.util.List;
  10. public class DocHashCountVectorizerPredictBatchOpTest {
  11. @Test
  12. public void testDocHashCountVectorizerPredictBatchOp() throws Exception {
  13. List <Row> df = Arrays.asList(
  14. Row.of(0, "二手旧书:医学电磁成像"),
  15. Row.of(1, "二手美国文学选读( 下册 )李宜燮南开大学出版社 9787310003969"),
  16. Row.of(2, "二手正版图解象棋入门/谢恩思主编/华龄出版社"),
  17. Row.of(3, "二手中国糖尿病文献索引"),
  18. Row.of(4, "二手郁达夫文集( 国内版 )全十二册馆藏书")
  19. );
  20. BatchOperator <?> inOp1 = new MemSourceBatchOp(df, "id int, text string");
  21. BatchOperator <?> segment = new SegmentBatchOp().setSelectedCol("text").linkFrom(inOp1);
  22. BatchOperator <?> train = new DocHashCountVectorizerTrainBatchOp().setSelectedCol("text").linkFrom(segment);
  23. BatchOperator <?> predictBatch = new DocHashCountVectorizerPredictBatchOp().setSelectedCol("text").linkFrom(
  24. train, segment);
  25. train.lazyPrint(-1);
  26. predictBatch.print();
  27. }
  28. }

运行结果

模型数据

| model_id | model_info | | —- | —- |

| 0 | {“numFeatures”:”262144”,”minTF”:”1.0”,”featureType”:””WORD_COUNT””} |

| 1048576 | {“0”:-0.6061358035703156,”37505”:1.0986122886681098,”180035”:1.0986122886681098,”214785”:1.0986122886681098,”195777”:1.0986122886681098,”181703”:1.0986122886681098,”216139”:0.6931471805599453,”226698”:1.0986122886681098,”10121”:1.0986122886681098,”261064”:1.0986122886681098,”126159”:1.0986122886681098,”251090”:1.0986122886681098,”46743”:1.0986122886681098,”219988”:1.0986122886681098,”206232”:0.0,”162140”:1.0986122886681098,”87711”:1.0986122886681098,”259932”:1.0986122886681098,”257763”:1.0986122886681098,”119456”:1.0986122886681098,”241122”:1.0986122886681098,”138080”:0.6931471805599453,”250534”:0.6931471805599453,”172901”:1.0986122886681098,”254628”:0.6931471805599453,”259051”:1.0986122886681098,”141480”:1.0986122886681098,”40170”:1.0986122886681098,”255656”:1.0986122886681098,”93228”:1.0986122886681098,”119217”:1.0986122886681098,”256946”:1.0986122886681098,”210357”:1.0986122886681098,”232884”:1.0986122886681098,”70777”:1.0986122886681098,”158267”:1.0986122886681098,”64444”:1.0986122886681098,”96509”:1.0986122886681098} |

批预测结果

| id | text | | —- | —- |

| 0 | $262144$10121:1.0 64444:1.0 119456:1.0 206232:1.0 210357:1.0 256946:1.0 |

| 1 | $262144$0:6.0 37505:1.0 46743:1.0 93228:1.0 119217:1.0 138080:1.0 141480:1.0 172901:1.0 206232:1.0 216139:1.0 226698:1.0 254628:1.0 |

| 2 | $262144$40170:1.0 70777:1.0 96509:1.0 126159:1.0 158267:1.0 181703:1.0 206232:1.0 216139:1.0 232884:1.0 250534:2.0 259932:1.0 |

| 3 | $262144$206232:1.0 214785:1.0 251090:1.0 255656:1.0 261064:1.0 |

| 4 | $262144$0:4.0 87711:1.0 138080:1.0 162140:1.0 180035:1.0 195777:1.0 206232:1.0 219988:1.0 241122:1.0 254628:1.0 257763:1.0 259051:1.0 |