Word2Vec - 云+社区 - 腾讯云

       以前对于文本类型的数据,都是通过tf-idf进行处理的,这个可以参见以前写的博客,这里就不在详细介绍了。最近项目组老大跟我说了word2vec这种文本型特征提取的方式。特地给我讲解了一下俩者之间的区别:

      一个词经过tf-idf处理之后,是一个数字,如果是相近的词语,它是无法区分的。Word2Vec就不一样了,比如研究和科研这俩个词,经过Word2Vec处理之后,是向量的形式。科研:[1,0,0,1,0],研究[1,0,0,0.8,0]。是可以判断是否相近的。

      对于概念,我在spark官网翻译了一段话:计算一系列词的分布式向量。分布式的主要优点是相近的词在向量空间中是相近的,使泛化的新模式更容易和模型的评估更强大。分布式向量显示在许多自然语言处理应用中是有用的。命名实体类别,消歧。解析,标注和机器翻译。

代码如图:

package com.iflytek.features import org.apache.spark.ml.feature.Word2Vec import org.apache.spark.ml.linalg.Vector import org.apache.spark.sql.Row import org.apache.spark.sql.SparkSession object wordtovec {   def main(args: Array[String]): Unit = {       val spark=SparkSession.builder().appName("pca").master("local").getOrCreate()       // Input data: Each row is a bag of words from a sentence or document.       val documentDF = spark.createDataFrame(Seq(         "Hi I heard about Spark".split(" "),         "I wish Java could use case classes".split(" "),         "Logistic regression models are neat".split(" ")          ).map(Tuple1.apply)).toDF("text")

      // Learn a mapping from words to Vectors.       val word2Vec = new Word2Vec()         .setInputCol("text")         .setOutputCol("result")         .setVectorSize(3)         .setMinCount(0)

      val model = word2Vec.fit(documentDF)

      val result = model.transform(documentDF)       result.select("result").take(3).foreach(println)       val vecs=model.getVectors       vecs.foreach { x => println(x.apply(0)+":"+x.apply(1))}       val synonyms =model.findSynonyms("are", 3)         synonyms.select("word", "similarity").foreach { x => println(x.apply(0)+":"+x.apply(1)) }   } }

result的输出结果:

[[-0.028139343485236168,0.04554025698453188,-0.013317196490243079]] [[0.06872416580361979,-0.02604914902310286,0.02165239889706884]] [[0.023467857390642166,0.027799883112311366,0.0331136979162693]]

vecs的输出结果:

heard:[-0.053989291191101074,0.14687322080135345,-0.0022512583527714014] are:[-0.16293057799339294,-0.14514029026031494,0.1139335036277771] neat:[-0.0406828410923481,0.028049567714333534,-0.16289857029914856] classes:[-0.1490514725446701,-0.04974571615457535,0.03320947289466858] I:[-0.019095497205853462,-0.131216898560524,0.14303986728191376] regression:[0.16541987657546997,0.06469681113958359,0.09233078360557556] Logistic:[0.036407098174095154,0.05800342187285423,-0.021965932101011276] Spark:[-0.1267719864845276,0.09859133511781693,-0.10378564894199371] could:[0.15352481603622437,0.06008218228816986,0.07726015895605087] use:[0.08318991959095001,0.002120430115610361,-0.07926633954048157] Hi:[-0.05663909390568733,0.009638422168791294,-0.033786069601774216] models:[0.11912573128938675,0.1333899050951004,0.1441687047481537] case:[0.14080166816711426,0.08094961196184158,0.1596144139766693] about:[0.11579915136098862,0.10381520539522171,-0.06980287283658981] Java:[0.12235434353351593,-0.03189820423722267,-0.1423865109682083] wish:[0.14934538304805756,-0.11263544857501984,-0.03990427032113075]

synonyms的输出:

classes:0.8926231541787831 I:0.8009102388269147 Hi:0.24258211195587995

getVectors:得到语料中所有词及其词向量

transform:将训练语料中,一行,也就是一个句子,表示成一个向量。它的处理方式是,对句子中所有的词向量取平均作为句子的向量表示,最native的表示方法。

findSynonyms("are",3):得到的是与词are相近的3个词。

经过我的实际测试,发现使用Word2Vector可以提高各项评价指标,大家也不妨试试啊。

spark2.0的分类、回归、聚类算法我都测试了一遍,只有分类的朴素贝叶斯是行不通的(特征值不接受负的),其他的都是行的通的。


Original url: Access
Created at: 2019-01-28 10:18:35
Category: default
Tags: none

请先后发表评论
  • 最新评论
  • 总共0条评论