Map:

Vector featureVector = features.get();

if (featureVector.size() < minVectorSize) {

      return;

    }

    // Initialize the MinHash values to highest

    for (int i = 0; i < numHashFunctions; i++) {

      minHashValues[i] = Integer.MAX_VALUE;

    }



    for (int i = 0; i < numHashFunctions; i++) {

      for (Vector.Element ele : featureVector.nonZeroes()) {

        int value = hashValue ? (int) ele.get() : ele.index();

        bytesToHash[0] = (byte) (value >> 24);

        bytesToHash[1] = (byte) (value >> 16);

        bytesToHash[2] = (byte) (value >> 8);

        bytesToHash[3] = (byte) value;

        int hashIndex = hashFunction[i].hash(bytesToHash);

        //if our new hash value is less than the old one, replace the old one

        if (minHashValues[i] > hashIndex) {

          minHashValues[i] = hashIndex;

        }

      }

    }

    // output the cluster information

    for (int i = 0; i < numHashFunctions; i++) {

      StringBuilder clusterIdBuilder = new StringBuilder();

      for (int j = 0; j < keyGroups; j++) {

        clusterIdBuilder.append(minHashValues[(i + j) % numHashFunctions]).append('-');

      }

      //remove the last dash

      clusterIdBuilder.deleteCharAt(clusterIdBuilder.length() - 1);



      cluster.set(clusterIdBuilder.toString());



      if (debugOutput) {

        vector.set(featureVector);

        context.write(cluster, vector);

      } else {

        context.write(cluster, item);

      }

    }


 protected void reduce(Text cluster, Iterable<Writable> points, Context context)

    throws IOException, InterruptedException {

    Collection<Writable> pointList = Lists.newArrayList();

    for (Writable point : points) {

      if (debugOutput) {

        Vector pointVector = ((VectorWritable) point).get().clone();

        Writable writablePointVector = new VectorWritable(pointVector);

        pointList.add(writablePointVector);

      } else {

        Writable pointText = new Text(point.toString());

        pointList.add(pointText);

      }

    }

    if (pointList.size() >= minClusterSize) {

      context.getCounter(Clusters.ACCEPTED).increment(1);

      for (Writable point : pointList) {

        context.write(cluster, point);

      }

    } else {

      context.getCounter(Clusters.DISCARDED).increment(1);

    }

  }

 


mahout系列----minhash聚类的更多相关文章

  1. Mahout系列之----kmeans 聚类

    Kmeans是最经典的聚类算法之一,它的优美简单.快速高效被广泛使用. Kmeans算法描述 输入:簇的数目k:包含n个对象的数据集D. 输出:k个簇的集合. 方法: 从D中任意选择k个对象作为初始簇 ...

  2. mahout系列之---谱聚类

    1.构造亲和矩阵W 2.构造度矩阵D 3.拉普拉斯矩阵L 4.计算L矩阵的第二小特征值(谱)对应的特征向量Fiedler 向量 5.以Fiedler向量作为kmean聚类的初始中心,用kmeans聚类 ...

  3. Mahout 系列之--canopy 算法

    Canopy 算法,流程简单,容易实现,一下是算法 (1)设样本集合为S,确定两个阈值t1和t2,且t1>t2. (2)任取一个样本点p属于S,作为一个Canopy,记为C,从S中移除p. (3 ...

  4. ML.NET技术研究系列-2聚类算法KMeans

    上一篇博文我们介绍了ML.NET 的入门: ML.NET技术研究系列1-入门篇 本文我们继续,研究分享一下聚类算法k-means. 一.k-means算法简介 k-means算法是一种聚类算法,所谓聚 ...

  5. Mahout系列之-----相似度

    Mahout推荐系统中有许多相似度实现,这些组件实现了计算不能User之间或Item之间的相似度.对于数据量以及数据类型不同的数据源,需要不同的相似度计算方法来提高推荐性能,在mahout提供了大量用 ...

  6. Mahout系列之----距离度量

       x = (x1,...,xn) 和y = (y1,...,yn) 之间的距离为 (1)欧氏距离   EuclideanDistanceMeasure (2)曼哈顿距离  ManhattanDis ...

  7. Mahout 系列之----共轭梯度

    无预处理共轭梯度 要求解线性方程组 ,稳定双共轭梯度法从初始解 开始按以下步骤迭代: 任意选择向量 使得 ,例如, 对 若 足够精确则退出 预处理共轭梯度 预处理通常被用来加速迭代方法的收敛.要使用预 ...

  8. Mahout系列之----共轭梯度预处理

    对于大型矩阵,预处理是很重要的.常用的预处理方法有: (1) 雅克比预处理 (2)块状雅克比预处理 (3)半LU 分解 (4)超松弛法

  9. mahout系列----Dirichlet 分布

    Dirichlet分布可以看做是分布之上的分布.如何理解这句话,我们可以先举个例子:假设我们有一个骰子,其有六面,分别为{1,2,3,4,5,6}.现在我们做了10000次投掷的实验,得到的实验结果是 ...

随机推荐

  1. JBOSS EAP 6 系列五 Managed domains 管理域最主要的功能是“统一部署,统一配置”

    摘要 本文首先介绍Managed Domain的概念,管理域最主要的功能是"统一部署,统一配置".接下来通过一个实例在"统一配置"部分实现一个双机配置起来的域, ...

  2. FFmpeg源代码简单分析:av_write_trailer()

    ===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...

  3. Android初级教程初谈自定义view自定义属性

    有些时候,自己要在布局文件中重复书写大量的代码来定义一个布局.这是最基本的使用,当然要掌握:但是有些场景都去对应的布局里面写对应的属性,就显得很无力.会发现,系统自带的控件无法满足我们的要求,这个时候 ...

  4. 深入理解Express.js

    转自:http://xvfeng.me/posts/understanding-expressjs/ 英文原文更赞:http://evanhahn.com/understanding-express- ...

  5. Get and Post(Unity3D开发之六)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢! 原文地址: http://www.cocos2dev.com/?p=565 unity3d中的www直 ...

  6. React Native入门教程2 -- 基本组件使用及样式

    在上一篇文章中,我们学会了如何搭建React Native的环境(React Native入门教程(笔记) 1 – 开发环境搭建),不知道你们是否搭建好了呢,如果还没有,那么快动起小手,来体验RN带给 ...

  7. Mybatis逻辑分页原理解析RowBounds

    Mybatis提供了一个简单的逻辑分页使用类RowBounds(物理分页当然就是我们在sql语句中指定limit和offset值),在DefaultSqlSession提供的某些查询接口中我们可以看到 ...

  8. Linux IPC实践(1) -- 概述

    进程的同步与互斥 进程同步: 多个进程需要相互配合共同完成一项任务. 进程互斥: 由于各进程要求共享资源,而且有些资源需要互斥使用,因此各进程间竞争使用这些资源,进程的这种关系为进程的互斥;系统中某些 ...

  9. Chipmunk僵尸物理对象的出现和解决(三)

    首先是触摸移动反弹棒的代码: -(void)touchMoved:(CCTouch *)touch withEvent:(CCTouchEvent *)event{ CGPoint location ...

  10. Swift基础之PickerView(时间)选择器

    代码讲解:(后面有额外代码讲解) 首页设计UIPickerView的样式设计: leftArray = ["花朵","颜色","形状"]; ...