没有合适的资源?快使用搜索试试~ 我知道了~
首页基于MapReduce实现决策树算法
资源详情
资源评论
资源推荐

基于基于MapReduce实现决策树算法实现决策树算法
主要为大家详细介绍了基于MapReduce实现决策树算法,具有一定的参考价值,感兴趣的小伙伴们可以参考一
下
本文实例为大家分享了MapReduce实现决策树算法的具体代码,供大家参考,具体内容如下
首先,基于C45决策树算法实现对应的Mapper算子,相关的代码如下:
public class MapClass extends MapReduceBase implements Mapper {
private final static IntWritable one = new IntWritable(1);
private Text attValue = new Text();
private int i;
private String token;
public static int no_Attr;
public Split split = null;
public int size_split_1 = 0;
public void configure(JobConf conf){
try {
split = (Split) ObjectSerializable.unSerialize(conf.get("currentsplit"));
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
size_split_1 = Integer.parseInt(conf.get("current_index"));
}
public void map(LongWritable key, Text value, OutputCollector output, Reporter reporter)
throws IOException {
String line = value.toString(); // changing input instance value to
// string
StringTokenizer itr = new StringTokenizer(line);
int index = 0;
String attr_value = null;
no_Attr = itr.countTokens() - 1;
String attr[] = new String[no_Attr];
boolean match = true;
for (i = 0; i < no_Attr; i++) {
attr[i] = itr.nextToken(); // Finding the values of different
// attributes
}
String classLabel = itr.nextToken();
int size_split = split.attr_index.size();
Counter counter = reporter.getCounter("reporter-"+Main.current_index, size_split+" "+size_split_1);
counter.increment(1l);
for (int count = 0; count < size_split; count++) {
index = (Integer) split.attr_index.get(count);
attr_value = (String) split.attr_value.get(count);
if (!attr[index].equals(attr_value)) {
match = false;
break;
}
}
if (match) {
for (int l = 0; l < no_Attr; l++) {
if (!split.attr_index.contains(l)) {
//表示出某个属性在某个类标签上出现了一次
token = l + " " + attr[l] + " " + classLabel;
attValue.set(token);
output.collect(attValue, one);
}
else{
}
}
if (size_split == no_Attr) {
token = no_Attr + " " + "null" + " " + classLabel;
attValue.set(token);
output.collect(attValue, one);
}

















安全验证
文档复制为VIP权益,开通VIP直接复制

评论0