`
wbj0110
  • 浏览: 1591225 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

mahout之TestNaiveBayesDriver源码分析

阅读更多

有个参数sequential决定是否本地执行,这里只讲MapReduce执行。
源代码如下,

1
2
3
4
5
6
7
8
9
10
11
12
  private boolean runMapReduce(Map< string , List< String > > parsedArgs) throws IOException,
      InterruptedExceptionClassNotFoundException {
    Path model = new Path(getOption("model"));
    HadoopUtil.cacheFiles(model, getConf());
    //the output key is the expected value, the output value are the scores for all the labels
    Job testJob = prepareJob(getInputPath(), getOutputPath(), SequenceFileInputFormat.class, BayesTestMapper.class,
            Text.class, VectorWritable.class, SequenceFileOutputFormat.class);
    boolean complementary = parsedArgs.containsKey("testComplementary");
    testJob.getConfiguration().set(COMPLEMENTARY, String.valueOf(complementary));
    boolean succeeded = testJob.waitForCompletion(true);
    return succeeded;
  }

首先从训练的模型中得到model,实例化model,也就是将写入的vectors重新读取出来罢了。
testJob只用到了map阶段,如下

1
2
3
4
5
  protected void map(Text key, VectorWritable value, Context context) throws IOExceptionInterruptedException {
    Vector result = classifier.classifyFull(value.get());
    //the key is the expected value
    context.write(new Text(key.toString().split("/")[1])new VectorWritable(result));
  }

输出的key就是类别的text,value就是输入的向量在每个类的得分。
classifier.classifyFull()计算输入的向量在每个label的得分:

1
2
3
4
5
6
7
  public Vector classifyFull(Vector instance) {
    Vector score = model.createScoringVector();
    for (int label = 0; label < model.numLabels(); label++) {
      score.set(label, getScoreForLabelInstance(label, instance));
    }
    return score;
  }

getScoreForLabelInstance如下,计算此label下的feature得分和。

1
2
3
4
5
6
7
8
9
10
  protected double getScoreForLabelInstance(int label, Vector instance) {
    double result = 0.0;
    Iterator<element> elements = instance.iterateNonZero();
    while (elements.hasNext()) {
      Element e = elements.next();
      result += e.get() * getScoreForLabelFeature(label, e.index());
    }
    return result;
  }
</element>

getScoreForLabelFeature有两种计算方式,
1, 标准bayes ,log[(Wi+alphai)/(ƩWi + N)]

1
2
3
4
5
6
7
8
9
10
11
12
  public double getScoreForLabelFeature(int label, int feature) {
    NaiveBayesModel model = getModel();
return
computeWeight(model.weight(label, feature), model.labelWeight(label), model.alphaI(),
        model.numFeatures());
  }
  public static double computeWeight(double featureLabelWeight, double labelWeight, double alphaI,
      double numFeatures) {
    double numerator = featureLabelWeight + alphaI;
    double denominator = labelWeight + alphaI * numFeatures;
    return Math.log(numerator / denominator);
  }

2, complementary bayes,也就是计算除此类之外的其他类的值。

1
2
3
4
5
6
7
8
9
10
11
12
//complementary bayes
    public double getScoreForLabelFeature(int label, int feature) {
    NaiveBayesModel model = getModel();
    return computeWeight(model.featureWeight(feature), model.weight(label, feature),
        model.totalWeightSum(), model.labelWeight(label), model.alphaI(), model.numFeatures());
  }
  public static double computeWeight(double featureWeight, double featureLabelWeight,
      double totalWeight, double labelWeight, double alphaI, double numFeatures) {
    double numerator = featureWeight - featureLabelWeight + alphaI;
    double denominator = totalWeight - labelWeight + alphaI * numFeatures;
    return -Math.log(numerator / denominator);
  }

最后就是analyze了,对每个key,通过score vector得到最大值,与label index比较。产生confusion matrix了。

http://hnote.org/big-data/mahout/mahout-testnaivebayesdriver-testnb

 

分享到:
评论

相关推荐

    Mahout源码

    Mahout 构建在Hadoop之上,利用MapReduce进行分布式计算。这意味着,对于处理大量数据,Mahout 可以在多台机器上并行运行,大大提高了计算效率。Hadoop的输入/输出机制与Mahout相结合,使得大数据处理变得简单易行。...

    mahout源码

    在大数据时代,Mahout已经成为数据科学家和工程师们的重要工具,尤其在文本分析、推荐系统和分类任务中扮演着关键角色。本篇将深入探讨Mahout中的朴素贝叶斯分类以及中文分词这两个核心功能。 一、Mahout与朴素...

    Mahout教程内含源码以及说明书可以自己运行复现.zip

    安装Mahout首先需要准备Hadoop环境,因为Mahout是构建在Hadoop之上的。你需要下载并安装Hadoop,配置Hadoop环境变量,并确保集群运行正常。接着,从Apache官方网站获取Mahout的最新版本,解压后将其添加到你的系统...

    mahout0.9源码(支持hadoop2)

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

    mahout in action中的源码

    《Mahout in Action》是一本深入探讨Apache Mahout机器学习框架的专业书籍,其源码提供了丰富的实践示例和深入理解Mahout算法的机会。在GitHub上,你可以找到这些源码的完整版本,链接为。下面,我们将详细探讨...

    mahout0.9 源码

    以上就是关于Mahout 0.9源码及其在Eclipse中的使用介绍。通过学习和实践,开发者可以利用Mahout构建强大的机器学习应用,处理各种数据挖掘任务。在实际应用中,可以根据项目需求选择合适的算法,结合Hadoop分布式...

    Mahout之Item-based应用使用

    《Mahout之Item-based应用使用》 Apache Mahout是一个开源的机器学习库,主要专注于大规模数据集上的推荐系统、分类和聚类算法。在这个主题中,我们将深入探讨Mahout中的Item-based协同过滤(Item-based ...

    人工智能-推荐系统-新闻推荐-基于Mahout的新闻推荐系统

    Mahout:整体框架,实现了协同过滤 Deeplearning4j,构建VSM Jieba:分词,关键词提取 HanLP:分词,关键词提取 Spring Boot:提供API、ORM 关键实现 基于用户的协同过滤 直接调用Mahout相关接口即可 选择不同...

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

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

    mahout所需jar包

    Mahout的目标是帮助开发人员构建智能应用程序,如推荐系统、分类和聚类算法,这些在大数据分析领域中极为重要。 **K-Means聚类算法** K-Means是一种无监督学习的聚类算法,用于将数据集分成不同的群组或类别。在...

    mahout-core-0.9.jar+mahout-core-0.8.jar+mahout-core-0.1.jar

    Mahout是建立在Hadoop之上的,利用其分布式计算能力处理大规模数据集。这使得Mahout能够处理超出单台机器内存和计算能力的数据。 3. **版本差异**: - mahout-core-0.1.jar:这是早期版本,可能包含的基本功能,...

    [Mahout] Windows下Mahout单机安装

    打开命令行,进入解压后的Mahout源码目录,执行以下Maven命令来构建Mahout: ``` mvn clean install -DskipTests ``` 这个过程可能会比较耗时,因为Maven会自动下载所有依赖。等待编译完成后,Mahout的可执行jar文件...

    apache-mahout-distribution-0.11.0-src.zip

    在源码中,您可以探索Mahout实现的各种算法,如协同过滤(Collaborative Filtering)、频繁项集挖掘(Frequent Itemset Mining)、近邻搜索(Nearest Neighbor Search)等。这些算法是通过Java编程语言实现的,因此...

    【甘道夫】通过Mahout构建贝叶斯文本分类器案例详解 -- 配套源码

    Apache Mahout是一个基于Hadoop的机器学习库,它提供了一系列的算法,包括聚类、分类和协同过滤,用于大数据分析。贝叶斯分类器是其中一种常用的文本分类方法,因其简单高效而在实际应用中广泛使用。 首先,我们要...

    mahout-distribution-0.7-src.zip

    《Apache Mahout 0.7源码解析与应用探索》 Apache Mahout 是一个开源机器学习库,专注于大规模数据集的算法实现。该库由Java编写,并采用Maven作为构建工具,提供了一系列用于构建智能应用的高效算法。本文将深入...

    spring-mahout-demo

    在项目中,"bhagyas-spring-mahout-demo-f25ecc6"这个文件可能是项目的源码仓库,包含了整个示例的完整代码。通过分析这些代码,我们可以学习到以下几点关键知识点: 1. **Spring配置**:Spring配置文件中会包含对...

    maven_mahout_template-mahout-0.8

    《Apache Maven与Mahout实战:基于maven_mahout_template-mahout-0.8的探索》 Apache Maven是一款强大的项目管理和依赖管理工具,广泛应用于Java开发领域。它通过一个项目对象模型(Project Object Model,POM)来...

Global site tag (gtag.js) - Google Analytics