接着上篇blog,继续看log里面的信息如下:

+ echo 'Training Naive Bayes model'
Training Naive Bayes model
+ ./bin/mahout trainnb -i /home/mahout/mahout-work-mahout/20news-train-vectors -el -o /home/mahout/mahout-work-mahout/model -li /home/mahout/mahout-work-mahout/labelindex -ow

这里mahout的trainnb对应的源码文件是TrainNaiveBayesJob类,该类主要的工作是:(1)新建了一个LabelIndex的文件;(2)执行了一个prepareJob,Mapper和Reducer分别是:IndexInstancesMapper、VectorSumReducer;(2)执行了另外的一个prepareJob,Mapper和Reducer分别是:WeightsMapper、VectorSumReducer;本篇主要分析前面两个工作。

新建LabelIndex的代码如下:

private long createLabelIndex(Path labPath) throws IOException {
long labelSize = 0;
if (hasOption(LABELS)) {
Iterable<String> labels = Splitter.on(",").split(getOption(LABELS));
labelSize = BayesUtils.writeLabelIndex(getConf(), labels, labPath);
} else if (hasOption(EXTRACT_LABELS)) {
SequenceFileDirIterable<Text, IntWritable> iterable =
new SequenceFileDirIterable<Text, IntWritable>(getInputPath(), PathType.LIST, PathFilters.logsCRCFilter(), getConf());
labelSize = BayesUtils.writeLabelIndex(getConf(), labPath, iterable);
}
return labelSize;
}

这里的主要工作是把相关的文件名转换为数字,文件名如下图:

下面看Mapper,IndexInstancesMapper的主要代码如下:

 labelIndex = BayesUtils.readIndexFromCache(ctx.getConfiguration());
