mahout算法源码分析之Itembased Collaborative Filtering(三)RowSimilarityJob验证
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验证的更多相关文章
- mahout算法源码分析之Itembased Collaborative Filtering(二)RowSimilarityJob
Mahout版本:0.7,hadoop版本:1.0.4,jdk:1.7.0_25 64bit. 本篇开始之前先来验证前篇blog的分析结果,编写下面的测试文件来进行对上篇三个job的输出进行读取: p ...
- mahout算法源码分析之Itembased Collaborative Filtering(四)共生矩阵乘法
Mahout版本:0.7,hadoop版本:1.0.4,jdk:1.7.0_25 64bit. 经过了SimilarityJob的计算共生矩阵后,就可以开始下面一个过程了,这个过程主要是共生矩阵的乘法 ...
- 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 这个算 ...
- mahout算法源码分析之Collaborative Filtering with ALS-WR (四)评价和推荐
Mahout版本:0.7,hadoop版本:1.0.4,jdk:1.7.0_25 64bit. 首先来总结一下 mahout算法源码分析之Collaborative Filtering with AL ...
- mahout算法源码分析之Collaborative Filtering with ALS-WR拓展篇
Mahout版本:0.7,hadoop版本:1.0.4,jdk:1.7.0_25 64bit. 额,好吧,心头的一块石头总算是放下了.关于Collaborative Filtering with AL ...
- diff.js 列表对比算法 源码分析
diff.js列表对比算法 源码分析 npm上的代码可以查看 (https://www.npmjs.com/package/list-diff2) 源码如下: /** * * @param {Arra ...
- JUC源码分析-线程池篇(三)ScheduledThreadPoolExecutor
JUC源码分析-线程池篇(三)ScheduledThreadPoolExecutor ScheduledThreadPoolExecutor 继承自 ThreadPoolExecutor.它主要用来在 ...
- JUC源码分析-线程池篇(三)Timer
JUC源码分析-线程池篇(三)Timer Timer 是 java.util 包提供的一个定时任务调度器,在主线程之外起一个单独的线程执行指定的计划任务,可以指定执行一次或者反复执行多次. 1. Ti ...
- 【Zookeeper】源码分析之Watcher机制(三)之Zookeeper
一.前言 前面已经分析了Watcher机制中的大多数类,本篇对于ZKWatchManager的外部类Zookeeper进行分析. 二.Zookeeper源码分析 2.1 类的内部类 Zookeeper ...
随机推荐
- 在stm32上移植wpa_supplicant(一)
wifi芯片为88w8686,已经写好了驱动,用的是SPI方式,接下来准备移植wpa_supplicant.参考的资料为一篇论文----<基于微控制器的WPA技术研究与应用>. wpa_s ...
- [Everyday Mathematics]20150206
$$\bex \sen{fg}_{L^1}\leq C\sen{f}_{L^{r,\al}}\sen{g}_{L^{r',\al'}}, \eex$$ 其中 $$\bex f\in L^{r,\al} ...
- C# Code for Downloading Stock Symbols z
http://www.jarloo.com/download-stock-symbols/ If your using C# you can easily get the XML data using ...
- Delphi 利用TComm组件 Spcomm 实现串行通信
Delphi 利用TComm组件 Spcomm 实现串行通信 摘要:利用Delphi开发工业控制系统软件成为越来越多的开发人员的选择,而串口通信是这个过程中必须解决的问题之一.本文在对几种常用串口通信 ...
- linux 命令——文件管理 ls
一.介绍 ls命令是linux下最常用的命令之一,ls跟dos下的dir命令是一样的都是用来列出目录下的文件和子目录.ls全称list,即列表. 缺省下ls用来打印出当前目录的清单,如果ls指定其他目 ...
- Selenium2Library系列 keywords 之 _SelectElementKeywords 之 get_selected_list_labels(self, locator)
def get_selected_list_labels(self, locator): """Returns the visible labels of selecte ...
- 机器学习——Logistic回归
参考<机器学习实战> 利用Logistic回归进行分类的主要思想: 根据现有数据对分类边界线建立回归公式,以此进行分类. 分类借助的Sigmoid函数: Sigmoid函数图: Sigmo ...
- 在EC2上安装MEAN环境
本文在个人博客上的地址为URL,欢迎品尝. 搭建决策树项目外网DEMO尝试几个地方后,最后选择了EC2(Amazon Elastic Compute Cloud).选择的是最经济便宜的Amazon L ...
- 转:高性能Mysql主从架构的复制原理及配置详解
温习<高性能MySQL>的复制篇. 1 复制概述 Mysql内建的复制功能是构建大型,高性能应用程序的基础.将Mysql的数据分布到多个系统上去,这种分布的机制,是通过将Mysql的某一台 ...
- mongodb基础系列——数据库查询数据返回前台JSP(二)
上篇博客论述了,数据库查询数据返回前台JSP.博客中主要使用Ajax调用来显示JSON串,来获取其中某一个字段,赋给界面中的某一个控件. 那这篇博客中,我们讲解,把后台List传递JSP展示. Lis ...