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

mahout中bayes分类分析—1

阅读更多
引用:http://blog.163.com/jiayouweijiewj@126/blog/static/17123217720113115027394/
实现包括三部分:The Trainer(训练器)、The Model(模型)、The Classifier(分类器)

1、训练

首先,要对输入数据进行预处理,转化成Bayes M/R job读入数据要求的格式,即训练器输入的数据是KeyValueTextInputFormat格式,第一个字符是类标签,剩余的是特征属性(即单词)。以20个新闻的例子来说,从官网上下载的原始数据是一个分类目录,下面每个文件夹名就是类标签,里面是属于此类的一些文档(一个文件是一篇文章)。mahout通过org.apache.mahout.classifier.bayes.PrepareTwentyNewsgroups对数据进行预处理,完成的工作是对于原始的一个分类目录下面的所有文件,依次遍历,并将目录名作为类别名,就这样完成了inputDir-->outputFile的转变。如果要处理html文件的话,那么就需要在BayesFileFormatter调用html clean,extract body text的过程,生成cleaned text。

PrepareTwentyNewsgroups调用org.apache.mahout.classifier.BayesFileFormatter和org.apache.lucene.analysis.standard.StandardAnalyzer进行分析处理,将数据转换成了:类标签作为一个文件的名字,文件里每一个行(一个回车算一行)是包含在此类中的一篇文章(格式是:类标签 一篇文章中的所有单词)。

BayesFileFormatter的作用是把一个目录里的所有文件转换成一个文件,label \t content其中的文件内容全部经过tokenize,词之间加入空格,存入content。BayesFileFormatter.collapse的作用是将一个目录的所有文件加上label处理成MapReduce所需要的按行处理的文件格式,变成1个文件。

其次,调用org.apache.mahout.classifier.bayes.TrainClassifier进行训练,结果生成newsmodel。

这个类会根据命令行参数调用两个训练器:trainCNaiveBayes和trainNaiveBayes,其中trainCNaiveBayes函数调用CBayesDriver类;trainNaiveBayes调用BayesDriver类。

这里只分析org.apache.mahout.classifier.bayes.mapreduce.bayes.BayesDriver类,它实现了BayesJob接口,在runJob函数里调用4个map/reduce作业类:

第一个:BayesFeatureDriver负责Read the features in each document normalized by length of each document

第二个:BayesTfIdfDriver负责Calculate the TfIdf for each word in each label

第三个:BayesWeightSummerDriver负责alculate the Sums of weights for each label, for each feature

第四个:BayesThetaNormalizerDriver负责:Calculate the normalization factor Sigma_W_ij for each complement class

以20-news的例子分别分析这四个类:

⑴BayesFeatureDriver

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

输入格式:KeyValueTextInputFormat.class

输出格式:BayesFeatureOutputFormat.class

输出key类型:StringTuple.class

输出value类型:DoubleWritable.class

Map:BayesFeatureMapper.class

Reduce:BayesFeatureReducer.class

注意:BayesFeatureDriver可以独立运行,默认的输入和输出:(没有试过,不知道可不可以独立运行,里面有main函数,应该是可以的,不过本人水平有限,目前不会)

input=new Path("/home/drew/mahout/bayes/20news-input");

output=new Path("/home/drew/mahout/bayes/20-news-features");

p=new BayesParameters(1) gramsize默认为1

BayesFeatureOutputFormat继承了MultipleOutputFormat,定义了产生的四个文件路径及名字,文件的格式还是SequenceFileOutputFormat.:

$OUTPUT/trainer-wordFreq

$OUTPUT/trainer-termDocCount

$OUTPUT/trainer-featureCount

$OUTPUT/trainer-docCount

①BayesFeatureMapper的输出为:

一行一个map,根据数据处理的格式即一篇文章一个map,以下的label指类标签,token是属性即单词,dKJ是某token在本篇文章中出现的次数,∑dKJ2是本篇文章中所有token出现次数的平方和,以下及后面的表格是觉得看着清楚自己画的,输出时只是里面的内容,例如:_WT,label,token空格value的值

key


value

_WT,label,token


Log[(1.0+dKJ)/(∑dKJ2)1/2]即为某词在一个文档中的TF值

通俗点就是:Log[(1.0+某属性在本篇文章中出现的次数)/(本篇文章中所有属性出现次数的平方和)1/2]

_DF,label,token


1.0

_FC,token


