mahout之bytes分类

来源:互联网 发布:calendar.java 编辑:程序博客网 时间:2024/05/16 08:39

实现包括三部分: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 cleanextract body text的过程,生成cleaned text

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

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

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

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

这里只分析org.apache.mahout.classifier.bayes.mapreduce.bayes.BayesDriver类,它实现了BayesJob接口,在runJob函数里调用4map/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

MapBayesFeatureMapper.class

ReduceBayesFeatureReducer.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)/(∑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-wordFreqtrainer-termDocCounttrainer-featureCount文件

输出:trainer-tfIdf文件

MapBayesTfIdfMapper.class

ReduceBayesTfIdfReducer.class

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

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

输出keyStringTuple.class

输出valueDoubleWritable.class

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

输出:trainer-weights文件

MapBayesWeightSummerMapper.class

ReduceBayesWeightSummerReducer.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

输出keyStringTuple.class

输出valueDoubleWritable.class

输入路径: 第二个map/reduce生成的trainer-tfIdf/下的trainer-tfIdftrainer-VocabCount,以及trainer-weights/Sigma_kSigma_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)]


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

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

BayesClassifierMapper

首先,运行configurealgorithm=new BayesAlgorithm()datastore=new InMemoryDatastoreparams)datastoreInMemoryDatastoreparams)方法将模型装入到datastore中即装入Sigma_jSigma_kSigma_kSigma_jthetaNormalizerweight=TfIdfalpha_i=1.0);classifier=new classifierContext(algorithm,datastore)classifier.initialize(),即初始化classifier,初始化classifierdatastore.initialize()algorithm.initializethis.datastore)。

datastore的初始化:

调用SequenceFileModelReaderloadModel方法(五个Load方法):

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

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

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

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

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

algorithm的初始化:

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

其次,运行map:开始分类classifier.classifyDocument(),classifyDocument()调用algorithm.classifyDocumentnew result{unkonwm,0} categories=label weight即所有的label集合;开始循环:针对每一个类进行循环,调用documenWeight:先计算文档中每个词的次数(frequency),生成一个Mapwordlist,针对wordlisteach pair计算:∑[frequency×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返回的值是测试文档属于某类的概率的大小,即所有属性的在某类下的frequency×result之和与在其他类下的和值进行比较,最大值的,取出它的label,文档就属于此类。

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

BayesClassifierReducer

只是合并map的结果。

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

根据对以上∑(frequency×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=}


原创粉丝点击