经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Spark » 查看文章
Spark Java API 之 CountVectorizer
来源:cnblogs  作者:hapjin  时间:2018/11/5 10:51:37  对本文有异议

Spark Java API 之 CountVectorizer

由于在Spark中文本处理与分析的一些机器学习算法的输入并不是文本数据,而是数值型向量。因此,需要进行转换。而将文本数据转换成数值型的向量有很多种方法,CountVectorizer是其中之一。

A CountVectorizer converts a collection of text documents into a vector representing the word count of text documents.

在构建向量时,有两个重要的参数:VocabSizeMinDF。前者表示词典的大小,后者表示当文档中某个Term出现的次数小于MinDF时,则不计入词典(该Term不属于词典中的 单词)。

比如说现在有两篇文档:【"w1", "w2", "w4", "w5", "w2"】,【"w1", "w2", "w3"】

  1. CountVectorizer cv = new CountVectorizer().setInputCol("text").setOutputCol("feature")
  2. .setVocabSize(3).setMinDF(2);

根据上面代码中的参数设置,词典大小为3,即一共可以有三个Term。由于在所有的文档中,"w1"出现2次,"w2"出现2次,因此计入词典。而"w3"、"w4"、"w5"只出现一次,不属于词典中的单词(Term)。如下图所示:词典中只有两个Term

When the dictionary is not defined CountVectorizer iterates over the dataset twice to prepare
the dictionary based on frequency and size.

CountVectorizer 首先扫描Dataset(文本数据)生成词典,然后再次扫描生成向量模型(CountVectorizerModel)

在构造Dataset 时,需要指定模式。用模式来解释Dataset中每一行的数据。

  1. StructType schema = new StructType(new StructField[]{
  2. new StructField("text", new ArrayType(DataTypes.StringType, true), false, Metadata.empty())
  3. });

A field inside a StructType. param: name The name of this field. param: dataType The data type of this field. param: nullable Indicates if values of this field can be null values. param: metadata The metadata of this field. The metadata should be preserved during transformation if the content of the column is not modified

第一个参数是:名称;第二个参数是dataType 数据类型;第三个参数是标识该字段的值是否可以为空;第四个参数为字段的元数据信息。

整个示例代码:

  1. import org.apache.spark.ml.feature.CountVectorizer;
  2. import org.apache.spark.ml.feature.CountVectorizerModel;
  3. import org.apache.spark.sql.Dataset;
  4. import org.apache.spark.sql.Row;
  5. import org.apache.spark.sql.RowFactory;
  6. import org.apache.spark.sql.SparkSession;
  7. import org.apache.spark.sql.types.*;
  8. import java.util.Arrays;
  9. import java.util.List;
  10. public class CounterVectorExample {
  11. public static void main(String[] args) {
  12. SparkSession spark = SparkSession.builder().appName("CountVectorizer").master("spark://172.25.129.170:7077").getOrCreate();
  13. List<Row> data = Arrays.asList(
  14. // RowFactory.create(Arrays.asList("a", "b", "c")),
  15. // RowFactory.create(Arrays.asList("a", "b", "b", "c", "a")),
  16. // RowFactory.create(Arrays.asList("a", "b", "a", "b"))
  17. RowFactory.create(Arrays.asList("w1", "w2", "w3")),
  18. RowFactory.create(Arrays.asList("w1", "w2", "w4", "w5", "w2"))
  19. );
  20. StructType schema = new StructType(new StructField[]{
  21. new StructField("text", new ArrayType(DataTypes.StringType, true), false, Metadata.empty())
  22. });
  23. Dataset<Row> df = spark.createDataFrame(data, schema);
  24. CountVectorizer cv = new CountVectorizer().setInputCol("text").setOutputCol("feature")
  25. .setVocabSize(3).setMinDF(2);
  26. CountVectorizerModel cvModel = cv.fit(df);
  27. //prior dictionary
  28. CountVectorizerModel cvm = new CountVectorizerModel(new String[]{"a", "b", "c"}).setInputCol("text")
  29. .setOutputCol("feature");
  30. // cvm.
  31. cvModel.transform(df).show(false);
  32. spark.stop();
  33. }
  34. }

输出结果默认是以稀疏向量表示:

A sparse vector represented by an index array and a value array.

param: size size of the vector. param: indices index array, assume to be strictly increasing. param: values value array, must have the same length as the index array.

第一个字段代表:向量长度,由于这里词典中只有2个Term,因此转换出来的向量长度为2;第二个字段:索引下标;第三个字段:索引位置处相应的向量元素值。由上图中位置0处的Term是 w2,位置1处的Term是w1,因此,输出:

当然,我们也可以预先定义词典:在构造CountVectorizerModel的时候指定词典:【"w1", "w2", "w3"】

  1. //prior dictionary
  2. CountVectorizerModel cvm = new CountVectorizerModel(new String[]{"w1", "w2", "w3"}).setInputCol("text").setOutputCol("feature");
  3. cvm.transform(df).show(false);

对于文本:[w1,w2,w3],每个Term都在词典中,且出现了一次,因此稀疏特征向量表示为:(3,[0,1,2],[1.0,1.0,1.0])。其中,3代表向量的长度为3维向量;[0,1,2]表示向量的索引;[1.0,1.0,1.0]表示,在相应的索引处,每个元素值为1.0(即各个Term只出现了一次)。而对于文本[w1, w2, w4, w5, w2],因为w4和w5不在词典中,w1出现一次,w2出现2次,故其特征如下:

可以看出:对于CountVectorizerModel,向量长度就是词典的大小。

系列文章:

原文:https://www.cnblogs.com/hapjin/p/9899164.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号