---
title: "Mahout系列----MinHash聚类"
author: "thd52java"
date: "2013-12-05T16:01:37Z"
tags: []
categories: ["互联网"]
slug: "mahout-minhash"
draft: false
lastmod: "2013-12-05T16:01:37Z"
id: 1985729
url: "https://thd52java.iteye.com/blog/1985729"
---

Map:

 

Vector featureVector = features.get();

    if (featureVector.size() &lt; minVectorSize) {  
      return;  
    }  
    // Initialize the MinHash values to highest  
    for (int i = 0; i &lt; numHashFunctions; i++) {  
      minHashValues\[i] = Integer.MAX\_VALUE;  
    }

    for (int i = 0; i &lt; numHashFunctions; i++) {  
      for (Vector.Element ele : featureVector.nonZeroes()) {  
        int value = hashValue ? (int) ele.get() : ele.index();  
        bytesToHash\[0] = (byte) (value &gt;&gt; 24);  
        bytesToHash\[1] = (byte) (value &gt;&gt; 16);  
        bytesToHash\[2] = (byte) (value &gt;&gt; 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] &gt; hashIndex) {  
          minHashValues\[i] = hashIndex;  
        }  
      }  
    }  
    // output the cluster information  
    for (int i = 0; i &lt; numHashFunctions; i++) {  
      StringBuilder clusterIdBuilder = new StringBuilder();  
      for (int j = 0; j &lt; 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&lt;Writable&gt; points, Context context)  
    throws IOException, InterruptedException {  
    Collection&lt;Writable&gt; 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() &gt;= minClusterSize) {  
      context.getCounter(Clusters.ACCEPTED).increment(1);  
      for (Writable point : pointList) {  
        context.write(cluster, point);  
      }  
    } else {  
      context.getCounter(Clusters.DISCARDED).increment(1);  
    }  
  }