`
lbxhappy
  • 浏览: 302972 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

mahout中bayes分类分析—2

阅读更多

2、 模型

以上训练部分的四个job 执行完毕后,整个 bayes 模型就建立完毕了,总共生成并保存三个目录文件:

trainer-tfIdf

trainer-weights

trainer-thetaNormalizer

我们可以将模型从分布式上Sequence 文件导成本地的 txt 文件进行查看。

3、测试

调用类:TestClassifier

所在包:package org.apache.mahout.classifier.bayes;

根据命令行参数会选择顺序执行还是并行map/reduce 执行,这里只分析并行 map/reduce ,执行时会调用 BayesClassifierDriver

分析BayesClassifierDriver

runjob runjob 中先运行 BayesClassifierMapper 再是 BayesClassifierReducer job 执行完毕后会调用混合矩阵: ConfusionMatrix 函数显示结果

BayesClassifierMapper

首先, 运行configure algorithm=new BayesAlgorithm() datastore=new InMemoryDatastore params) datastore InMemoryDatastore params) 方法将模型装入到 datastore 中即装入 Sigma_j Sigma_ k Sigma_ k Sigma_j thetaNormalizer weight=TfIdf alpha_i=1.0 ); classifier=new classifierContext(algorithm,datastore) classifier.initialize() ,即初始化 classifier ,初始化 classifier datastore.initialize() algorithm.initialize this.datastore )。

datastore的初始化:

调用SequenceFileModelReader loadModel 方法(五个 Load 方法):

loadFeatureWeights(装入的是 Sigma_j )生成hashmap  Sigma_j {0 weight 1 weight  }其中 0 1 等是属性的标号,weight Sigma_j value

loadLabelWeights(装入的是 Sigma_ k)生成 hashmap  Sigma_ k{0 weight 1 weight  }其中 0 1 等是label 即类标签的标号, weight Sigma_ k value

loadSumWeight (装入的是 Sigma_ k Sigma_j )使datastore 的成员变量 Sigma_ j Sigma_ k=value(训练得到的所有 tfidf 总和)。

loadThetaNormalizer (装入的是 ThetaNormalizer )生成 hashmap thetaNormalizerPerlabel{0 weight 1 weight  }其中 weight 是传进来的 value ,使 datastore 的成员变量 thetaNormalizer=Max(1.0 |weight|)

loadWeightMatrix (装入的是 weight tfidf )生成 weightMatrix SparseMatrix ,其中行是属性的标号,列是 label 的标号,行列交叉的地方是 tfidf

algorithm的初始化:

调用datastore.getKeys getKeys 返回 labeldicionary.Keys 即返回一个集合,里面放的是所有的 label

其次, 运行map :开始分类 classifier.classifyDocument(),classifyDocument() 调用 algorithm.classifyDocument new result{unkonwm,0} categories= label weight 即所有的label 集合; 开始循环:针对每一个类进行循环,调用documenWeight :先计算文档中每个词的次数( frequency ),生成一个 Map wordlist ,针对 wordlist each pair 计算:∑ [frequenc y×featureweight(datastore,label,word) ]。其中 featureweight共四个,都调用datastore.getWeight,以下分别分析:

①double result = 调用datastore.getWeight,稀疏矩阵的getQuick,取出矩阵的Tfidf值;

②double vocabCount =属性总数;

③double sumLableWeight =Sigma_k的值;

④double numerator =result + 1.0;

⑤double denominator = sumLableWeight + vocabCount;

⑥double weight =log(numerator/denominator)也就是=log[(Tfidf+1.0)/(Sigma_k+属性个数)];

返回的是result = -weight;

所以说, documenWeight返回的值是测试文档属于某类的概率的大小,即所有属性的在某类下的 frequenc y×result之和与在其他类下的和值进行比较,最大值的,取出它的label,文档就属于此类。

key=_CT 正确 label  分类 label  value=1.0

BayesClassifierReducer

只是合并map的结果。

key=_CT 正确 label  分类 label  value= 正确分类的文档数

根据对以上∑(frequenc y×result)进行分析,参照贝叶斯多项式模型, frequency 是对weight中取对数时转移到前面的,即log(numerator/denominator) frequency  frequency ×log(numerator/denominator),weight是条件概率,即log[(numerator/denominator) frequency

×(numerator/denominator) frequency  ] =  log(numerator/denominator) frequency 因为按贝叶斯原理来说,后验概率=先验概率×条件概率,据我理解,此处为什么没有乘先验概率,可能是因为所用的20个新闻的数据每类中的文档数大致一样,先验概率几乎一样,所以没必要乘(个人猜测)。

ConfusionMatrix函数显示结果

key=正确 label  value={key= 分类 label value= }

分享到:
评论

相关推荐

    【甘道夫】通过Mahout构建贝叶斯文本分类器案例详解 -- 配套源码

    http://blog.csdn.net/u010967382/article/details/25368795#comments 博客配套源码

    Apache.Mahout.Beyond.MapReduce.1523775785

    Apache Mahout: Beyond MapReduce. Distributed algorithm design This book is about designing mathematical and Machine Learning algorithms using the Apache Mahout "Samsara" platform. The material takes...

    Learning Apache Mahout Classification

    Next, you will learn about different classification algorithms and models such as the Naïve Bayes algorithm, the Hidden Markov Model, and so on. Finally, along with the examples that assist you in ...

    Mahout in Action

    1.1 Mahout’s story 2 1.2 Mahout’s machine learning themes 3 Recommender engines 3 ■ Clustering 3 ■ Classification 4 1.3 Tackling large scale with Mahout and Hadoop 5 1.4 Setting up Mahout 6 Java ...

    [Mahout] 使用Mahout 对Kddcup 1999的数据进行分析 -- Naive Bayes

    NULL 博文链接:https://rangerwolf.iteye.com/blog/2093940

    Mahout in Action完整版本(英文)

    一些支持 Map-Reduce 的集群实现包括 k-Means、模糊 k-Means、Canopy、Dirichlet 和 Mean-Shift。  Distributed Naive Bayes 和 Complementary Naive Bayes 分类实现。

    Learning Apache Mahout(PACKT,2015)

    You will learn about Mahout building blocks, addressing feature extraction, reduction and the curse of dimensionality, delving into classification use cases with the random forest and Naïve Bayes ...

    yelp-dataset-project:Yelp 学术数据集项目

    yelp-数据集-项目 Yelp 学术数据集项目 该项目分为以下目录:- 1.) DSParser:- a.) 包含解析数据集的库 b.) 与数据库交互 c.) 过滤数据并将数据从文本... 5.) SLURM_Scripts:- 包含运行 Mahout Naive Bayes 的 SLURM

    大数据分析及处理方法.docx

    比较典型算法有用于聚类的K-Means、用于统计学习的SVM和用于分类的Naive Bayes,主要使用的工具有Hadoop的Mahout等。 该过程的特点和挑战主要是用于挖掘的算法很冗杂,并且计算涉及的`数据量和计算量都很大,还有,...

    mahoutr:R的大数据

    MAHOUR,具体是将并行化算法移植到R语言中的一个数据分析平台,由海量数据研究中心负责开发,以下是具体安装步骤。 环境准备:系统要求 ubuntu,jdk最好为1.6 确保hadoop,mahout,R安装正确,并且可以顺利运行。 以下...

Global site tag (gtag.js) - Google Analytics