Mahout版本:0.7,hadoop版本:1.0.4,jdk:1.7.0_25 64bit。

本篇分析上篇的分析是否正确,主要是编写上篇输出文件的读取以及添加log信息打印相关变量。

首先,编写下面的测试文件分析所有的输出:

package mahout.fansy.item;

import java.io.IOException;
import java.util.Map; import mahout.fansy.utils.read.ReadArbiKV; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Writable;
import org.apache.mahout.math.Vector;
import org.apache.mahout.math.hadoop.similarity.cooccurrence.Vectors; import junit.framework.TestCase; public class ReadRowSimilarityJobOut extends TestCase {
// 测试 weights 输出:
public void testWeights() throws IOException{
String path="hdfs://ubuntu:9000/user/mahout/item/temp/weights/part-r-00000";
Map<Writable,Writable> map= ReadArbiKV.readFromFile(path);
System.out.println("weights=================");
System.out.println(map);
}
//normsPath
public void testNormsPath() throws IOException{
String path="hdfs://ubuntu:9000/user/mahout/item/temp/norms.bin";
Vector map=getVector(path);
System.out.println("normsPath=================");
System.out.println(map);
}
//maxValues.bin
public void testMaxValues() throws IOException{
String path="hdfs://ubuntu:9000/user/mahout/item/temp/maxValues.bin";
Vector map=getVector(path);
System.out.println("maxValues=================");
System.out.println(map);
}
//numNonZeroEntries.bin
public void testNumNonZeroEntries() throws IOException{
String path="hdfs://ubuntu:9000/user/mahout/item/temp/numNonZeroEntries.bin";
Vector map=getVector(path);
System.out.println("numNonZeroEntries=================");
System.out.println(map);
} //pairwiseSimilarityPath
public void testPairwiseSimilarityPath() throws IOException{
String path="hdfs://ubuntu:9000/user/mahout/item/temp/pairwiseSimilarity/part-r-00000"; Map<Writable,Writable> map= ReadArbiKV.readFromFile(path);
System.out.println("pairwiseSimilarityPath=================");
System.out.println(map);
} //similarityMatrix
public void testSimilarityMatrix() throws IOException{
String path="hdfs://ubuntu:9000/user/mahout/item/temp/similarityMatrix/part-r-00000";
Map<Writable,Writable> map= ReadArbiKV.readFromFile(path);
System.out.println("similarityMatrix=================");
System.out.println(map);
} // 读取.bin文件
public Vector getVector(String path){
Configuration conf=new Configuration();
conf.set("mapred.job.tracker", "ubuntu:9001");
Vector vector=null;
try {
vector = Vectors.read(new Path(path), conf);
} catch (IOException e) {
e.printStackTrace();
}
return vector;
}
}

运行上面的文件得到下面的输出:

weights=================
{1={103:2.5,102:3.0,101:5.0}, 2={101:2.0,104:2.0,103:5.0,102:2.5}, 3={101:2.5,107:5.0,105:4.5,104:4.0}, 4={101:5.0,106:4.0,104:4.5,103:3.0}, 5={106:4.0,105:3.5,104:4.0,103:2.0,102:3.0,101:4.0}}
normsPath=================
{107:25.0,106:32.0,105:32.5,104:56.25,103:44.25,102:24.25,101:76.25}
maxValues=================
{}
numNonZeroEntries=================
{}
pairwiseSimilarityPath=================
{102={106:0.14972506706560876,105:0.14328432723886902,104:0.12789210656028413,103:0.1975496259559987}, 103={106:0.1424339656566283,105:0.11208890297777215,104:0.14037600977966974}, 101={107:0.10275248635596666,106:0.1424339656566283,105:0.1158457425543559,104:0.16015261286229274,103:0.15548737703860027,102:0.14201473202245876}, 106={}, 107={}, 104={107:0.13472338607037426,106:0.18181818181818182,105:0.16736577623297264}, 105={107:0.2204812092115424,106:0.14201473202245876}}
similarityMatrix=================
{102={101:0.14201473202245876,106:0.14972506706560876,105:0.14328432723886902,104:0.12789210656028413,103:0.1975496259559987}, 103={101:0.15548737703860027,106:0.1424339656566283,105:0.11208890297777215,104:0.14037600977966974,102:0.1975496259559987}, 101={107:0.10275248635596666,106:0.1424339656566283,105:0.1158457425543559,104:0.16015261286229274,103:0.15548737703860027,102:0.14201473202245876}, 106={101:0.1424339656566283,105:0.14201473202245876,104:0.18181818181818182,103:0.1424339656566283,102:0.14972506706560876}, 107={105:0.2204812092115424,104:0.13472338607037426,101:0.10275248635596666}, 104={107:0.13472338607037426,106:0.18181818181818182,105:0.16736577623297264,103:0.14037600977966974,102:0.12789210656028413,101:0.16015261286229274}, 105={107:0.2204812092115424,106:0.14201473202245876,104:0.16736577623297264,103:0.11208890297777215,102:0.14328432723886902,101:0.1158457425543559}}

