接着上篇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. Away3D带你360°漫游全景影像

    1代码展示 package { import away3d.containers.View3D; import away3d.controllers.HoverController; import a ...

  2. 深入浅出单实例Singleton设计模式

    深入浅出单实例Singleton设计模式 陈皓 单实例Singleton设计模式可能是被讨论和使用的最广泛的一个设计模式了,这可能也是面试中问得最多的一个设计模式了.这个设计模式主要目的是想在整个系统 ...

  3. Js脚本实现选项卡的实例

    效果演示: 具体代码: <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http:/ ...

  4. EasyUi 中datagrid 实现查询方法

    1.在初始化表格方法中添加传入參数,例如以下: //初始化表格 function initTable(<strong><span style="color:#ff6666; ...

  5. 使用aidl绑定远程服务

    一.服务端 1.清单文件,因为要远程调用,所以要配个action <service android:name="com.example.alipayservice.AliPayServ ...

  6. perl encode_json 会产生 UTF-8 (binary) string decode_json 需要一个 UTF-8 (binary) string

    encode_json $json_text = encode_json $perl_scalar Converts the given Perl data structure to a UTF-8 ...

  7. 基于visual Studio2013解决面试题之1309求子集

     题目

  8. HDOJ 1800 Flying to the Mars 盲目搜索......................so easy...........

    check the original problem here:http://acm.hdu.edu.cn/showproblem.php?pid=1800 the AC code: #include ...

  9. 1352 - Colored Cubes (枚举方法)

    There are several colored cubes. All of them are of the same size but they may be colored differentl ...

  10. javaee加密部署,tomcat使用自己的classloader解密【正解】

    [起因] 公司需要对一个web项目进行加密之后出售, 大家都知道的,class很好反编译, 所以需要对class文件先进行加密, 然后使用自己的classloader进行解密并加载. [步骤] 大概分 ...