String label = labelText.toString().split("/")[1];
if (labelIndex.containsKey(label)) {
ctx.write(new IntWritable(labelIndex.get(label)), instance);

首先在setup函数中读取labelindex的map映射关系,然后在map中针对输入/alt.atheism/51060解析/后面的字符串,即文件名进行匹配,输出对应的数字和相应的value不变;

VectorSumReducer:

 Vector vector = null;
for (VectorWritable v : values) {
if (vector == null) {
vector = v.get();
} else {
vector.assign(v.get(), Functions.PLUS);
}
}
ctx.write(key, new VectorWritable(vector));

上面的代码就是把相同的文件对应的word的单词的个数全部加起来,由于一共有20个文件,所以这里的reduce输出应该有20个,对应log里面的信息,可以看到确实匹配,如下图:

这里额可以通过下面的代码来测试相关的文件:

package mahout.fansy.test.bayes.read;

import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.Map; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.util.ReflectionUtils;
import org.apache.mahout.math.VectorWritable; public class ReadLabelIndex { /**
* @param args
*/
public static Configuration conf=new Configuration();
static String fPath="";
static String trainPath="";
static{
conf.set("mapred.job.tracker", "ubuntu:9001");
fPath="hdfs://ubuntu:9000/home/mahout/mahout-work-mahout/labelindex"; // lableindex 数据文件
trainPath="hdfs://ubuntu:9000/home/mahout/mahout-work-mahout/"
+"20news-train-vectors/part-r-00000"; // 训练样本数据
}
public static void main(String[] args) throws IOException {
// readFromFile(fPath);
readFromFile(trainPath);
} /**
* 读取LabelIndex文件
* @param fPath
* @return
* @throws IOException
*/
public static Map<Writable,Writable> readFromFile(String fPath) throws IOException{
FileSystem fs = FileSystem.get(URI.create(fPath), conf);
Path path = new Path(fPath);
Map<Writable,Writable> map=new HashMap<Writable,Writable>();
SequenceFile.Reader reader = null;
try {
reader = new SequenceFile.Reader(fs, path, conf);
Writable key = (Writable)
ReflectionUtils.newInstance(reader.getKeyClass(), conf);
Writable value = (Writable)
ReflectionUtils.newInstance(reader.getValueClass(), conf);
while (reader.next(key, value)) {
// Writable k=; // 如何实现Writable的深度复制?
// map.put(key, value);
System.out.println(key.toString()+", "+value.toString());
System.exit(-1);// 只打印第一条记录
}
} finally {
IOUtils.closeStream(reader);
}
return map;
} }

这里在写的时候想做一个通用的,所以需要对Writable深度复制,但是一时间还没有想到办法,所以这里留个问题,有时间解决。

分享,成长,快乐

转载请注明blog地址:http://blog.csdn.net/fansy1990

Twenty Newsgroups Classification实例任务之TrainNaiveBayesJob(一)的更多相关文章

  1. mahout 运行Twenty Newsgroups Classification实例

    按照mahout官网https://cwiki.apache.org/confluence/display/MAHOUT/Twenty+Newsgroups的说法,我只用运行一条命令就可以完成这个算法 ...

  2. Twenty Newsgroups Classification任务之二seq2sparse(5)

    接上篇blog,继续分析.接下来要调用代码如下: // Should document frequency features be processed if (shouldPrune || proce ...

  3. Twenty Newsgroups Classification任务之二seq2sparse(3)

    接上篇,如果想对上篇的问题进行测试其实可以简单的编写下面的代码: package mahout.fansy.test.bayes.write; import java.io.IOException; ...

  4. Twenty Newsgroups Classification任务之二seq2sparse

    seq2sparse对应于mahout中的org.apache.mahout.vectorizer.SparseVectorsFromSequenceFiles,从昨天跑的算法中的任务监控界面可以看到 ...

  5. Twenty Newsgroups Classification任务之二seq2sparse(2)

    接上篇,SequenceFileTokenizerMapper的输出文件在/home/mahout/mahout-work-mahout0/20news-vectors/tokenized-docum ...

  6. W3School-CSS 分类 (Classification) 实例

    CSS 分类 (Classification) 实例 CSS 实例 CSS 背景实例 CSS 文本实例 CSS 字体(font)实例 CSS 边框(border)实例 CSS 外边距 (margin) ...

  7. CSS 分类 (Classification) 实例

    CSS 分类 (Classification) 实例CSS 分类属性 (Classification)CSS 分类属性允许你控制如何显示元素,设置图像显示于另一元素中的何处,相对于其正常位置来定位元素 ...

  8. W3School-CSS 伪元素 (Pseudo-elements) 实例

    CSS 伪元素 (Pseudo-elements)实例 CSS 实例 CSS 背景实例 CSS 文本实例 CSS 字体(font)实例 CSS 边框(border)实例 CSS 外边距 (margin ...

  9. W3School-CSS 伪类 (Pseudo-classes) 实例

    CSS 伪类 (Pseudo-classes) 实例 CSS 实例 CSS 背景实例 CSS 文本实例 CSS 字体(font)实例 CSS 边框(border)实例 CSS 外边距 (margin) ...

随机推荐

  1. C++静态库中使用_declspec(dllexport) 不能导出函数的问题

    在某项目中,有一些静态库,这些静态库中有类型命名的函数GET_XXX.在一次项目结构调整的时候,我想将调用这静态库的代码编译成DLL,并且将这些Get函数导出,我就直接就这些函数前面添加了_decls ...

  2. Android 锁屏状态/锁屏密码等相关

    Android 锁屏状态/锁屏密码等相关 开始是在设备管理器方面找方法,但一直不行,可能在公司系统组同事的帮助下,知道KeyguardManager这个类 /** * 当前系统锁屏是否有密码 * @p ...

  3. Jquery学习笔记:删除节点的操作

    假设如下的html代码 <div id="mydiv" style="width:100px;height:100px;border:1px solid red&q ...

  4. The Apache™ Batik Project

    Apache(tm) Batik SVG Toolkit - a Java-based toolkit for applications or applets that want to use ima ...

  5. IE保护模式下ActiveX控件打不开共享内存的解决方案

    原文:http://www.cppblog.com/Streamlet/archive/2012/10/25/193831.html 感谢溪流漫话的投递 IE保护模式下,ActiveX控件会打不开别的 ...

  6. 终于懂了:TWinControl主要是Delphi官方用来封装Windows的官方控件,开发者还是应该是有TCustomControl来开发三方控件

    再具体一点,就是TWinControl一般情况下不需要Canvas和Paint(TForm是个例外),而TCustomControl自带这2个. 同时开发者应该使用TGraphicControl,而不 ...

  7. 基于visual Studio2013解决面试题之1204大数组查找

     题目

  8. salon_百度百科

    salon_百度百科     salon    编辑    是法语Salon一字的译音,中文意即客厅,原指法国上层人物住宅中的豪华会客厅.从十七世纪,巴黎的名人(多半是名媛贵妇)常把客厅变成著名的社交 ...

  9. Jquery Mobile转场特效之slide | 小小iPhone开发

    Jquery Mobile转场特效之slide | 小小iPhone开发 2012 Jquery Mobile转场特效之slide 作者:小小   发布:2012-12-12 14:03   分类:j ...

  10. 【解决方法】System.IO.FileNotFoundException

    错误日志 See the end of this message for details on invoking just-in-time (JIT) debugging instead of thi ...