其中第一个weights就和分析的一模一样,这里就不再相信写了。那就只分析pairwiseSimilarityPath和similarityMatrix了:

(1)pairwiseSimilarityPath:

前面关于这个的分析在最后reducer的时候是有错误的,应该说是没有分析完,如下图(此截图是使用log打印的变量信息):

可以看到上篇其实只是分析到了第二行(第二行和第三行一样)而已,而没有分析到最后的输出。其实也只是少分析了一个while循环而已:

while (dotsWith.hasNext()) {
Vector.Element b = dotsWith.next();
double similarityValue = similarity.similarity(b.get(), normA, norms.getQuick(b.index()), numberOfColumns);
if (similarityValue >= treshold) {
similarities.set(b.index(), similarityValue);
}
}

这里来分析一下根据第二行的值如何求得第四行的值,首先normA是norms中的102对应的值,即24.25,然后来看similarity函数:

public double similarity(double dots, double normA, double normB, int numberOfColumns) {
double euclideanDistance = Math.sqrt(normA - 2 * dots + normB);
return 1.0 / (1.0 + euclideanDistance);
}

项目106调用的参数应该是similarity(12.0,24.25,32.0,5),所以返回的值是1/(1+sqrt(24.25-2*12+32))=0.149725067,刚好和第四行的值对应;最后的输出没有102,是因为设置了similarities.setQuick(row.get(), 0);这样一句代码,把相对应的值设置为了0,也就是不输出。

(2)similarityMatrix

由(1)的分析可以知道,(2)的输入是这样的:

{102={106:0.14972506706560876,105:0.14328432723886902,104:0.12789210656028413,103:0.1975496259559987},
103={106:0.1424339656566283,105:0.11208890297777215,104:0.14037600977966974},
101={107:0.10275248635596666,106:0.1424339656566283,105:0.1158457425543559,104:0.16015261286229274,103:0.15548737703860027,102:0.14201473202245876},
106={},
107={},
104={107:0.13472338607037426,106:0.18181818181818182,105:0.16736577623297264},
105={107:0.2204812092115424,106:0.14201473202245876}}

关于这个job的mapper分析是正确的,但是combiner分析中的merge方法是不对的,可以看到merge的代码如下:

public static Vector merge(Iterable<VectorWritable> partialVectors) {
Iterator<VectorWritable> vectors = partialVectors.iterator();
Vector accumulator = vectors.next().get();
while (vectors.hasNext()) {
VectorWritable v = vectors.next();
if (v != null) {
Iterator<Vector.Element> nonZeroElements = v.get().iterateNonZero();
while (nonZeroElements.hasNext()) {
Vector.Element nonZeroElement = nonZeroElements.next();
accumulator.setQuick(nonZeroElement.index(), nonZeroElement.get());
}
}
}
return accumulator;
}

看到这个代码的作用是把相同的key中的value全部设置一下,查看log信息如下:

首先是map的输出(key在101~103):


(key在104~107):

combiner的输出:


这样看到数据的输出后,就可以很好的理解combiner的具体操作了;

最后看reducer的操作,就是把combiner的输出进行排序即可:

但是,看到上面的log信息,似乎不是这样的,关于那个Vectors.topKElements方法没有细看,应该是和猜测的不同操作吧,这个下次在看了。

分享,成长,快乐

转载请注明blog地址:http://blog.csdn.net/fansy1990