1.0

_LC,label


1.0

②BayesFeatureReducer的输出为:

相同的key放在一个reduce里执行合并

key


value


输出

_WT,label,token


∑Log[(1.0+dKJ)/(∑dKJ2)1/2]即某类中某属性的TF值


trainer-wordFreq

_DF,label,token


某label中出现某token的文档数


trainer-termDocCount

_FC,token


所有训练集文章中出现某token的文档数


trainer-featureCount

_LC,label


某label下的文档数


trainer-docCount

_FT,token


与_FC的value一样


没输出且只在mhaout-0.4里出现这部分计算,0.3里没有

⑵BayesTfIdfDriver

输入格式:SequenceFileInputFormat.class

输出格式:BayesTfIdfOutPutFormat.class

输出key类型:StringTuple.class

输出value类型:DoubleWritable.class

输入路径:就是第一个map/reduce生成的trainer-wordFreq、trainer-termDocCount、trainer-featureCount文件

输出:trainer-tfIdf文件

Map:BayesTfIdfMapper.class

Reduce:BayesTfIdfReducer.class

根据BayesFeatureReducer的输出文件计算TF-IDF值,但是只调用了以上的trainer-wordFreq、trainer-termDocCount、trainer-featureCount三个文件,计算完毕后会删除这些中间文件(目前还没搞懂在哪删除的,只看见bayesDriver里面有把),并生成两个文件trainer-tfIdf和trainer-vocabCount(BayesTfIdfOutPutFormat里有)。

①BayesTfIdfMapper的输出为:

TF值是调用trainer-wordFreq中的value

Idf=某类下的文档数/某类下出现该token的文档数

其中某类下出现该token的文档数是调用trainer-termDocCount中的value

key


value

_WT,label,token


TF值

_WT,label,token


logidf

_FS


1.0

②BayesTfIdfReducer的输出为:

key


value


输出

_WT,label,token


TF×logidf


trainer-tfidf

_FS


属性总数


trainer-VocabCount

⑶BayesWeightSummerDriver

输入文件格式:SequenceFileInputFormat.class

输出文件格式:BayesWeightSummerOutputFormat.class

输出key:StringTuple.class

输出value:DoubleWritable.class

输入路径:是第二个map/reduce生成的trainer-tfIdf文件

输出:trainer-weights文件

Map:BayesWeightSummerMapper.class

Reduce:BayesWeightSummerReducer.class

这里只调用了第二个map/reduce生成的trainer-tfIdf,没有调trainer-VocabCount

①BayesWeightSummerMapper的输出为:

key


value

_SJ,token


TFIdf值

_SK,label


TFIdf值

_SJSK


TFIdf值

②BayesWeightSummerReducer的输出为:

key


value


输出

_SJ,token


某属性的全部TFIdf总和


Sigma_j

_SK,label


某类下的所有属性的TFIdf总和


Sigma_k

_SJSK


所有的TFIdf值


Sigma_kSigma_j

⑷BayesThetaNormalizerDriver

输入文件格式:SequenceFileInputFormat.class

输出文件格式:SequenceFileOutputFormat.class

输出key:StringTuple.class

输出value:DoubleWritable.class

输入路径: 第二个map/reduce生成的trainer-tfIdf/下的trainer-tfIdf和trainer-VocabCount,以及trainer-weights/的Sigma_k和Sigma_kSigma_j

输出:trainer-thetaNormalizer文件

①BayesWeightSummerMapper的输出为:

Log里的分子中TFIdf是某类下某属性的TFIdf值,分母中VocabCount是属性总数

key


value

_LTN,label


Log[(TFIdf+1.0)/(sigma_k+VocabCount)]

②BayesWeightSummerReducer的输出为:

将Map的结果合并

key


value

_LTN,label


ΣLog[(TFIdf+1.0)/(sigma_k+VocabCount)]
分享到:
评论

相关推荐

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

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

    Apache.Mahout.Beyond.MapReduce.1523775785

    Chapter 1 Meet Mahout Chapter 2 Setting things up Part II — Coding with Mahout Chapter 3 In-core Algebra Chapter 4 Distributed Algebra Part III — Approximating Distributed Problems Chapter 5 ...

    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 ■ Meet Apache Mahout 1 PART 1 RECOMMENDATIONS ...................................................11 2 ■ Introducing recommenders 13 3 ■ Representing recommender data 26 4 ■ Making ...

    [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