代码共分为四部分:

<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. linux-网络监控命令-netstat初级

    简介 Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Member ...

  2. linux-系统资源查看-动态

    1.top 2.sar http://blog.csdn.net/hguisu/article/details/7493661  很重要 http://blog.itpub.net/24435147/ ...

  3. ubuntu配置无密码登录

    1 本地生成ssh公钥和私钥, 2将公钥拷贝到ubuntu上的.ssh/authorized_keys 中

  4. c++11 std::prev、std::next、std::advance与auto 使用

    auto 定义变量时放在变量前,无需知道具体变量类型,系统可自行推断类型,减少编程工作,特别是在模板使用时,使用更方便. 下面简单例子: auto a=; auto b='a'; auto s=&qu ...

  5. oracle数据库中函数和存储过程中的区别

    一.函数必须有返回值,过程没有返回值: 二.函数可以单独执行,过程必须通过execute执行: 三.函数可以嵌入SQL中执行,过程不能. 可以将比较复杂的查询写成函数,然后在过程中调用.

  6. perl学习笔记二

    数组: 特殊的数组索引:加入对索引值超过数组尾端的元素进行赋值,数组将会根据需要自动扩大,只要有可用的内存分配给数组.意外增加的数组元素的值为undef. $a[0]="aa"; ...

  7. Java程序猿的JavaScript学习笔记(12——jQuery-扩展选择器)

    计划按例如以下顺序完毕这篇笔记: Java程序猿的JavaScript学习笔记(1--理念) Java程序猿的JavaScript学习笔记(2--属性复制和继承) Java程序猿的JavaScript ...

  8. odoo12新特性: 会计改进

    改进分析会计 分析会计层级结构 分析分配 分析分录增加了表格视图     ============== SPECIFICATIONS ============== a. Hierarchy  - Cr ...

  9. sed `grep` 查找并替换

    sed "s/libletvwatermark/libletv_watermark/" `grep -rl libletvwatermark` grep [options] 3.主 ...

  10. Oracle 导入导出数据库

    imp userid=yrsuser/yrsuser2587 fromuser=yrsuser touser=yrsuser file=E:\yrs.dmp exp userid=yrsuser/yr ...