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. Afinal加载网络图片及下载文件使用方法

    Afinal快速开发框架使用起来非常方便,下面将讲解如何利用Afinal加载网络图片及下载文件: 先看效果图: 注意:使用Afinal前需添加Afinal的jar,可以在这里下载:http://dow ...

  2. [ExtJS5学习笔记]第七节 Extjs5的组件components及其模板事件方法学习

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/38487519 本文作者:sushengmiyan ------------------ ...

  3. linux下字节对齐

    一,内存地址对齐的概念    计算机内存中排列.访问数据的一种方式,包含基本数据对齐和结构体数据对齐.    32位系统中,数据总线宽度为32,每次能够读取4字节数据.地址总线为32,最大寻址空间为4 ...

  4. java操作XML文件--读取内容

          先把问题贴出来:编写一个可以解析xml及修改xml内容的工具类       由于我以前做过Android应用程序开发,之前也解析过xml文件,所以,这道题不是很难,这篇文章我先解决第一个问 ...

  5. 【一天一道LeetCode】#349. Intersection of Two Arrays

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

  6. Win7 Eclipse Hadoop2.4插件配置

    准备工作: 1.下载hadoop2x-eclipse-plugin-master.zip Github地址:https://github.com/winghc/hadoop2x-eclipse-plu ...

  7. Dynamics CRM 导入用户数据错误 could not retrieve salesperson role

    在CRM中通过导入数据的方式创建用户时报下图中的错误,"could not retrieve saleperson role".原因是系统中的自带的salesperson安全角色被 ...

  8. Weblogic10 集群配置

     1.预备知识 什么是Domain和Server Domain Domain是WebLogic Server实例的基本管理单元.所谓Domain就是,由配置为Administrator Serve ...

  9. Windows下配置nginx+FastCgi + Spawn-fcgi

    前提: 下载nginx, FastCgi, Spawn-fcgi Spawn-fcgi有个Windows的版本,但不能在VS中编译,这里有一个编译好的版本:http://download.csdn.n ...

  10. UILabel-UITextField-UIBotton&nbsp;UI_…

    注意:AppDelegate是类,所以self在这个类中指的就是AppDelegate对象 - (BOOL)application:(UIApplication *)application didFi ...