代码共分为四部分:

<strong><span style="font-size:18px;">/***
* @author YangXin
* @info 封装共现关系列
*/
package unitSix;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.mahout.cf.taste.hadoop.item.VectorOrPrefWritable;
import org.apache.mahout.math.VectorWritable; public class CooccurrenceColumnWrapperMapper extends Mapper<IntWritable, VectorWritable, IntWritable, VectorOrPrefWritable>{
public void map(IntWritable key, VectorWritable value, Context context) throws IOException, InterruptedException{
context.write(key, new VectorOrPrefWritable(value.get()));
}
}
</span></strong>



<strong><span style="font-size:18px;">/***
* @author YangXin
* @info 切割用户数量
*/
package unitSix; import java.io.IOException;
import java.util.Iterator;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.mahout.cf.taste.hadoop.item.VectorOrPrefWritable;
import org.apache.mahout.math.VarLongWritable;
import org.apache.mahout.math.Vector;
import org.apache.mahout.math.VectorWritable; public class UserVectorSplitterMapper extends Mapper<VarLongWritable, VectorWritable, IntWritable, VectorOrPrefWritable>{
public void map(VarLongWritable key, VectorWritable value, Context context) throws IOException, InterruptedException{
long userID = key.get();
Vector userVector = value.get();
Iterator<Vector.Element> it = userVector.nonZeroes().iterator();
IntWritable itemIndexWritable = new IntWritable();
while(it.hasNext()){
Vector.Element e = it.next();
int itemIndex = e.index();
float preferenceValue = (float)e.get();
itemIndexWritable.set(itemIndex);
context.write(itemIndexWritable, new VectorOrPrefWritable(userID, preferenceValue));
}
}
}</span></strong>

<strong><span style="font-size:18px;">/***
* @author YangXin
* @info 计算部分推荐向量
*/
package unitSix;
import java.io.IOException;
import java.util.List;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.mahout.cf.taste.hadoop.item.VectorAndPrefsWritable;
import org.apache.mahout.math.VarLongWritable;
import org.apache.mahout.math.Vector;
import org.apache.mahout.math.VectorWritable; public class PartialMultiplyMapper extends Mapper<IntWritable, VectorAndPrefsWritable, VarLongWritable, VectorWritable>{
public void map(IntWritable key, VectorAndPrefsWritable vectorAndPrefsWritable, Context context) throws IOException, InterruptedException{
Vector cooccurrenceColumn = vectorAndPrefsWritable.getVector();
List<Long> userIDs = vectorAndPrefsWritable.getUserIDs();
List<Float> prefValues = vectorAndPrefsWritable.getValues();
for(int i = 0; i < userIDs.size(); i++){
long userID = userIDs.get(i);
float prefValue = prefValues.get(i);
Vector partialProduct = cooccurrenceColumn.times(prefValue);
context.write(new VarLongWritable(userID), new VectorWritable(partialProduct));;
}
}
}
</span></strong>

<strong><span style="font-size:18px;">/***
* @author YangXin
* @info 实现部分成绩的combiner
*/
package unitSix; import java.io.IOException; import org.apache.hadoop.mapreduce.Reducer;
import org.apache.mahout.math.VarLongWritable;
import org.apache.mahout.math.Vector;
import org.apache.mahout.math.VectorWritable; public class AggregateCombiner extends Reducer<VarLongWritable, VectorWritable, VarLongWritable, VectorWritable>{
public void reduce(VarLongWritable key, Iterable<VectorWritable> values, Context context) throws IOException, InterruptedException{
Vector partial = null;
for(VectorWritable vectorWritable : values){
partial = partial == null ? vectorWritable.get() : partial.plus(vectorWritable.get());
}
context.write(key, new VectorWritable(partial));
}
}
</span></strong>







