mahout中bayes分类分析—1

实现包括三部分: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

所在包:packageorg.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=newPath("/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)/(∑dKJ21/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)/(∑dKJ21/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)]

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值