ClusterClassifier extends AbstractVectorClassifier implements OnlineLearner, Writable { @Override public Vector classify(Vector instance) { if (models.get(0) instanceof SoftCluster) { Collection<SoftCluster> clusters = Lists.newArrayList(); List<Double> distances = Lists.newArrayList(); for (Cluster model : models) { SoftCluster sc = (SoftCluster) model; clusters.add(sc); distances.add(sc.getMeasure().distance(instance, sc.getCenter())); } return new FuzzyKMeansClusterer().computePi(clusters, distances); } else { int i = 0; Vector pdfs = new DenseVector(models.size()); for (Cluster model : models) { pdfs.set(i++, model.pdf(new VectorWritable(instance))); } return pdfs.assign(new TimesFunction(), 1.0 / pdfs.zSum()); } } ClusterClassifier(List<Cluster> models);  ClusterClassifier(); @Override Vector classify(Vector instance); @Override double classifyScalar(Vector instance); @Override int numCategories(); @Override void write(DataOutput out); @Override void readFields(DataInput in); @Override void train(int actual, Vector instance); void train(int actual, Vector data, double weight); @Override void train(long trackingKey, String groupKey, int actual, Vector instance); @Override void train(long trackingKey, int actual, Vector instance); @Override void close(); List<Cluster> getModels();  }
@Test public void testDMClusterClassification() { ClusterClassifier classifier = newDMClassifier(); Vector pdf = classifier.classify(new DenseVector(2)); assertEquals("[0,0]", "[0.200, 0.600, 0.200]", AbstractCluster.formatVector(pdf, null)); pdf = classifier.classify(new DenseVector(2).assign(2)); assertEquals("[2,2]", "[0.493, 0.296, 0.211]", AbstractCluster.formatVector(pdf, null)); }
@Test public void testCanopyClassification() { List<Cluster> models = Lists.newArrayList(); DistanceMeasure measure = new ManhattanDistanceMeasure(); models.add(new Canopy(new DenseVector(2).assign(1), 0, measure)); models.add(new Canopy(new DenseVector(2), 1, measure)); models.add(new Canopy(new DenseVector(2).assign(-1), 2, measure)); ClusterClassifier classifier = new ClusterClassifier(models); Vector pdf = classifier.classify(new DenseVector(2)); assertEquals("[0,0]", "[0.200, 0.600, 0.200]", AbstractCluster.formatVector(pdf, null)); pdf = classifier.classify(new DenseVector(2).assign(2)); assertEquals("[2,2]", "[0.493, 0.296, 0.211]", AbstractCluster.formatVector(pdf, null)); }
@Test public void testClusterClassification() { ClusterClassifier classifier = newClusterClassifier(); Vector pdf = classifier.classify(new DenseVector(2)); assertEquals("[0,0]", "[0.200, 0.600, 0.200]", AbstractCluster.formatVector(pdf, null)); pdf = classifier.classify(new DenseVector(2).assign(2)); assertEquals("[2,2]", "[0.493, 0.296, 0.211]", AbstractCluster.formatVector(pdf, null)); }
@Test(expected = UnsupportedOperationException.class) public void testMSCanopyClassification() { List<Cluster> models = Lists.newArrayList(); DistanceMeasure measure = new ManhattanDistanceMeasure(); models.add(new MeanShiftCanopy(new DenseVector(2).assign(1), 0, measure)); models.add(new MeanShiftCanopy(new DenseVector(2), 1, measure)); models.add(new MeanShiftCanopy(new DenseVector(2).assign(-1), 2, measure)); ClusterClassifier classifier = new ClusterClassifier(models); classifier.classify(new DenseVector(2)); }
@Test public void testSoftClusterClassification() { ClusterClassifier classifier = newSoftClusterClassifier(); Vector pdf = classifier.classify(new DenseVector(2)); assertEquals("[0,0]", "[0.000, 1.000, 0.000]", AbstractCluster.formatVector(pdf, null)); pdf = classifier.classify(new DenseVector(2).assign(2)); assertEquals("[2,2]", "[0.735, 0.184, 0.082]", AbstractCluster.formatVector(pdf, null)); }
@Test public void testGaussianClusterClassification() { ClusterClassifier classifier = newGaussianClassifier(); Vector pdf = classifier.classify(new DenseVector(2)); assertEquals("[0,0]", "[0.212, 0.576, 0.212]", AbstractCluster.formatVector(pdf, null)); pdf = classifier.classify(new DenseVector(2).assign(2)); assertEquals("[2,2]", "[0.952, 0.047, 0.000]", AbstractCluster.formatVector(pdf, null)); }