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); } }
查看本栏目更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索cluster
, context
, knn 聚类 分类
, value
, minhash
Writable
,以便于您获取更多的相关知识。
时间: 2024-10-01 16:08:57