接上文重点分析map操作:

 Vector probabilities = classifier.classify(value.get());// 第一行
Vector selections = policy.select(probabilities); // 第二行
for (Iterator<Element> it = selections.iterateNonZero(); it.hasNext();) {
Element el = it.next();
classifier.train(el.index(), value.get(), el.get()); // 第三行
}

这几句要如何理解?

比如我随机的中心点向量是:

2.9,2.9
3.0,3.0

然后我的所有的输入向量为:

[{1:8.1,0:8.1}, {1:8.0,0:8.0}, {1:7.0,0:7.0}, {1:7.1,0:7.1}, {1:6.1,0:6.1}, {1:6.2,0:6.2}, {1:9.0,0:9.0}, {1:2.0,0:2.0}, {1:7.1,0:7.1}, {1:1.0,0:1.0}, {}, {1:2.1,0:2.1}, {1:2.9,0:2.9}, {1:1.1,0:1.1}, {1:0.1,0:0.1}, {1:3.0,0:3.0}]

那么第一行就是针对一个输入向量,求其到中心点向量的距离,如果我有三个中心点,那么probabilities的size就是3,第二行的作用就是找到probabilities值较大(这里为什么是较大?而不是较小?因为在求距离的时候用到了倒数,这样原来小的就变大了,具体计算过程有时间再分析)的下标值,然后用第三行的方法把这个输入向量分入到其对应的中心点向量。如何分?比如第一个输入向量[8.1,8.1]那么应该把其分入[3.0,3.0],那么第1个中心点向量在第一条记录后,其s0=2,s1=8.1+3.0,s2=8.1*8.1+3.0*3.0 ,一次类推,等全部输入结束后,两个中心点的属性如下:

[2.9,2.9]:  s0=8,  s1={1:12.1,0:12.1}  ,s2={1:27.450000000000003,0:27.450000000000003}

[3.0,3.0]:  s0=10,  s1={1:64.60000000000001,0:64.60000000000001} , s2={1:454.08000000000004,0:454.08000000000004}

然后这两个中心点 输出到reduce;

然后我整体跑了一遍,得到第一个输出结果即cluster-1的结果是两个中心点,为 CL-12{n=8 c=[1.513, 1.513] r=[1.069, 1.069]},

CL-15{n=10 c=[6.460, 6.460] r=[1.917, 1.917]}。

然后我又仿造了Reducer:

package mahout.fansy.kmeans;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.mahout.clustering.Cluster;
import org.apache.mahout.clustering.classify.ClusterClassifier;
import org.apache.mahout.clustering.iterator.ClusterWritable;
import org.apache.mahout.clustering.iterator.ClusteringPolicy;
import org.apache.mahout.common.iterator.sequencefile.PathFilters;
import org.apache.mahout.common.iterator.sequencefile.PathType;
import org.apache.mahout.common.iterator.sequencefile.SequenceFileDirValueIterable;
import org.apache.mahout.math.Vector;
import org.apache.mahout.math.VectorWritable;
import org.apache.mahout.math.Vector.Element; import com.google.common.collect.Lists; public class TestCIReducer { /**
* @param args
*/ private static ClusterClassifier classifier; private static ClusteringPolicy policy; public static void main(String[] args) throws IOException {
setup();
reduce();
} /**
* 仿造setup函数
* @throws IOException
*/
public static void setup() throws IOException{ Configuration conf=new Configuration();
conf.set("mapred.job.tracker", "hadoop:9001"); // 这句是否可以去掉? String priorClustersPath ="hdfs://hadoop:9000/user/hadoop/out/kmeans-output/clusters-0";
classifier = new ClusterClassifier();
classifier.readFromSeqFiles(conf, new Path(priorClustersPath));
policy = classifier.getPolicy();
policy.update(classifier);
}
/**
* 仿造map函数
*/
public static void map(){
List<VectorWritable> vList=getInputData();
for(VectorWritable value: vList){
Vector probabilities = classifier.classify(value.get());
Vector selections = policy.select(probabilities);
for (Iterator<Element> it = selections.iterateNonZero(); it.hasNext();) {
Element el = it.next();
classifier.train(el.index(), value.get(), el.get());
}
}
} /**
* 仿造cleanup函数
*/
public static List<ClusterWritable> cleanup(){
List<Cluster> clusters = classifier.getModels();
List<ClusterWritable> cList=Lists.newArrayList();
ClusterWritable cw = null;
for (int index = 0; index < clusters.size(); index++) {
cw=new ClusterWritable();
cw.setValue(clusters.get(index));
cList.add(cw);
//System.out.println("index:"+index+",cw :"+ cw.getValue().getCenter() );
}
return cList;
} public static void reduce(){
map(); // 给classifier赋值
List<ClusterWritable>cList = cleanup();
ClusterWritable first = null;
for (ClusterWritable cw :cList) {
if (first == null) {
first = cw;
} else {
first.getValue().observe(cw.getValue());
}
}
List<Cluster> models = new ArrayList<Cluster>();
models.add(first.getValue());
classifier = new ClusterClassifier(models, policy);
classifier.close();
System.out.println("value:"+first); } /**
* 获得输入数据
* @return
*/
public static List<VectorWritable> getInputData(){
String input="hdfs://hadoop:9000/user/hadoop/out/kmeans-in-transform/part-r-00000";
Path path=new Path(input);
Configuration conf=new Configuration();
List<VectorWritable> vList=Lists.newArrayList();
for (VectorWritable cw : new SequenceFileDirValueIterable<VectorWritable>(path, PathType.LIST,
PathFilters.logsCRCFilter(), conf)) {
vList.add(cw);
}
return vList;
}
}

