Spark MLlib LDA主题模型
2015-08-07 11:52
471 查看
Spark MLlib LDA主题模型(1)
Spark MLlib LDA主题模型是Spark1.3开始加入的,具体介绍看以下文档:官方编程指南:
http://spark.apache.org/docs/latest/mllib-clustering.html#latent-dirichlet-allocation-lda
Spark MLlib LDA 简介:
http://blog.jobbole.com/86130/
关于LDA主题模型的理论知识讲解放在下期。
1.1 LDA实例
实例步骤:1)加载数据
返回的数据格式为:documents: RDD[(Long, Vector)],其中:Long为文章ID,Vector为文章分词后的词向量;用户可以读取指定目录下的数据,通过分词以及数据格式的转换,转换成RDD[(Long, Vector)]即可。
2)建立模型
模型参数设置说明:
k: 主题数,或者聚类中心数
DocConcentration:文章分布的超参数(Dirichlet分布的参数),必需>1.0
TopicConcentration:主题分布的超参数(Dirichlet分布的参数),必需>1.0
MaxIterations:迭代次数
setSeed:随机种子
CheckpointInterval:迭代计算时检查点的间隔
Optimizer:优化计算方法,目前支持"em", "online"
3)结果输出
topicsMatrix以及topics(word,topic))输出。
实例代码如下:
import org.apache.log4j.{ Level, Logger } import org.apache.spark.{ SparkConf, SparkContext } import org.apache.spark.mllib.clustering.LDA import org.apache.spark.mllib.linalg.Vectors object lda { def main(args: Array[String]) { //0 构建Spark对象 val conf = new SparkConf().setAppName("lda") val sc = new SparkContext(conf) Logger.getRootLogger.setLevel(Level.WARN) //1 加载数据,返回的数据格式为:documents: RDD[(Long, Vector)] // 其中:Long为文章ID,Vector为文章分词后的词向量 // 可以读取指定目录下的数据,通过分词以及数据格式的转换,转换成RDD[(Long, Vector)]即可 val data = sc.textFile("data/mllib/sample_lda_data.txt") val parsedData = data.map(s => Vectors.dense(s.trim.split(' ').map(_.toDouble))) // Index documents with unique IDs val corpus = parsedData.zipWithIndex.map(_.swap).cache() //2 建立模型,设置训练参数,训练模型 /** * k: 主题数,或者聚类中心数 * DocConcentration:文章分布的超参数(Dirichlet分布的参数),必需>1.0 * TopicConcentration:主题分布的超参数(Dirichlet分布的参数),必需>1.0 * MaxIterations:迭代次数 * setSeed:随机种子 * CheckpointInterval:迭代计算时检查点的间隔 * Optimizer:优化计算方法,目前支持"em", "online" */ val ldaModel = new LDA(). setK(3). setDocConcentration(5). setTopicConcentration(5). setMaxIterations(20). setSeed(0L). setCheckpointInterval(10). setOptimizer("em"). run(corpus) //3 模型输出,模型参数输出,结果输出 // Output topics. Each is a distribution over words (matching word count vectors) println("Learned topics (as distributions over vocab of " + ldaModel.vocabSize + " words):") val topics = ldaModel.topicsMatrix for (topic <- Range(0, 3)) { print("Topic " + topic + ":") for (word <- Range(0, ldaModel.vocabSize)) { print(" " + topics(word, topic)); } println() } } }
转载请注明出处:
http://blog.csdn.net/sunbow0/
相关文章推荐
- IIS500的问题
- jquery 根据输入框输入的值 自动计算其他值的实例
- 右侧跟随滚动框(调用框架)
- 9.6智力题(二)——九球称重
- 数组名作函数参数
- 52. Element item() 方法
- hadoop序列化机制与java序列化机制对比
- hdu 1686 Oulipo
- run_time library
- 51. Element replaceChild() 方法
- UVa11991(map+vector)
- centOS配置mysql+appache+vsftp=php服务器
- Android圆角Tag控件的另类实现
- win8.1 64位环境建设android开发环境
- Struts2里面 s:textfield标签 Date类型的格式化以及日期时间控件使用
- 互联网公司2014前端笔试面试题:JavaScript篇
- 初次使用ticcs注意事项
- websocket入门指南
- C/C++校招笔试面试经典题目总结十
- 微信开放平台,公众号第三方平台接入