Mahout源码MeanShiftCanopy聚类算法分析(1)
来源:互联网 发布:linux sqlplus退出 编辑:程序博客网 时间:2024/05/22 12:32
如果要先把meanshift算法先跑一遍的话,可以直接使用synthetic_control.data数据来做,把synthetic_control.data 下载赋值到一个文本文件中,然后上传到HDFS文件系统上面,使用下面的命令: bin/hadoop fs -put synthetic_control.data testdata;上传完毕后直接在mahout中运行:bin/mahout org.apache.mahout.clustering.syntheticcontrol.meanshift.Job 即可在终端中看到运行的情况,但是在最后面该程序把所有的数据结果都显示出来了,导致前面的Job信息看不到了,所以我就改变了下程序的某些部分,编写下面的程序,来进行测试:
package mahout.fansy.test.meanshift;import java.io.IOException;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.Path;import org.apache.mahout.clustering.conversion.meanshift.InputDriver;import org.apache.mahout.clustering.meanshift.MeanShiftCanopyDriver;import org.apache.mahout.common.distance.DistanceMeasure;import org.apache.mahout.common.distance.EuclideanDistanceMeasure;import org.apache.mahout.common.kernel.TriangularKernelProfile;public class TestMeanShift {/** * @param args * @throws Exception */public static void main(String[] args) throws Exception {testMeanShift();}public static void testMeanShift() throws ClassNotFoundException, IOException, InterruptedException{Configuration conf=new Configuration();conf.set("mapred.job.tracker", "ubuntu:9001");//Path input=new Path("hdfs://ubuntu:9000/user/test/input/transform/part-r-00000");Path input=new Path("hdfs://ubuntu:9000/user/test/input/synthetic_control.data");Path realInput=new Path("hdfs://ubuntu:9000/user/test/input/real_input");InputDriver.runJob(input, realInput);Path output=new Path("hdfs://ubuntu:9000/user/test/output-meanshift");DistanceMeasure measure=new EuclideanDistanceMeasure();TriangularKernelProfile kernelProfile =new TriangularKernelProfile();double t1=47.6;double t2=1;double convergenceDelta=0.5;int maxIterations=10;MeanShiftCanopyDriver.run(conf, realInput, output, measure, kernelProfile, t1, t2, convergenceDelta, maxIterations,true,true,false);}}编译,打包上面的程序,然后在终端中运行:bin/hadoop jar ../../mahout_jar/ClusteringUtils.jar mahout.fansy.test.meanshift.TestMeanShift,然后在终端中即可看到显示的信息,主要的信息粘贴如下:
由上面的信息可以看到第一个job,只使用了mapper,把600条记录转换为了600个meanshiftcanopy(等下分析);然后进行了3次循环,每次map和reduce的输入记录数都有变化,分别如下:
map-inmap_outreduce_inreduce_out第一次600600600479第二次47912912946第三次4615157可以看出最后只剩下7个类了,(但是原始数据是6个类的,所以此处的参数还是有待调整的);然后先分析下第一个Job,这个Job就是把原始每条记录都转换为一个meanshiftcanopy,具体代码如下:
InputDriver:
Job job = new Job(conf, "Mean Shift Input Driver running over input: " + input); job.setOutputKeyClass(Text.class); job.setOutputValueClass(ClusterWritable.class); job.setOutputFormatClass(SequenceFileOutputFormat.class); job.setMapperClass(org.apache.mahout.clustering.conversion.meanshift.InputMapper.class); job.setReducerClass(Reducer.class); job.setNumReduceTasks(0); job.setJarByClass(InputDriver.class); FileInputFormat.setInputPaths(job, input); FileOutputFormat.setOutputPath(job, output);org.apache.mahout.clustering.conversion.meanshift.InputMapper:
protected void map(LongWritable key, Text values, Context context) throws IOException, InterruptedException { String[] numbers = SPACE.split(values.toString()); // sometimes there are multiple separator spaces Collection<Double> doubles = Lists.newArrayList(); for (String value : numbers) { if (!value.isEmpty()) { doubles.add(Double.valueOf(value)); } } // ignore empty lines in input data if (!doubles.isEmpty()) { Vector point = new DenseVector(doubles.size()); int index = 0; for (Double d : doubles) { point.set(index++, d); } cw.setValue(new MeanShiftCanopy(point, nextCanopyId++, new EuclideanDistanceMeasure())); context.write(new Text(), cw); } }第一个job相对比较好理解,和前面的数据转换差不多,都是把文本转换为序列文件同时设定key和value的类型。
分享,快乐,成长
转载请注明出处:http://blog.csdn.net/fansy1990
- Mahout源码MeanShiftCanopy聚类算法分析(1)
- Mahout源码MeanShiftCanopy聚类算法分析(3-1)
- Mahout源码MeanShiftCanopy聚类算法分析(2)
- Mahout源码MeanShiftCanopy聚类算法分析(3)
- Mahout聚类算法canopy源码分析(1)
- Mahout源码K均值聚类算法分析(1)
- Mahout决策树算法源码分析(1)
- Mahout贝叶斯算法源码分析(1)
- Mahout源码canopy聚类算法分析(2)
- Mahout源码canopy聚类算法分析(3)
- Mahout源码K均值聚类算法分析(3)
- Mahout源码K均值聚类算法分析(4)
- Mahout源码K均值聚类算法分析(5)
- mahout源码K均值聚类算法分析(7)
- Mahout决策树算法源码分析(3-1)建树实战
- Mahout关联规则算法源码分析(1)
- Mahout贝叶斯算法源码分析(2-1)
- Mahout随机森林算法源码分析(1)--Describe
- bash中使两条指令同时运行
- 优先队列priority_queue(重载)
- HDU 3496 Watch The Movie( 二维费用背包)
- 计算机视觉、机器学习相关领域论文和源代码大集合
- 一个基于 EasyUI 的前台架构(2)主体内容区结构设计
- Mahout源码MeanShiftCanopy聚类算法分析(1)
- Android 动态加载布局
- DP 背包问题 01背包
- 一个基于 EasyUI 的前台架构(1)页面布局
- 树莓派配置无线网络(补充) 【for_wind】
- 一个基于 EasyUI 的前台架构(3)封装操作Tabs的JS代码
- 一个基于 EasyUI 的前台架构(4)主体页面框架收工
- Android实战技巧:如何在ScrollView中嵌套ListView
- 每天一个OpenGL程序 第二篇 点的绘制