mahout算法源码分析之Itembased Collaborative Filtering(三)RowSimilarityJob验证的更多相关文章

  1. mahout算法源码分析之Itembased Collaborative Filtering(二)RowSimilarityJob

    Mahout版本:0.7,hadoop版本:1.0.4,jdk:1.7.0_25 64bit. 本篇开始之前先来验证前篇blog的分析结果,编写下面的测试文件来进行对上篇三个job的输出进行读取: p ...

  2. mahout算法源码分析之Itembased Collaborative Filtering(四)共生矩阵乘法

    Mahout版本:0.7,hadoop版本:1.0.4,jdk:1.7.0_25 64bit. 经过了SimilarityJob的计算共生矩阵后,就可以开始下面一个过程了,这个过程主要是共生矩阵的乘法 ...

  3. mahout算法源码分析之Collaborative Filtering with ALS-WR 并行思路

    Mahout版本:0.7,hadoop版本:1.0.4,jdk:1.7.0_25 64bit. mahout算法源码分析之Collaborative Filtering with ALS-WR 这个算 ...

  4. mahout算法源码分析之Collaborative Filtering with ALS-WR (四)评价和推荐

    Mahout版本:0.7,hadoop版本:1.0.4,jdk:1.7.0_25 64bit. 首先来总结一下 mahout算法源码分析之Collaborative Filtering with AL ...

  5. mahout算法源码分析之Collaborative Filtering with ALS-WR拓展篇

    Mahout版本:0.7,hadoop版本:1.0.4,jdk:1.7.0_25 64bit. 额,好吧,心头的一块石头总算是放下了.关于Collaborative Filtering with AL ...

  6. diff.js 列表对比算法 源码分析

    diff.js列表对比算法 源码分析 npm上的代码可以查看 (https://www.npmjs.com/package/list-diff2) 源码如下: /** * * @param {Arra ...

  7. JUC源码分析-线程池篇(三)ScheduledThreadPoolExecutor

    JUC源码分析-线程池篇(三)ScheduledThreadPoolExecutor ScheduledThreadPoolExecutor 继承自 ThreadPoolExecutor.它主要用来在 ...

  8. JUC源码分析-线程池篇(三)Timer

    JUC源码分析-线程池篇(三)Timer Timer 是 java.util 包提供的一个定时任务调度器,在主线程之外起一个单独的线程执行指定的计划任务,可以指定执行一次或者反复执行多次. 1. Ti ...

  9. 【Zookeeper】源码分析之Watcher机制(三)之Zookeeper

    一.前言 前面已经分析了Watcher机制中的大多数类,本篇对于ZKWatchManager的外部类Zookeeper进行分析. 二.Zookeeper源码分析 2.1 类的内部类 Zookeeper ...

随机推荐

  1. hdu 2594-Simpsons’ Hidden Talents(KMP)

    题意: 给你两个串a,b,求既是a的前缀又是b的后缀的最长子串的长度. 分析: 很自然的想到把两个串连接起来,根据KMP的性质求即可 #include <map> #include < ...

  2. C#几种截取字符串的方法小结

    1.根据单个分隔字符用split截取例如代码如下: string st="GT123_1"; string[] sArray=st.split("_"); 即可 ...

  3. Google Chart API 参考 中文版

    Google Chart API 参考 中文版 文档信息 翻译: Cloudream ,最后修改:02/22/2008 06:11:08 英文版版权归 Google , 转载此中文版必须以链接形式注明 ...

  4. mybatis系列-10-一对一查询

    10.1     需求 查询订单信息,关联查询创建订单的用户信息 10.2     resultType 10.2.1      sql语句 确定查询的主表:订单表 确定查询的关联表:用户表 关联查询 ...

  5. 我需要在电脑上安装C编译器

    这本书中我们使用了gcc(GNU编译器套装),它不但功能十分强大,而且还是免费的.你需要确保你的电脑上已经安装了gcc.如果你的操作系统是Linux,恭喜你,你已经拥有了gcc.

  6. TCPSocket v1.0 for cocos2d-x下载

    下载地址:http://files.cnblogs.com/elephant-x/TCPSocketLibs_V1.0.rar 这是自己封装的一个TCPSOCKET包,是独立于cocos2d-x的,使 ...

  7. 我从其他人的Shell脚本中学到的

    我从其他人的Shell脚本中学到的 2013/08/20 | 分类: 程序员 | 2 条评论 | 标签: SHELL, 脚本 分享到:17 本文由 伯乐在线 - 伯乐在线读者 翻译自 Fizer Kh ...

  8. ES6学习(1)——如何通过babel将ES6转化成ES5

    使用babel编译ES6 babel是一个工具,可以通过多个平台,让js文件从ES6转化成ES5,从而支持一些浏览器并未支持的语法. Insall babel $ sudo npm install b ...

  9. DelphiXE7操作sqlite数据库

    准备工作: 1.用SQLiteExpertPers建立一个sqlite数据库. 2.打开delphi xe7. 一.FireDAC法 设置库联接 1.放入FDConnection1控件 2.放入FDC ...

  10. JSF 2 checkboxes example

    In JSF, <h:selectBooleanCheckbox /> tag is used to render a single HTML input element of " ...