但是最终只是输出了一个中心点,结果有误?应该是我仿造的代码有问题,明天继续。。。

分享,快乐,成长

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

mahout源码KMeansDriver分析之五CIMapper的更多相关文章

  1. mahout源码KMeansDriver分析之五CIMapper初探

    接着上篇,继续分析代码.下面就到了MR的循环了,这里MR应该算是比较好理解的,重点是退出循环的条件设置,即如何判断前后两次中心点误差小于给定阈值. 首先,while循环: while (iterati ...

  2. mahout源码KMeansDriver分析之四

    昨天说到为什么Configuration没有设置conf.set("mapred.job.tracker","hadoop:9000")仍然可以访问hdfs文件 ...

  3. Mahout源码MeanShiftCanopyDriver分析之二MeanShiftCanopyMapper仿造

    首先更正一点,昨天处理数据的时候是有问题的,直接从网页中拷贝的文件的空格是有问题的,直接拷贝然后新建的文件中的空格可能有一个两个.三个的,所以要把两个或者三个的都换为一个,在InputMapper中下 ...

  4. Mahout源码目录说明&&算法集

    Mahout源码目录说明 mahout项目是由多个子项目组成的,各子项目分别位于源码的不同目录下,下面对mahout的组成进行介绍: 1.mahout-core:核心程序模块,位于/core目录下: ...

  5. mybatis源码配置文件解析之五:解析mappers标签(解析XML映射文件)

    在上篇文章中分析了mybatis解析<mappers>标签,<mybatis源码配置文件解析之五:解析mappers标签>重点分析了如何解析<mappers>标签中 ...

  6. MapReduce的ReduceTask任务的运行源码级分析

    MapReduce的MapTask任务的运行源码级分析 这篇文章好不容易恢复了...谢天谢地...这篇文章讲了MapTask的执行流程.咱们这一节讲解ReduceTask的执行流程.ReduceTas ...

  7. Activity源码简要分析总结

    Activity源码简要分析总结 摘自参考书籍,只列一下结论: 1. Activity的顶层View是DecorView,而我们在onCreate()方法中通过setContentView()设置的V ...

  8. MapReduce的MapTask任务的运行源码级分析

    TaskTracker任务初始化及启动task源码级分析 这篇文章中分析了任务的启动,每个task都会使用一个进程占用一个JVM来执行,org.apache.hadoop.mapred.Child方法 ...

  9. TaskTracker任务初始化及启动task源码级分析

    在监听器初始化Job.JobTracker相应TaskTracker心跳.调度器分配task源码级分析中我们分析的Tasktracker发送心跳的机制,这一节我们分析TaskTracker接受JobT ...

随机推荐

  1. 高仿精仿快播应用android源码下载

    今天给大家在网上找到的一款高仿精仿快播应用android源码,分享给大家,希望大家功能喜欢. 说明源码更新中.... 源码即将上传 也可以到这个网站下载:download

  2. Python 技巧

    1.根据路径导入模块 如果想引用指定路径下的某个模块,则需要使用sys.path.append("module_directory") 来把这个路径添加到sys下,这就涉及到Pyt ...

  3. c# in depth之泛型的类型约束详细

    类型约束 1.引用类型约束 这种约束(表示成T:class,必须是为类型参数指定的第一个约束)用于确保使用的类型实参是引用类型,这可能是任何类,接口,数组,委托或者已知是引用类型的另一个类型参数. 例 ...

  4. WinExec函数,启动其他应用程序

    WinExec The WinExec function runs the specified application. Note  This function is provided only fo ...

  5. hdu 4712 Hamming Distance bfs

    我的做法,多次宽搜,因为后面的搜索扩展的节点会比较少,所以复杂度还是不需要太悲观的,然后加上一开始对答案的估计,用估计值来剪枝,就可以ac了. #include <iostream> #i ...

  6. CSS中float属性和clear属性的一些笔记

    在学习CSS的最后一部分内容中,float属性和clear属性比较难以用语言描述,因此在笔记本中无法准确的记录这两个属性的用法.所以在博客园上以图文的形式记录这两种属性的特征,以备以后查阅. 首先,定 ...

  7. Photon的使用

    这几个月给公司一个正在做的半吊子游戏加pvp功能,一个人居然要2个多月弄个 PVP  类似 Dota 对战的游戏.我手里有套现成搭建服务端架构都没敢用起来,这服务器还是太初步了,只是验证了 Boost ...

  8. ThinkPhp学习10

    原文:ThinkPhp学习10 查询操作 Action模块 User下的search public function search(){ //判断username是否已经传入,且不为空 if(isse ...

  9. COCOS2D中对精灵的操作、对图片的各种操作

    内容简要: 1.初始化 2.创建无图的精灵 3.设置精灵贴图大小  4.添加入层中 5.对精灵进行缩放  6.对精灵宽或高进行缩放  7.旋转精灵 8.设置精灵透明度  9.精灵的镜像反转  10.设 ...

  10. Static关键字的作用及使用

    1.使用static声明属性 如果希望一个属性被所有对象共同拥有,可以将其声明为static类型. 声明为static类型的属性或方法,此属性或方法也被称为类方法,可以由类名直接调用. class P ...