`
thecloud
  • 浏览: 882975 次
文章分类
社区版块
存档分类
最新评论

mahout决策树之Partial Implementation源码分析 part4

 
阅读更多

今天来说,应该是把所有Partial Implementation的内容分析完了(当然也只是分析了属性是离散值的情况的数据,而非离散的并没有分析),下面就说下Partial Implementation实战的第三部分:TestForest,这个源文件在$MAHOUT_HOME/example/src/main/java/org/apache/mahout/classifier/df/mapreduce里面,打开源码可以看到:

TestForest里面的主要操作如下:

1.设置参数,直接调用 testForest()方法,在testForest()方法中首先确认(1)输出路径不存在;(2)确认forestt存在;(3)确认data存在;

// make sure the output file does not exist
    if (outputPath != null) {
      outFS = outputPath.getFileSystem(getConf());
      if (outFS.exists(outputPath)) {
        throw new IllegalArgumentException("Output path already exists");
      }
    }
    // make sure the decision forest exists
    FileSystem mfs = modelPath.getFileSystem(getConf());
    if (!mfs.exists(modelPath)) {
      throw new IllegalArgumentException("The forest path does not exist");
    }
    // make sure the test data exists
    dataFS = dataPath.getFileSystem(getConf());
    if (!dataFS.exists(dataPath)) {
      throw new IllegalArgumentException("The Test data path does not exist");
    }
2.一般都是使用mapreduce方法,而非单机,所以直接调用mapreduce()方法,mapreduce()方法如下

定义了Classifier 类,然后直接执行其run()方法:

Classifier classifier = new Classifier(modelPath, dataPath, datasetPath, outputPath, getConf());

classifier.run();
3. 打开Classifier源文件,可以看到这个类定义了一个Job,其Mapper为CMapper(属于Classifier的静态内部类),Cmapper的setup() 方法主要是进行一些参数的设定工作,map()方法如下:

3.1 首先获得 数据转换 ,即把Text的输入转为Instance:

Instance instance = converter.convert(line);
3.2 直接调用forest.classify()方法进行对instance的分类:

double prediction = forest.classify(dataset, rng, instance);

3.2.1forest的classify方法是去遍历我们得到的全部的tree,然后每棵树都会有一个预测的分类结果,把这些全部加起来,取次数最多的分类结果

 int[] predictions = new int[dataset.nblabels()];
      for (Node tree : trees) {
        double prediction = tree.classify(instance);
        if (prediction != -1) {
          predictions[(int) prediction]++;
        }
      }
  
 return DataUtils.maxindex(rng, predictions);

3.2.2每一个分类结果都会查询到叶子节点,叶子节点即Leaf直接返回该节点的label值:

public double classify(Instance instance) {
    return label;
  }
3.3 把原始已知的分类结果设置为key,把预测的分类结果设置为value进行输出:

lkey.set(dataset.getLabel(instance));
lvalue.set(Double.toString(prediction));
context.write(lkey, lvalue);
4. 接着Classifier把mapper的输出结果复制到输出路径,并删除Mapper的输出结果;
parseOutput(job);

HadoopUtil.delete(conf, mappersOutputPath);


分享,快乐,成长


转载请注明出处:http://blog.csdn.net/fansy1990


分享到:
评论

相关推荐

    mahout Algorithms源码分析

    mahoutAlgorithms源码分析 mahout代码解析

    mahout-core-0.7-job.jar

    用于测试mahout中的决策树 ,即Partial Implementation用到的测试jar包。所谓的测试其实也只是把相应的数据可以打印出来,方便单机调试,理解算法实现原理而已。

    Mahout源码

    Mahout是一个Java的机器学习库。Mahout的完整源代码,基于maven,可以轻易导入工程中

    mahout源码

    mahout,朴素贝叶斯分类,中文分词,mahout,朴素贝叶斯分类,中文分词,

    Mahout_In_Action(源码)

    Mahout in Action 源码,结合Mahout in Action 学习数据挖掘,比较容易理解

    mahout in action中的源码

    该资源是mahout in action 中的源码,适用于自学,可在github下载:https://github.com/tdunning/MiA

    Mahout算法调用展示平台2.1-part3

    第三部分 功能主要包括四个方面:集群配置、集群算法监控、Hadoop模块、Mahout模块。 详情参考《Mahout算法调用展示平台2.1》

    Mahout算法调用展示平台2.1-part2

    第二部分 功能主要包括四个方面:集群配置、集群算法监控、Hadoop模块、Mahout模块。 详情参考《Mahout算法调用展示平台2.1》

    mahout-distribution-0.5-src.zip mahout 源码包

    mahout-distribution-0.5-src.zip mahout 源码包

    Apache.Mahout.Beyond.MapReduce.1523775785

    Apache Mahout: Beyond MapReduce. Distributed algorithm design This book is about designing mathematical and Machine Learning algorithms using the Apache Mahout "Samsara" platform. The material takes...

    svd mahout算法

    svd算法的工具类,直接调用出结果,调用及设置方式参考http://blog.csdn.net/fansy1990 <mahout源码分析之DistributedLanczosSolver(七)>

    mahout 0.7 src

    mahout 0.7 src, mahout 源码包, hadoop 机器学习子项目 mahout 源码包

    mahout-distribution-0.5.tar.gz + 源码

    mahout实战 源码 mahout实战 配套 mahout-distribution-0.5.tar.gz 版本

    MAHOUT源码包

    Mahout 是 Apache Software Foundation(ASF) 旗下的一个开源项目,提供一些可扩展的机器学习领域经典算法的实现,旨在帮助开发人员更加方便快捷地创建智能应用程序。Mahout包含许多实现,包括聚类、分类、推荐过滤...

    mahout api 学习资料

    mahout_help,mahout的java api帮助文档,可以帮你更轻松掌握mahout

    mahout0.9源码(支持hadoop2)

    mahout0.9的源码,支持hadoop2,需要自行使用mvn编译。mvn编译使用命令: mvn clean install -Dhadoop2 -Dhadoop.2.version=2.2.0 -DskipTests

    maven_mahout_template-mahout-0.8

    maven_mahout_template-mahout-0.8

    Mahout in Action

    MapReduce: matrix multiplication by partial products 102 Translating to MapReduce: making recommendations 105 6.4 Running MapReduces with Hadoop 107 Setting up Hadoop 107 ■ Running recommendations ...

    Apache Mahout Cookbook code

    Thank you for requesting the download for Apache Mahout Cookbook. Please click the following link to download the code:

Global site tag (gtag.js) - Google Analytics