手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Hadoop k-means 算法实现

[日期:2012-10-01] 来源:Linux社区  作者:fansy1990 [字体:大 中 小]

上面就是全部的代码了,下面贴出结果:

  1. 0   0.0 0.2 0.4 
  2. 1   0.3 0.2 0.4 
  3. 1   0.4 0.2 0.4 
  4. 1   0.5 0.2 0.4 
  5. 2   5.0 5.2 5.4 
  6. 2   6.0 5.2 6.4 
  7. 2   4.0 5.2 4.4 
  8. 2   10.3    10.4    10.5 
  9. 2   10.3    10.4    10.5 
  10. 2   10.3    10.4    10.5 

由最终的结果可以看出分类的结果不是很好,所以说初始的数据中心点一定要选好才行。

下面说下我遇到的问题,刚开始的时候我获取数据维度的方法不是使用读入文件然后再取得相应的信息,而是按照下面的方法:

WrongCombine:

  1. package org.fansy.date927; 
  2.  
  3. import java.io.IOException; 
  4. import java.util.Iterator; 
  5.  
  6. import org.apache.Hadoop.io.IntWritable; 
  7. import org.apache.hadoop.io.Text; 
  8. import org.apache.hadoop.mapreduce.Reducer; 
  9.  
  10. public class KmeansC extends Reducer<IntWritable,DataPro,IntWritable,DataPro> { 
  11.      
  12.     public void reduce(IntWritable key,Iterable<DataPro> values,Context context)throws InterruptedException, IOException{ 
  13.         // get dimension first  
  14.         Iterator<DataPro> iter=values.iterator(); 
  15.         int dimension=0; 
  16.         for(DataPro val:values){ 
  17.             String[] datastr=val.getCenter().toString().split("\t"); 
  18.             dimension=datastr.length; 
  19.             break; 
  20.         } 
  21.          
  22.         double[] sum=new double[dimension]; 
  23.         int sumCount=0; 
  24.         //  operation one  
  25.         while(iter.hasNext()){ 
  26.             DataPro val=iter.next(); 
  27.             String[] datastr=val.getCenter().toString().split("\t"); 
  28.             sumCount+=val.getCount().get(); 
  29.             for(int i=0;i<dimension;i++){ 
  30.                 sum[i]+=Double.parseDouble(datastr[i]); 
  31.             } 
  32.         } 
  33.          
  34.         // operation two  
  35.         /*for(DataPro val:values){ 
  36.             String[] datastr=val.getCenter().toString().split("\t"); 
  37.             sumCount+=val.getCount().get(); 
  38.             for(int i=0;i<dimension;i++){ 
  39.                 sum[i]+=Double.parseDouble(datastr[i]); 
  40.             } 
  41.         }*/ 
  42.         //  calculate the new centers  
  43. //      double[] newcenter=new double[dimension];  
  44.         StringBuffer sb=new StringBuffer(); 
  45.         for(int i=0;i<dimension;i++){ 
  46.             sb.append(sum[i]+"\t"); 
  47.         } 
  48.         System.out.println("combine text:"+sb.toString()); 
  49.         System.out.println("combine sumCount:"+sumCount); 
  50.         DataPro newvalue=new DataPro(); 
  51.         newvalue.set(new Text(sb.toString()), new IntWritable(sumCount)); 
  52.         context.write(key, newvalue); 
  53.     } 
  54. } 

从第16到20行是我获得数据维度的方法,但是虽然维度获得了,但是,后面的操作就出问题了,从我加入的调试提示信息可以看出是出了什么问题了:

  1. 12/09/28 14:40:40 INFO mapred.Task:  Using ResourceCalculatorPlugin : org.apache.hadoop.util.LinuxResourceCalculatorPlugin@435e331b 
  2. 12/09/28 14:40:40 INFO mapred.MapTask: io.sort.mb = 100 
  3. 12/09/28 14:40:40 INFO mapred.MapTask: data buffer = 79691776/99614720 
  4. 12/09/28 14:40:40 INFO mapred.MapTask: record buffer = 262144/327680 
  5. 0.0,0.0,0.0, 
  6. 5.0,5.0,5.0, 
  7. 10.0,10.0,10.0, 
  8. the map out:0,0.0   0.2 0.4 
  9. the map out:0,0.3   0.2 0.4 
  10. the map out:0,0.4   0.2 0.4 
  11. the map out:0,0.5   0.2 0.4 
  12. the map out:1,5.0   5.2 5.4 
  13. the map out:1,6.0   5.2 6.4 
  14. the map out:1,4.0   5.2 4.4 
  15. the map out:2,10.3  10.4    10.5 
  16. the map out:2,10.3  10.4    10.5 
  17. the map out:2,10.3  10.4    10.5 
  18. 12/09/28 14:40:40 INFO mapred.MapTask: Starting flush of map output 
  19. combine text:1.2    0.6000000000000001  1.2000000000000002   
  20. combine sumCount:3 
  21. combine text:10.0   10.4    10.8     
  22. combine sumCount:2 
  23. combine text:20.6   20.8    21.0     
  24. combine sumCount:2 
  25. 12/09/28 14:40:40 INFO mapred.MapTask: Finished spill 0 
  26. 12/09/28 14:40:40 INFO mapred.Task: Task:attempt_local_0001_m_000000_0 is done. And is in the process of commiting 

有上面的信息可以看出首先map操作没有问题,但是combine操作时计算相同的index的个数不对,每个都少了1个,而且计算的总和也不对,所以我这样猜想,是不是通过这样的操作:

  1. for(DataPro val:values){ 
  2.             String[] datastr=val.getCenter().toString().split("\t"); 
  3.             dimension=datastr.length; 
  4.             break; 
  5.         } 

可能改变了values的指针或者其他的什么东西之类的,导致后面我再使用这样的操作读取的时候就会从第二个开始读了。这个就不知到了。

linux
相关资讯       HDFS 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款