You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by je...@apache.org on 2010/06/11 00:25:49 UTC
svn commit: r953483 - in /mahout/trunk/core/src:
main/java/org/apache/mahout/clustering/fuzzykmeans/
test/java/org/apache/mahout/clustering/fuzzykmeans/
Author: jeastman
Date: Thu Jun 10 22:25:49 2010
New Revision: 953483
URL: http://svn.apache.org/viewvc?rev=953483&view=rev
Log:
MAHOUT-167: Converted FuzzyKMeans to 0.20.2
Modified:
mahout/trunk/core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/FuzzyKMeansClusterMapper.java
mahout/trunk/core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/FuzzyKMeansClusterer.java
mahout/trunk/core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/FuzzyKMeansCombiner.java
mahout/trunk/core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/FuzzyKMeansDriver.java
mahout/trunk/core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/FuzzyKMeansMapper.java
mahout/trunk/core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/FuzzyKMeansReducer.java
mahout/trunk/core/src/test/java/org/apache/mahout/clustering/fuzzykmeans/TestFuzzyKmeansClustering.java
Modified: mahout/trunk/core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/FuzzyKMeansClusterMapper.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/FuzzyKMeansClusterMapper.java?rev=953483&r1=953482&r2=953483&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/FuzzyKMeansClusterMapper.java (original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/FuzzyKMeansClusterMapper.java Thu Jun 10 22:25:49 2010
@@ -21,54 +21,38 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
+import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.WritableComparable;
-import org.apache.hadoop.mapred.JobConf;
-import org.apache.hadoop.mapred.MapReduceBase;
-import org.apache.hadoop.mapred.Mapper;
-import org.apache.hadoop.mapred.OutputCollector;
-import org.apache.hadoop.mapred.Reporter;
+import org.apache.hadoop.mapreduce.Mapper;
import org.apache.mahout.clustering.WeightedVectorWritable;
import org.apache.mahout.math.VectorWritable;
-public class FuzzyKMeansClusterMapper extends MapReduceBase implements
- Mapper<WritableComparable<?>, VectorWritable, IntWritable, WeightedVectorWritable> {
+public class FuzzyKMeansClusterMapper extends Mapper<WritableComparable<?>, VectorWritable, IntWritable, WeightedVectorWritable> {
private final List<SoftCluster> clusters = new ArrayList<SoftCluster>();
private FuzzyKMeansClusterer clusterer;
- //private boolean emitMostLikely = false;
-
- //private double threshold = 0;
-
+ /* (non-Javadoc)
+ * @see org.apache.hadoop.mapreduce.Mapper#map(java.lang.Object, java.lang.Object, org.apache.hadoop.mapreduce.Mapper.Context)
+ */
@Override
- public void map(WritableComparable<?> key,
- VectorWritable point,
- OutputCollector<IntWritable, WeightedVectorWritable> output,
- Reporter reporter) throws IOException {
- clusterer.emitPointToClusters(point, clusters, output);
- }
+ protected void map(WritableComparable<?> key, VectorWritable point, Context context) throws IOException, InterruptedException {
+ clusterer.emitPointToClusters(point, clusters, context);
+ }
- /**
- * Configure the mapper by providing its clusters. Used by unit tests.
- *
- * @param clusters
- * a List<Cluster>
+ /* (non-Javadoc)
+ * @see org.apache.hadoop.mapreduce.Mapper#setup(org.apache.hadoop.mapreduce.Mapper.Context)
*/
- void config(List<SoftCluster> clusters) {
- this.clusters.clear();
- this.clusters.addAll(clusters);
- }
-
@Override
- public void configure(JobConf job) {
-
- super.configure(job);
- clusterer = new FuzzyKMeansClusterer(job);
+ protected void setup(Context context) throws IOException, InterruptedException {
+ super.setup(context);
+ Configuration conf = context.getConfiguration();
+ clusterer = new FuzzyKMeansClusterer(conf);
- String clusterPath = job.get(FuzzyKMeansConfigKeys.CLUSTER_PATH_KEY);
+ String clusterPath = conf.get(FuzzyKMeansConfigKeys.CLUSTER_PATH_KEY);
if ((clusterPath != null) && (clusterPath.length() > 0)) {
FuzzyKMeansUtil.configureWithClusterInfo(new Path(clusterPath), clusters);
}
@@ -78,4 +62,15 @@ public class FuzzyKMeansClusterMapper ex
}
}
+ /**
+ * Configure the mapper by providing its clusters. Used by unit tests.
+ *
+ * @param clusters
+ * a List<Cluster>
+ */
+ void setup(List<SoftCluster> clusters, Configuration conf) {
+ this.clusters.clear();
+ this.clusters.addAll(clusters);
+ this.clusterer = new FuzzyKMeansClusterer(conf);
+ }
}
Modified: mahout/trunk/core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/FuzzyKMeansClusterer.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/FuzzyKMeansClusterer.java?rev=953483&r1=953482&r2=953483&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/FuzzyKMeansClusterer.java (original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/FuzzyKMeansClusterer.java Thu Jun 10 22:25:49 2010
@@ -21,10 +21,11 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
+import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
-import org.apache.hadoop.mapred.JobConf;
-import org.apache.hadoop.mapred.OutputCollector;
+import org.apache.hadoop.io.WritableComparable;
+import org.apache.hadoop.mapreduce.Mapper;
import org.apache.mahout.clustering.WeightedVectorWritable;
import org.apache.mahout.common.distance.DistanceMeasure;
import org.apache.mahout.math.DenseVector;
@@ -60,8 +61,8 @@ public class FuzzyKMeansClusterer {
this.m = m;
}
- public FuzzyKMeansClusterer(JobConf job) {
- this.configure(job);
+ public FuzzyKMeansClusterer(Configuration conf) {
+ this.configure(conf);
}
/**
@@ -70,7 +71,7 @@ public class FuzzyKMeansClusterer {
* @param job
* the JobConf for the job
*/
- private void configure(JobConf job) {
+ private void configure(Configuration job) {
try {
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
Class<?> cl = ccl.loadClass(job.get(FuzzyKMeansConfigKeys.DISTANCE_MEASURE_KEY));
@@ -98,12 +99,12 @@ public class FuzzyKMeansClusterer {
* a point
* @param clusters
* a List<SoftCluster>
- * @param output
- * the OutputCollector to emit into
+ * @param context
+ * the Context to emit into
+ * @throws InterruptedException
*/
- public void emitPointProbToCluster(Vector point,
- List<SoftCluster> clusters,
- OutputCollector<Text, FuzzyKMeansInfo> output) throws IOException {
+ public void emitPointProbToCluster(Vector point, List<SoftCluster> clusters,
+ Mapper<WritableComparable<?>, VectorWritable, Text, FuzzyKMeansInfo>.Context context) throws IOException, InterruptedException {
List<Double> clusterDistanceList = new ArrayList<Double>();
for (SoftCluster cluster : clusters) {
@@ -113,13 +114,8 @@ public class FuzzyKMeansClusterer {
for (int i = 0; i < clusters.size(); i++) {
double probWeight = computeProbWeight(clusterDistanceList.get(i), clusterDistanceList);
Text key = new Text(clusters.get(i).getIdentifier());
- // just output the identifier,avoids too much data traffic
- /*
- * Text value = new Text(Double.toString(probWeight) + FuzzyKMeansDriver.MAPPER_VALUE_SEPARATOR +
- * values.toString());
- */
FuzzyKMeansInfo value = new FuzzyKMeansInfo(probWeight, point);
- output.collect(key, value);
+ context.write(key, value);
}
}
@@ -158,7 +154,8 @@ public class FuzzyKMeansClusterer {
}
public void emitPointToClusters(VectorWritable point, List<SoftCluster> clusters,
- OutputCollector<IntWritable, WeightedVectorWritable> output) throws IOException {
+ Mapper<WritableComparable<?>, VectorWritable, IntWritable, WeightedVectorWritable>.Context context) throws IOException,
+ InterruptedException {
// calculate point distances for all clusters
List<Double> clusterDistanceList = new ArrayList<Double>();
for (SoftCluster cluster : clusters) {
@@ -171,9 +168,9 @@ public class FuzzyKMeansClusterer {
pi.set(i, probWeight);
}
if (emitMostLikely) {
- emitMostLikelyCluster(point.get(), clusters, pi, output);
+ emitMostLikelyCluster(point.get(), clusters, pi, context);
} else {
- emitAllClusters(point.get(), clusters, pi, output);
+ emitAllClusters(point.get(), clusters, pi, context);
}
}
@@ -185,9 +182,11 @@ public class FuzzyKMeansClusterer {
* @param clusterPdfList
* @param output
* @throws IOException
+ * @throws InterruptedException
*/
static void emitMostLikelyCluster(Vector point, List<SoftCluster> clusters, Vector clusterPdfList,
- OutputCollector<IntWritable, WeightedVectorWritable> output) throws IOException {
+ Mapper<WritableComparable<?>, VectorWritable, IntWritable, WeightedVectorWritable>.Context context) throws IOException,
+ InterruptedException {
int clusterId = -1;
double clusterPdf = 0;
for (int i = 0; i < clusters.size(); i++) {
@@ -199,7 +198,7 @@ public class FuzzyKMeansClusterer {
}
}
// System.out.println("cluster-" + clusterId + ": " + ClusterBase.formatVector(point, null));
- output.collect(new IntWritable(clusterId), new WeightedVectorWritable(clusterPdf, new VectorWritable(point)));
+ context.write(new IntWritable(clusterId), new WeightedVectorWritable(clusterPdf, new VectorWritable(point)));
}
/**
@@ -210,14 +209,16 @@ public class FuzzyKMeansClusterer {
* @param pi
* @param output
* @throws IOException
+ * @throws InterruptedException
*/
void emitAllClusters(Vector point, List<SoftCluster> clusters, Vector pi,
- OutputCollector<IntWritable, WeightedVectorWritable> output) throws IOException {
+ Mapper<WritableComparable<?>, VectorWritable, IntWritable, WeightedVectorWritable>.Context context) throws IOException,
+ InterruptedException {
for (int i = 0; i < clusters.size(); i++) {
double pdf = pi.get(i);
if (pdf > threshold) {
// System.out.println("cluster-" + clusterId + ": " + ClusterBase.formatVector(point, null));
- output.collect(new IntWritable(i), new WeightedVectorWritable(pdf, new VectorWritable(point)));
+ context.write(new IntWritable(i), new WeightedVectorWritable(pdf, new VectorWritable(point)));
}
}
}
@@ -241,12 +242,8 @@ public class FuzzyKMeansClusterer {
* @return
* a List<List<SoftCluster>> of clusters produced per iteration
*/
- public static List<List<SoftCluster>> clusterPoints(List<Vector> points,
- List<SoftCluster> clusters,
- DistanceMeasure measure,
- double threshold,
- double m,
- int numIter) {
+ public static List<List<SoftCluster>> clusterPoints(List<Vector> points, List<SoftCluster> clusters, DistanceMeasure measure,
+ double threshold, double m, int numIter) {
List<List<SoftCluster>> clustersList = new ArrayList<List<SoftCluster>>();
clustersList.add(clusters);
FuzzyKMeansClusterer clusterer = new FuzzyKMeansClusterer(measure, threshold, m);
@@ -273,9 +270,7 @@ public class FuzzyKMeansClusterer {
* @param clusterList
* the List<Cluster> clusters
*/
- public static boolean runFuzzyKMeansIteration(List<Vector> points,
- List<SoftCluster> clusterList,
- FuzzyKMeansClusterer clusterer) {
+ public static boolean runFuzzyKMeansIteration(List<Vector> points, List<SoftCluster> clusterList, FuzzyKMeansClusterer clusterer) {
for (Vector point : points) {
List<Double> clusterDistanceList = new ArrayList<Double>();
for (SoftCluster cluster : clusterList) {
Modified: mahout/trunk/core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/FuzzyKMeansCombiner.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/FuzzyKMeansCombiner.java?rev=953483&r1=953482&r2=953483&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/FuzzyKMeansCombiner.java (original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/FuzzyKMeansCombiner.java Thu Jun 10 22:25:49 2010
@@ -21,27 +21,22 @@ import java.io.IOException;
import java.util.Iterator;
import org.apache.hadoop.io.Text;
-import org.apache.hadoop.mapred.JobConf;
-import org.apache.hadoop.mapred.MapReduceBase;
-import org.apache.hadoop.mapred.OutputCollector;
-import org.apache.hadoop.mapred.Reducer;
-import org.apache.hadoop.mapred.Reporter;
+import org.apache.hadoop.mapreduce.Reducer;
-public class FuzzyKMeansCombiner extends MapReduceBase implements
- Reducer<Text,FuzzyKMeansInfo,Text,FuzzyKMeansInfo> {
+public class FuzzyKMeansCombiner extends Reducer<Text,FuzzyKMeansInfo,Text,FuzzyKMeansInfo> {
private FuzzyKMeansClusterer clusterer;
+
+ /* (non-Javadoc)
+ * @see org.apache.hadoop.mapreduce.Reducer#reduce(java.lang.Object, java.lang.Iterable, org.apache.hadoop.mapreduce.Reducer.Context)
+ */
@Override
- public void reduce(Text key,
- Iterator<FuzzyKMeansInfo> values,
- OutputCollector<Text,FuzzyKMeansInfo> output,
- Reporter reporter) throws IOException {
+ protected void reduce(Text key, Iterable<FuzzyKMeansInfo> values, Context context) throws IOException, InterruptedException {
SoftCluster cluster = new SoftCluster(key.toString().trim());
- while (values.hasNext()) {
- // String pointInfo = values.next().toString();
- FuzzyKMeansInfo info = values.next();
-
+ Iterator<FuzzyKMeansInfo> it = values.iterator();
+ while (it.hasNext()) {
+ FuzzyKMeansInfo info = it.next();
if (info.getCombinerPass() == 0) { // first time thru combiner
cluster.addPoint(info.getVector(), Math.pow(info.getProbability(), clusterer.getM()));
} else {
@@ -50,13 +45,15 @@ public class FuzzyKMeansCombiner extends
info.setCombinerPass(info.getCombinerPass() + 1);
}
// TODO: how do we pass along the combinerPass? Or do we not need to?
- output.collect(key, new FuzzyKMeansInfo(cluster.getPointProbSum(), cluster.getWeightedPointTotal(), 1));
- }
-
+ context.write(key, new FuzzyKMeansInfo(cluster.getPointProbSum(), cluster.getWeightedPointTotal(), 1));
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.hadoop.mapreduce.Reducer#setup(org.apache.hadoop.mapreduce.Reducer.Context)
+ */
@Override
- public void configure(JobConf job) {
- super.configure(job);
- clusterer = new FuzzyKMeansClusterer(job);
+ protected void setup(Context context) throws IOException, InterruptedException {
+ super.setup(context);
+ clusterer = new FuzzyKMeansClusterer(context.getConfiguration());
}
-
}
Modified: mahout/trunk/core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/FuzzyKMeansDriver.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/FuzzyKMeansDriver.java?rev=953483&r1=953482&r2=953483&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/FuzzyKMeansDriver.java (original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/FuzzyKMeansDriver.java Thu Jun 10 22:25:49 2010
@@ -36,12 +36,11 @@ import org.apache.hadoop.fs.PathFilter;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Text;
-import org.apache.hadoop.mapred.FileInputFormat;
-import org.apache.hadoop.mapred.FileOutputFormat;
-import org.apache.hadoop.mapred.JobClient;
-import org.apache.hadoop.mapred.JobConf;
-import org.apache.hadoop.mapred.SequenceFileInputFormat;
-import org.apache.hadoop.mapred.SequenceFileOutputFormat;
+import org.apache.hadoop.mapreduce.Job;
+import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
+import org.apache.hadoop.mapreduce.lib.input.SequenceFileInputFormat;
+import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
+import org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat;
import org.apache.mahout.clustering.Cluster;
import org.apache.mahout.clustering.WeightedVectorWritable;
import org.apache.mahout.clustering.kmeans.RandomSeedGenerator;
@@ -167,6 +166,7 @@ public final class FuzzyKMeansDriver {
* a boolean if true emit only most likely cluster for each point
* @param threshold
* a double threshold value emits all clusters having greater pdf (emitMostLikely = false)
+ * @throws IOException
*/
public static void runJob(Path input,
Path clustersIn,
@@ -179,7 +179,7 @@ public final class FuzzyKMeansDriver {
float m,
boolean runClustering,
boolean emitMostLikely,
- double threshold) {
+ double threshold) throws IOException {
boolean converged = false;
int iteration = 1;
@@ -239,6 +239,7 @@ public final class FuzzyKMeansDriver {
* the fuzzification factor - see
* http://en.wikipedia.org/wiki/Data_clustering#Fuzzy_c-means_clustering
* @return true if the iteration successfully runs
+ * @throws IOException
*/
private static boolean runIteration(Path input,
Path clustersIn,
@@ -248,28 +249,9 @@ public final class FuzzyKMeansDriver {
int numMapTasks,
int numReduceTasks,
int iterationNumber,
- float m) {
-
- JobConf conf = new JobConf(FuzzyKMeansDriver.class);
- conf.setJobName("Fuzzy K Means{" + iterationNumber + '}');
-
- conf.setMapOutputKeyClass(Text.class);
- conf.setMapOutputValueClass(FuzzyKMeansInfo.class);
- conf.setOutputKeyClass(Text.class);
- conf.setOutputValueClass(SoftCluster.class);
-
- FileInputFormat.setInputPaths(conf, input);
- FileOutputFormat.setOutputPath(conf, clustersOut);
-
- conf.setInputFormat(SequenceFileInputFormat.class);
- conf.setOutputFormat(SequenceFileOutputFormat.class);
-
- conf.setMapperClass(FuzzyKMeansMapper.class);
- conf.setCombinerClass(FuzzyKMeansCombiner.class);
- conf.setReducerClass(FuzzyKMeansReducer.class);
- conf.setNumMapTasks(numMapTasks);
- conf.setNumReduceTasks(numReduceTasks);
+ float m) throws IOException {
+ Configuration conf = new Configuration();
conf.set(FuzzyKMeansConfigKeys.CLUSTER_PATH_KEY, clustersIn.toString());
conf.set(FuzzyKMeansConfigKeys.DISTANCE_MEASURE_KEY, measureClass);
conf.set(FuzzyKMeansConfigKeys.CLUSTER_CONVERGENCE_KEY, String.valueOf(convergenceDelta));
@@ -277,17 +259,40 @@ public final class FuzzyKMeansDriver {
// these values don't matter during iterations as only used for clustering if requested
conf.set(FuzzyKMeansConfigKeys.EMIT_MOST_LIKELY_KEY, Boolean.toString(true));
conf.set(FuzzyKMeansConfigKeys.THRESHOLD_KEY, Double.toString(0));
+
+ Job job = new Job(conf);
+ job.setMapOutputKeyClass(Text.class);
+ job.setMapOutputValueClass(FuzzyKMeansInfo.class);
+ job.setOutputKeyClass(Text.class);
+ job.setOutputValueClass(SoftCluster.class);
+ job.setInputFormatClass(SequenceFileInputFormat.class);
+ job.setOutputFormatClass(SequenceFileOutputFormat.class);
+
+ job.setMapperClass(FuzzyKMeansMapper.class);
+ job.setCombinerClass(FuzzyKMeansCombiner.class);
+ job.setReducerClass(FuzzyKMeansReducer.class);
+ //TODO: job.setNumMapTasks(numMapTasks);
+ job.setNumReduceTasks(numReduceTasks);
+
+ FileInputFormat.addInputPath(job, input);
+ FileOutputFormat.setOutputPath(job, clustersOut);
// uncomment it to run locally
// conf.set("mapred.job.tracker", "local");
try {
- JobClient.runJob(conf);
+ job.waitForCompletion(true);
FileSystem fs = FileSystem.get(clustersOut.toUri(), conf);
return isConverged(clustersOut, conf, fs);
} catch (IOException e) {
log.warn(e.toString(), e);
return true;
+ } catch (InterruptedException e) {
+ log.warn(e.toString(), e);
+ return true;
+ } catch (ClassNotFoundException e) {
+ log.warn(e.toString(), e);
+ return true;
}
}
@@ -310,6 +315,7 @@ public final class FuzzyKMeansDriver {
* a boolean if true emit only most likely cluster for each point
* @param threshold
* a double threshold value emits all clusters having greater pdf (emitMostLikely = false)
+ * @throws IOException
*/
private static void runClustering(Path input,
Path clustersIn,
@@ -319,36 +325,39 @@ public final class FuzzyKMeansDriver {
int numMapTasks,
float m,
boolean emitMostLikely,
- double threshold) {
+ double threshold) throws IOException {
- JobConf conf = new JobConf(FuzzyKMeansDriver.class);
- conf.setJobName("Fuzzy K Means Clustering");
-
- conf.setOutputKeyClass(IntWritable.class);
- conf.setOutputValueClass(WeightedVectorWritable.class);
-
- FileInputFormat.setInputPaths(conf, input);
- FileOutputFormat.setOutputPath(conf, output);
-
- conf.setMapperClass(FuzzyKMeansClusterMapper.class);
-
- conf.setInputFormat(SequenceFileInputFormat.class);
- conf.setOutputFormat(SequenceFileOutputFormat.class);
-
- // uncomment it to run locally
- // conf.set("mapred.job.tracker", "local");
- conf.setNumMapTasks(numMapTasks);
- conf.setNumReduceTasks(0);
+ Configuration conf = new Configuration();
conf.set(FuzzyKMeansConfigKeys.CLUSTER_PATH_KEY, clustersIn.toString());
conf.set(FuzzyKMeansConfigKeys.DISTANCE_MEASURE_KEY, measureClass);
conf.set(FuzzyKMeansConfigKeys.CLUSTER_CONVERGENCE_KEY, String.valueOf(convergenceDelta));
conf.set(FuzzyKMeansConfigKeys.M_KEY, String.valueOf(m));
conf.set(FuzzyKMeansConfigKeys.EMIT_MOST_LIKELY_KEY, Boolean.toString(emitMostLikely));
conf.set(FuzzyKMeansConfigKeys.THRESHOLD_KEY, Double.toString(threshold));
+
+ Job job = new Job(conf);
+ job.setOutputKeyClass(IntWritable.class);
+ job.setOutputValueClass(WeightedVectorWritable.class);
+
+ FileInputFormat.setInputPaths(job, input);
+ FileOutputFormat.setOutputPath(job, output);
+
+ job.setMapperClass(FuzzyKMeansClusterMapper.class);
+
+ job.setInputFormatClass(SequenceFileInputFormat.class);
+ job.setOutputFormatClass(SequenceFileOutputFormat.class);
+ //TODO: job.setNumMapTasks(numMapTasks);
+ job.setNumReduceTasks(0);
try {
- JobClient.runJob(conf);
+ job.waitForCompletion(true);
} catch (IOException e) {
log.warn(e.toString(), e);
+ } catch (InterruptedException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (ClassNotFoundException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
}
}
Modified: mahout/trunk/core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/FuzzyKMeansMapper.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/FuzzyKMeansMapper.java?rev=953483&r1=953482&r2=953483&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/FuzzyKMeansMapper.java (original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/FuzzyKMeansMapper.java Thu Jun 10 22:25:49 2010
@@ -21,49 +21,38 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
+import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;
-import org.apache.hadoop.mapred.JobConf;
-import org.apache.hadoop.mapred.MapReduceBase;
-import org.apache.hadoop.mapred.Mapper;
-import org.apache.hadoop.mapred.OutputCollector;
-import org.apache.hadoop.mapred.Reporter;
+import org.apache.hadoop.mapreduce.Mapper;
import org.apache.mahout.math.VectorWritable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-public class FuzzyKMeansMapper extends MapReduceBase implements
- Mapper<WritableComparable<?>,VectorWritable,Text,FuzzyKMeansInfo> {
+public class FuzzyKMeansMapper extends Mapper<WritableComparable<?>,VectorWritable,Text,FuzzyKMeansInfo> {
private static final Logger log = LoggerFactory.getLogger(FuzzyKMeansMapper.class);
private final List<SoftCluster> clusters = new ArrayList<SoftCluster>();
private FuzzyKMeansClusterer clusterer;
- @Override
- public void map(WritableComparable<?> key,
- VectorWritable point,
- OutputCollector<Text,FuzzyKMeansInfo> output,
- Reporter reporter) throws IOException {
- clusterer.emitPointProbToCluster(point.get(), clusters, output);
- }
- /**
- * Configure the mapper by providing its clusters. Used by unit tests.
- *
- * @param clusters
- * a List<Cluster>
+ /* (non-Javadoc)
+ * @see org.apache.hadoop.mapreduce.Mapper#map(java.lang.Object, java.lang.Object, org.apache.hadoop.mapreduce.Mapper.Context)
*/
- void config(List<SoftCluster> clusters) {
- this.clusters.clear();
- this.clusters.addAll(clusters);
+ @Override
+ protected void map(WritableComparable<?> key, VectorWritable point, Context context) throws IOException, InterruptedException {
+ clusterer.emitPointProbToCluster(point.get(), clusters, context);
}
-
+
+ /* (non-Javadoc)
+ * @see org.apache.hadoop.mapreduce.Mapper#setup(org.apache.hadoop.mapreduce.Mapper.Context)
+ */
@Override
- public void configure(JobConf job) {
-
- super.configure(job);
+ protected void setup(Context context) throws IOException, InterruptedException {
+ super.setup(context);
+ Configuration job = context.getConfiguration();
clusterer = new FuzzyKMeansClusterer(job);
log.info("In Mapper Configure:");
@@ -77,5 +66,15 @@ public class FuzzyKMeansMapper extends M
throw new IllegalStateException("Cluster is empty!!!");
}
}
-
+
+ /**
+ * Configure the mapper by providing its clusters. Used by unit tests.
+ *
+ * @param clusters
+ * a List<Cluster>
+ */
+ void config(List<SoftCluster> clusters) {
+ this.clusters.clear();
+ this.clusters.addAll(clusters);
+ }
}
Modified: mahout/trunk/core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/FuzzyKMeansReducer.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/FuzzyKMeansReducer.java?rev=953483&r1=953482&r2=953483&view=diff
==============================================================================
--- mahout/trunk/core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/FuzzyKMeansReducer.java (original)
+++ mahout/trunk/core/src/main/java/org/apache/mahout/clustering/fuzzykmeans/FuzzyKMeansReducer.java Thu Jun 10 22:25:49 2010
@@ -24,64 +24,64 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
-import org.apache.hadoop.mapred.JobConf;
-import org.apache.hadoop.mapred.MapReduceBase;
-import org.apache.hadoop.mapred.OutputCollector;
-import org.apache.hadoop.mapred.Reducer;
-import org.apache.hadoop.mapred.Reporter;
-
-public class FuzzyKMeansReducer extends MapReduceBase implements
- Reducer<Text,FuzzyKMeansInfo,Text,SoftCluster> {
-
- private final Map<String,SoftCluster> clusterMap = new HashMap<String,SoftCluster>();
+import org.apache.hadoop.mapreduce.Reducer;
+import org.apache.mahout.common.distance.DistanceMeasure;
+
+public class FuzzyKMeansReducer extends Reducer<Text, FuzzyKMeansInfo, Text, SoftCluster> {
+
+ private final Map<String, SoftCluster> clusterMap = new HashMap<String, SoftCluster>();
+
private FuzzyKMeansClusterer clusterer;
-
+
+ /* (non-Javadoc)
+ * @see org.apache.hadoop.mapreduce.Reducer#reduce(java.lang.Object, java.lang.Iterable, org.apache.hadoop.mapreduce.Reducer.Context)
+ */
@Override
- public void reduce(Text key,
- Iterator<FuzzyKMeansInfo> values,
- OutputCollector<Text,SoftCluster> output,
- Reporter reporter) throws IOException {
-
+ protected void reduce(Text key, Iterable<FuzzyKMeansInfo> values, Context context) throws IOException, InterruptedException {
SoftCluster cluster = clusterMap.get(key.toString());
-
- while (values.hasNext()) {
- FuzzyKMeansInfo value = values.next();
-
+ Iterator<FuzzyKMeansInfo> it = values.iterator();
+ while (it.hasNext()) {
+ FuzzyKMeansInfo value = it.next();
+
if (value.getCombinerPass() == 0) { // escaped from combiner
cluster.addPoint(value.getVector(), Math.pow(value.getProbability(), clusterer.getM()));
} else {
cluster.addPoints(value.getVector(), value.getProbability());
}
-
+
}
// force convergence calculation
boolean converged = clusterer.computeConvergence(cluster);
if (converged) {
- reporter.incrCounter("Clustering", "Converged Clusters", 1);
+ // TODO: reporter.incrCounter("Clustering", "Converged Clusters", 1);
}
- output.collect(new Text(cluster.getIdentifier()), cluster);
+ context.write(new Text(cluster.getIdentifier()), cluster);
}
-
+
+ /* (non-Javadoc)
+ * @see org.apache.hadoop.mapreduce.Reducer#setup(org.apache.hadoop.mapreduce.Reducer.Context)
+ */
@Override
- public void configure(JobConf job) {
-
- super.configure(job);
- clusterer = new FuzzyKMeansClusterer(job);
-
+ protected void setup(Context context) throws IOException, InterruptedException {
+ super.setup(context);
+ Configuration conf = context.getConfiguration();
+ clusterer = new FuzzyKMeansClusterer(conf);
+
List<SoftCluster> clusters = new ArrayList<SoftCluster>();
- String clusterPath = job.get(FuzzyKMeansConfigKeys.CLUSTER_PATH_KEY);
+ String clusterPath = conf.get(FuzzyKMeansConfigKeys.CLUSTER_PATH_KEY);
if ((clusterPath != null) && (clusterPath.length() > 0)) {
FuzzyKMeansUtil.configureWithClusterInfo(new Path(clusterPath), clusters);
setClusterMap(clusters);
}
-
+
if (clusterMap.isEmpty()) {
throw new IllegalStateException("Cluster is empty!!!");
}
}
-
+
private void setClusterMap(List<SoftCluster> clusters) {
clusterMap.clear();
for (SoftCluster cluster : clusters) {
@@ -89,9 +89,10 @@ public class FuzzyKMeansReducer extends
}
clusters.clear();
}
-
- public void config(List<SoftCluster> clusters) {
+
+ public void setup(List<SoftCluster> clusters, Configuration conf) {
setClusterMap(clusters);
+ clusterer = new FuzzyKMeansClusterer(conf);
}
-
+
}
Modified: mahout/trunk/core/src/test/java/org/apache/mahout/clustering/fuzzykmeans/TestFuzzyKmeansClustering.java
URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/test/java/org/apache/mahout/clustering/fuzzykmeans/TestFuzzyKmeansClustering.java?rev=953483&r1=953482&r2=953483&view=diff
==============================================================================
--- mahout/trunk/core/src/test/java/org/apache/mahout/clustering/fuzzykmeans/TestFuzzyKmeansClustering.java (original)
+++ mahout/trunk/core/src/test/java/org/apache/mahout/clustering/fuzzykmeans/TestFuzzyKmeansClustering.java Thu Jun 10 22:25:49 2010
@@ -29,12 +29,12 @@ import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Text;
-import org.apache.hadoop.mapred.JobConf;
import org.apache.mahout.clustering.ClusteringTestUtils;
+import org.apache.mahout.clustering.MockMapperContext;
+import org.apache.mahout.clustering.MockReducerContext;
import org.apache.mahout.clustering.WeightedVectorWritable;
import org.apache.mahout.clustering.kmeans.TestKmeansClustering;
import org.apache.mahout.common.DummyOutputCollector;
-import org.apache.mahout.common.DummyReporter;
import org.apache.mahout.common.MahoutTestCase;
import org.apache.mahout.common.distance.DistanceMeasure;
import org.apache.mahout.common.distance.EuclideanDistanceMeasure;
@@ -199,10 +199,10 @@ public class TestFuzzyKmeansClustering e
// now run the Job
Path output = getTestTempDirPath("output");
- FuzzyKMeansDriver.runJob(pointsPath, clustersPath, output, EuclideanDistanceMeasure.class.getName(), 0.001,
- 2, 1, k + 1, 2, false, true, 0);
+ FuzzyKMeansDriver.runJob(pointsPath, clustersPath, output, EuclideanDistanceMeasure.class.getName(), 0.001, 2, 1, k + 1, 2,
+ false, true, 0);
- SequenceFile.Reader reader = new SequenceFile.Reader(fs, new Path(output, "clusteredPoints/part-00000"), conf);
+ SequenceFile.Reader reader = new SequenceFile.Reader(fs, new Path(output, "clusteredPoints/part-m-00000"), conf);
IntWritable key = new IntWritable();
WeightedVectorWritable out = new WeightedVectorWritable();
while (reader.next(key, out)) {
@@ -233,18 +233,19 @@ public class TestFuzzyKmeansClustering e
// run mapper
FuzzyKMeansMapper mapper = new FuzzyKMeansMapper();
mapper.config(clusterList);
-
- JobConf conf = new JobConf();
- conf.set(FuzzyKMeansConfigKeys.DISTANCE_MEASURE_KEY, "org.apache.mahout.common.distance.EuclideanDistanceMeasure");
+ DistanceMeasure measure = new EuclideanDistanceMeasure();
+ Configuration conf = new Configuration();
+ conf.set(FuzzyKMeansConfigKeys.DISTANCE_MEASURE_KEY, measure.getClass().getName());
conf.set(FuzzyKMeansConfigKeys.CLUSTER_CONVERGENCE_KEY, "0.001");
conf.set(FuzzyKMeansConfigKeys.M_KEY, "2");
conf.set(FuzzyKMeansConfigKeys.EMIT_MOST_LIKELY_KEY, "true");
conf.set(FuzzyKMeansConfigKeys.THRESHOLD_KEY, "0");
- mapper.configure(conf);
DummyOutputCollector<Text, FuzzyKMeansInfo> mapCollector = new DummyOutputCollector<Text, FuzzyKMeansInfo>();
+ MockMapperContext<Text, FuzzyKMeansInfo> mapContext = new MockMapperContext<Text, FuzzyKMeansInfo>(mapper, conf, mapCollector);
+ mapper.setup(mapContext);
for (VectorWritable point : points) {
- mapper.map(new Text(), point, mapCollector, null);
+ mapper.map(new Text(), point, mapContext);
}
// now verify mapper output
@@ -257,17 +258,14 @@ public class TestFuzzyKmeansClustering e
List<FuzzyKMeansInfo> values = mapCollector.getValue(key);
for (FuzzyKMeansInfo value : values) {
-
Double val = pointTotalProbMap.get(value.getVector());
double probVal = 0.0;
if (val != null) {
probVal = val;
}
-
pointTotalProbMap.put(value.getVector(), probVal + value.getProbability());
}
}
-
for (Map.Entry<Vector, Double> entry : pointTotalProbMap.entrySet()) {
Vector key = entry.getKey();
double value = round(entry.getValue(), 1);
@@ -297,28 +295,29 @@ public class TestFuzzyKmeansClustering e
FuzzyKMeansMapper mapper = new FuzzyKMeansMapper();
mapper.config(clusterList);
- JobConf conf = new JobConf();
+ Configuration conf = new Configuration();
conf.set(FuzzyKMeansConfigKeys.DISTANCE_MEASURE_KEY, "org.apache.mahout.common.distance.EuclideanDistanceMeasure");
conf.set(FuzzyKMeansConfigKeys.CLUSTER_CONVERGENCE_KEY, "0.001");
conf.set(FuzzyKMeansConfigKeys.M_KEY, "2");
conf.set(FuzzyKMeansConfigKeys.EMIT_MOST_LIKELY_KEY, "true");
conf.set(FuzzyKMeansConfigKeys.THRESHOLD_KEY, "0");
- mapper.configure(conf);
DummyOutputCollector<Text, FuzzyKMeansInfo> mapCollector = new DummyOutputCollector<Text, FuzzyKMeansInfo>();
+ MockMapperContext<Text, FuzzyKMeansInfo> mapContext = new MockMapperContext<Text, FuzzyKMeansInfo>(mapper, conf, mapCollector);
+ mapper.setup(mapContext);
for (VectorWritable point : points) {
- mapper.map(new Text(), point, mapCollector, null);
+ mapper.map(new Text(), point, mapContext);
}
// run combiner
- DummyOutputCollector<Text, FuzzyKMeansInfo> combinerCollector = new DummyOutputCollector<Text, FuzzyKMeansInfo>();
FuzzyKMeansCombiner combiner = new FuzzyKMeansCombiner();
- combiner.configure(conf);
-
+ DummyOutputCollector<Text, FuzzyKMeansInfo> combinerCollector = new DummyOutputCollector<Text, FuzzyKMeansInfo>();
+ MockReducerContext<Text, FuzzyKMeansInfo> combinerContext = new MockReducerContext<Text, FuzzyKMeansInfo>(combiner, conf,
+ combinerCollector, Text.class, FuzzyKMeansInfo.class);
+ combiner.setup(combinerContext);
for (Text key : mapCollector.getKeys()) {
-
List<FuzzyKMeansInfo> values = mapCollector.getValue(key);
- combiner.reduce(new Text(key), values.iterator(), combinerCollector, null);
+ combiner.reduce(new Text(key), values, combinerContext);
}
// now verify the combiner output
@@ -350,39 +349,42 @@ public class TestFuzzyKmeansClustering e
// run mapper
FuzzyKMeansMapper mapper = new FuzzyKMeansMapper();
mapper.config(clusterList);
-
- JobConf conf = new JobConf();
- conf.set(FuzzyKMeansConfigKeys.DISTANCE_MEASURE_KEY, "org.apache.mahout.common.distance.EuclideanDistanceMeasure");
+ DistanceMeasure measure = new EuclideanDistanceMeasure();
+ Configuration conf = new Configuration();
+ conf.set(FuzzyKMeansConfigKeys.DISTANCE_MEASURE_KEY, measure.getClass().getName());
conf.set(FuzzyKMeansConfigKeys.CLUSTER_CONVERGENCE_KEY, "0.001");
conf.set(FuzzyKMeansConfigKeys.M_KEY, "2");
conf.set(FuzzyKMeansConfigKeys.EMIT_MOST_LIKELY_KEY, "true");
conf.set(FuzzyKMeansConfigKeys.THRESHOLD_KEY, "0");
- mapper.configure(conf);
DummyOutputCollector<Text, FuzzyKMeansInfo> mapCollector = new DummyOutputCollector<Text, FuzzyKMeansInfo>();
+ MockMapperContext<Text, FuzzyKMeansInfo> mapContext = new MockMapperContext<Text, FuzzyKMeansInfo>(mapper, conf, mapCollector);
+ mapper.setup(mapContext);
for (VectorWritable point : points) {
- mapper.map(new Text(), point, mapCollector, null);
+ mapper.map(new Text(), point, mapContext);
}
// run combiner
- DummyOutputCollector<Text, FuzzyKMeansInfo> combinerCollector = new DummyOutputCollector<Text, FuzzyKMeansInfo>();
FuzzyKMeansCombiner combiner = new FuzzyKMeansCombiner();
- combiner.configure(conf);
-
+ DummyOutputCollector<Text, FuzzyKMeansInfo> combinerCollector = new DummyOutputCollector<Text, FuzzyKMeansInfo>();
+ MockReducerContext<Text, FuzzyKMeansInfo> combinerContext = new MockReducerContext<Text, FuzzyKMeansInfo>(combiner, conf,
+ combinerCollector, Text.class, FuzzyKMeansInfo.class);
+ combiner.setup(combinerContext);
for (Text key : mapCollector.getKeys()) {
List<FuzzyKMeansInfo> values = mapCollector.getValue(key);
- combiner.reduce(new Text(key), values.iterator(), combinerCollector, null);
+ combiner.reduce(new Text(key), values, combinerContext);
}
// run reducer
- DummyOutputCollector<Text, SoftCluster> reducerCollector = new DummyOutputCollector<Text, SoftCluster>();
FuzzyKMeansReducer reducer = new FuzzyKMeansReducer();
- reducer.config(clusterList);
- reducer.configure(conf);
+ DummyOutputCollector<Text, SoftCluster> reducerCollector = new DummyOutputCollector<Text, SoftCluster>();
+ MockReducerContext<Text, SoftCluster> reducerContext = new MockReducerContext<Text, SoftCluster>(reducer, conf,
+ reducerCollector, Text.class, SoftCluster.class);
+ reducer.setup(clusterList, conf);
for (Text key : combinerCollector.getKeys()) {
List<FuzzyKMeansInfo> values = combinerCollector.getValue(key);
- reducer.reduce(new Text(key), values.iterator(), reducerCollector, new DummyReporter());
+ reducer.reduce(new Text(key), values, reducerContext);
}
// now verify the reducer output
@@ -399,7 +401,6 @@ public class TestFuzzyKmeansClustering e
pointsVectors.add((Vector) point.get());
}
- DistanceMeasure measure = new EuclideanDistanceMeasure();
FuzzyKMeansClusterer clusterer = new FuzzyKMeansClusterer(measure, 0.001, 2);
FuzzyKMeansClusterer.runFuzzyKMeansIteration(pointsVectors, reference, clusterer);
@@ -430,71 +431,72 @@ public class TestFuzzyKmeansClustering e
cluster.addPoint(cluster.getCenter(), 1);
clusterList.add(cluster);
}
+ for (SoftCluster softCluster : clusterList) {
+ softCluster.recomputeCenter();
+ }
// run mapper
FuzzyKMeansMapper mapper = new FuzzyKMeansMapper();
mapper.config(clusterList);
+ DistanceMeasure measure = new EuclideanDistanceMeasure();
- JobConf conf = new JobConf();
- conf.set(FuzzyKMeansConfigKeys.DISTANCE_MEASURE_KEY, "org.apache.mahout.common.distance.EuclideanDistanceMeasure");
+ Configuration conf = new Configuration();
+ conf.set(FuzzyKMeansConfigKeys.DISTANCE_MEASURE_KEY, measure.getClass().getName());
conf.set(FuzzyKMeansConfigKeys.CLUSTER_CONVERGENCE_KEY, "0.001");
conf.set(FuzzyKMeansConfigKeys.M_KEY, "2");
conf.set(FuzzyKMeansConfigKeys.EMIT_MOST_LIKELY_KEY, "true");
conf.set(FuzzyKMeansConfigKeys.THRESHOLD_KEY, "0");
- mapper.configure(conf);
DummyOutputCollector<Text, FuzzyKMeansInfo> mapCollector = new DummyOutputCollector<Text, FuzzyKMeansInfo>();
+ MockMapperContext<Text, FuzzyKMeansInfo> mapContext = new MockMapperContext<Text, FuzzyKMeansInfo>(mapper, conf, mapCollector);
+ mapper.setup(mapContext);
for (VectorWritable point : points) {
- mapper.map(new Text(), point, mapCollector, null);
- }
- for (SoftCluster softCluster : clusterList) {
- softCluster.recomputeCenter();
+ mapper.map(new Text(), point, mapContext);
}
+
// run combiner
- DummyOutputCollector<Text, FuzzyKMeansInfo> combinerCollector = new DummyOutputCollector<Text, FuzzyKMeansInfo>();
FuzzyKMeansCombiner combiner = new FuzzyKMeansCombiner();
- combiner.configure(conf);
-
+ DummyOutputCollector<Text, FuzzyKMeansInfo> combinerCollector = new DummyOutputCollector<Text, FuzzyKMeansInfo>();
+ MockReducerContext<Text, FuzzyKMeansInfo> combinerContext = new MockReducerContext<Text, FuzzyKMeansInfo>(combiner, conf,
+ combinerCollector, Text.class, FuzzyKMeansInfo.class);
+ combiner.setup(combinerContext);
for (Text key : mapCollector.getKeys()) {
-
List<FuzzyKMeansInfo> values = mapCollector.getValue(key);
- combiner.reduce(new Text(key), values.iterator(), combinerCollector, null);
+ combiner.reduce(new Text(key), values, combinerContext);
}
// run reducer
- DummyOutputCollector<Text, SoftCluster> reducerCollector = new DummyOutputCollector<Text, SoftCluster>();
FuzzyKMeansReducer reducer = new FuzzyKMeansReducer();
- reducer.config(clusterList);
- reducer.configure(conf);
+ DummyOutputCollector<Text, SoftCluster> reducerCollector = new DummyOutputCollector<Text, SoftCluster>();
+ MockReducerContext<Text, SoftCluster> reducerContext = new MockReducerContext<Text, SoftCluster>(reducer, conf,
+ reducerCollector, Text.class, SoftCluster.class);
+ reducer.setup(clusterList, conf);
for (Text key : combinerCollector.getKeys()) {
List<FuzzyKMeansInfo> values = combinerCollector.getValue(key);
- reducer.reduce(new Text(key), values.iterator(), reducerCollector, null);
+ reducer.reduce(new Text(key), values, reducerContext);
}
// run clusterMapper
- List<SoftCluster> reducerCluster = new ArrayList<SoftCluster>();
-
+ List<SoftCluster> reducerClusters = new ArrayList<SoftCluster>();
for (Text key : reducerCollector.getKeys()) {
List<SoftCluster> values = reducerCollector.getValue(key);
- reducerCluster.add(values.get(0));
+ reducerClusters.add(values.get(0));
}
- for (SoftCluster softCluster : reducerCluster) {
+ for (SoftCluster softCluster : reducerClusters) {
softCluster.recomputeCenter();
}
- DummyOutputCollector<IntWritable, WeightedVectorWritable> clusterMapperCollector = new DummyOutputCollector<IntWritable, WeightedVectorWritable>();
-
FuzzyKMeansClusterMapper clusterMapper = new FuzzyKMeansClusterMapper();
- clusterMapper.config(reducerCluster);
- clusterMapper.configure(conf);
+ DummyOutputCollector<IntWritable, WeightedVectorWritable> clusterMapperCollector = new DummyOutputCollector<IntWritable, WeightedVectorWritable>();
+ MockMapperContext<IntWritable, WeightedVectorWritable> clusterMapperContext = new MockMapperContext<IntWritable, WeightedVectorWritable>(
+ clusterMapper, conf, clusterMapperCollector);
+ clusterMapper.setup(reducerClusters, conf);
for (VectorWritable point : points) {
- clusterMapper.map(new Text(), point, clusterMapperCollector, null);
+ clusterMapper.map(new Text(), point, clusterMapperContext);
}
- // now run for one iteration of referencefuzzykmeans and compare the
- // results
// compute the reference result after one iteration and compare
List<SoftCluster> reference = new ArrayList<SoftCluster>();
for (int i = 0; i < k + 1; i++) {
@@ -516,6 +518,8 @@ public class TestFuzzyKmeansClustering e
// Now compare the clustermapper results with reference implementation
assertEquals("mapper and reference sizes", refClusters.size(), clusterMapperCollector.getKeys().size());
for (int pcId : refClusters.keySet()) {
+ System.out.println("refClusters=" + refClusters.get(pcId) + " mapClusters="
+ + clusterMapperCollector.getValue(new IntWritable(pcId)));
assertEquals("cluster " + pcId + " sizes", refClusters.get(pcId).size(), clusterMapperCollector.getValue(
new IntWritable(pcId)).size());
}