2015-04-21 27 views
0

我想用Weka打印J48分类器的混淆矩阵。我得到的输出是每个映射器的矩阵数。运行的映射器数量设置为两个。为了得到从减速器只有一个汇总值

这个类是Weka分类器输出的缩减器它给出了来自映射器的一串交叉验证的数据块,它的工作是将数据聚合成一个解决方案。

public void reduce(Text key, Iterable<AggregateableEvaluation> values, Context context) throws IOException, InterruptedException {  
     int sum = 0;      
     // loop through each of the values and "aggregate" 
     // which basically means to consolidate the values 
     for (AggregateableEvaluation val : values) { 
      System.out.println("IN THE REDUCER!"); 

      // The first time through, give aggEval a value 
      if (sum == 0) { 
       try { 
        aggEval = val; 
       } 
       catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
      else { 
       // combine the values 
       aggEval.aggregate(val); 
      } 

      try { 
       // This is what is taken from the mapper to be aggregated 
       //System.out.println("This is the map result"); 
       //System.out.println(aggEval.toMatrixString()); 
      } 
      catch (Exception e) { 
       e.printStackTrace(); 
      }      

      sum += 1; 
     }   
     try { 
      System.out.println("This is reduce matrix"); 
      System.out.println(aggEval.toMatrixString()); 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 
+1

不要得到它。什么是问题? – Zelldon

+0

@Zelldon我想打印一个混淆矩阵,即weka输出,但我得到很多矩阵......我不确定,但它的像reducer正在执行许多次。 – Amogh

回答

0

我不知道什么WEKA,但与“正常”的MapReduce,您减少函数应该是这样的形式:https://hadoop.apache.org/docs/current/api/org/apache/hadoop/mapreduce/Reducer.html

public class IntSumReducer<Key> extends Reducer<Key,IntWritable, 
               Key,IntWritable> { 
    private IntWritable result = new IntWritable(); 

    public void reduce(Key key, Iterable<IntWritable> values, 
         Context context) throws IOException, InterruptedException { 
    int sum = 0; 
    for (IntWritable val : values) { 
     sum += val.get(); 
    } 
    result.set(sum); 
    context.write(key, result); 
    } 
} 

所以基本上,的减速方法被调用一次为,每个您的密钥为。您将获得映射到该特定键的所有值,您应该将这些值集中在一起,然后完成后请执行context.write(key, aggEval)以从缩小方法发出结果

+0

我试着你说的,但问题是,我需要使用AggregateableEvaluation而不是IntWritable,所以context.write不允许(key,aggEval)。它期望键和IntWritable类型 – Amogh

+0

Iterable <>中的类型不必与上下文中发出的类型匹配。所以要么你必须将你的AggregateableEvaluation聚合到IntWritable中,要么你需要改变reduce阶段发出的类型。你会注意到Reducer的形式是。 V2是你正在发射的类型。 – FuriousGeorge

+0

事情是我需要输出矩阵类型的评估类toMatrixString产生一个混淆矩阵..所以我可以不能聚合成IntWritable的权利?并在任何其他类型的发射它不会给矩阵正确的? – Amogh