向MapReduce转换:通过部分成绩计算矩阵乘法的更多相关文章

  1. 使用shared memory 计算矩阵乘法 (其实并没有加速多少)

    #include "cuda_runtime.h" #include "device_launch_parameters.h" #include "d ...

  2. OpenCL NativeKernel 计算矩阵乘法

    ▶ 使用函数 clEnqueueNativeKernel 来调用 C/C++ 本地函数(在 OpenCL 中将其看做回调函数),使用本地编译器(而不是 OpenCL 编译器)来编译和执行内核 ● 代码 ...

  3. 有关CUBLAS中的矩阵乘法函数

    关于cuBLAS库中矩阵乘法相关的函数及其输入输出进行详细讨论. ▶ 涨姿势: ● cuBLAS中能用于运算矩阵乘法的函数有4个,分别是 cublasSgemm(单精度实数).cublasDgemm( ...

  4. 蓝桥杯 BASIC_17 矩阵乘法 (矩阵快速幂)

    问题描述 给定一个N阶矩阵A,输出A的M次幂(M是非负整数) 例如: A = 1 2 3 4 A的2次幂 7 10 15 22 输入格式 第一行是一个正整数N.M(1<=N<=30, 0& ...

  5. MapReduce实现矩阵乘法

    简单回想一下矩阵乘法: 矩阵乘法要求左矩阵的列数与右矩阵的行数相等.m×n的矩阵A,与n×p的矩阵B相乘,结果为m×p的矩阵C.具体内容能够查看:矩阵乘法. 为了方便描写叙述,先进行如果: 矩阵A的行 ...

  6. POJ C程序设计进阶 编程题#1:计算矩阵边缘之和

    编程题#1:计算矩阵边缘元素之和 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB ...

  7. 机器学习进阶-直方图与傅里叶变换-傅里叶变换(高低通滤波) 1.cv2.dft(进行傅里叶变化) 2.np.fft.fftshift(将低频移动到图像的中心) 3.cv2.magnitude(计算矩阵的加和平方根) 4.np.fft.ifftshift(将低频和高频移动到原来位置) 5.cv2.idft(傅里叶逆变换)

    1. cv2.dft(img, cv2.DFT_COMPLEX_OUTPUT) 进行傅里叶变化 参数说明: img表示输入的图片, cv2.DFT_COMPLEX_OUTPUT表示进行傅里叶变化的方法 ...

  8. MapReduce实现大矩阵乘法

    来自:http://blog.csdn.net/xyilu/article/details/9066973 引言 何 为大矩阵?Excel.SPSS,甚至SAS处理不了或者处理起来非常困难,需要设计巧 ...

  9. 【原创】开源Math.NET基础数学类库使用(15)C#计算矩阵行列式

                   本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新  开源Math.NET基础数学类库使用总目录:[目录]开源Math.NET基础数学类库使用总目录 上个月 ...

随机推荐

  1. 链接服务器 "(null)" 的 OLE DB 访问接口 "SQLNCLI11" 指示该对象没有列,或当前用户没有访问该对象的权限。

    原文:链接服务器 "(null)" 的 OLE DB 访问接口 "SQLNCLI11" 指示该对象没有列,或当前用户没有访问该对象的权限. SELECT * F ...

  2. util.date.js

    ylbtech-JavaScript-util: util.date.js 日期处理工具 1.A,JS-效果图返回顶部   1.B,JS-Source Code(源代码)返回顶部 1.B.1, m.y ...

  3. gamma校正原理

    http://blog.csdn.net/u013286409/article/details/50239377 目录(?)[-]   图2中左图为原图,中图为gamma = 1/2.2在校正结果,原 ...

  4. 2017.4.10 spring-ldap官方文档学习

    官网:http://www.springframework.org/ldap 官方文档及例子(重要):http://docs.spring.io/spring-ldap/docs/2.1.0.RELE ...

  5. Redis缓存清理

    Redis缓存清理 学习了:https://www.cnblogs.com/ZnCl/p/7116870.html 使用 redis-cli.exe登录, 使用flushall 命令: 或者key * ...

  6. JAVA Eclipse如何修改Android程序名称

    Values中修改strings.xml中的app_name即可   注意他是连接到AndroidManifest.xml文件的  

  7. Linux非阻塞IO(二)网络编程中非阻塞IO与IO复用模型结合

    上文描述了最简易的非阻塞IO,采用的是轮询的方式,这节我们使用IO复用模型.   阻塞IO   过去我们使用IO复用与阻塞IO结合的时候,IO复用模型起到的作用是并发监听多个fd. 以简单的回射服务器 ...

  8. react-navigation + react-native-vector-icons

    1.安装 yarn add react-navigation react-native-vector-icons 2.创建 root.js import React, {Component} from ...

  9. iOS使用自己定义字体

    1.加入相应的字体(.ttf或.odf)到project的resurce,比如my.ttf. 2.在info.plist中加入一项 Fonts provided by application (ite ...

  10. Shell脚本之:函数

    Shell 也支持函数.Shell函数必须先定义后使用. 函数的定义与调用 Shell 函数的定义格式如下: function_name () { list of commands [ return ...