VectorModelClassifier extends AbstractVectorClassifier { @Override public Vector classify(Vector instance) { Vector pdfs = new DenseVector(models.size()); if (models.get(0) instanceof SoftCluster) { Collection<SoftCluster> clusters = new ArrayList<SoftCluster>(); List<Double> distances = new ArrayList<Double>(); for (Model<VectorWritable> 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; for (Model<VectorWritable> model : models) { pdfs.set(i++, model.pdf(new VectorWritable(instance))); } return pdfs.assign(new TimesFunction(), 1.0 / pdfs.zSum()); } } VectorModelClassifier(List<Model<VectorWritable>> models); @Override Vector classify(Vector instance); @Override double classifyScalar(Vector instance); @Override int numCategories();  }
@Test public void testDMClusterClassification() { List<Model<VectorWritable>> models = new ArrayList<Model<VectorWritable>>(); DistanceMeasure measure = new ManhattanDistanceMeasure(); models.add(new DistanceMeasureCluster(new DenseVector(2).assign(1), 0, measure)); models.add(new DistanceMeasureCluster(new DenseVector(2), 1, measure)); models.add(new DistanceMeasureCluster(new DenseVector(2).assign(-1), 2, measure)); AbstractVectorClassifier classifier = new VectorModelClassifier(models); Vector pdf = classifier.classify(new DenseVector(2)); assertEquals("[0,0]", "[0.107, 0.787, 0.107]", AbstractCluster.formatVector(pdf, null)); pdf = classifier.classify(new DenseVector(2).assign(2)); assertEquals("[2,2]", "[0.867, 0.117, 0.016]", AbstractCluster.formatVector(pdf, null)); }
@Test public void testCanopyClassification() { List<Model<VectorWritable>> models = new ArrayList<Model<VectorWritable>>(); 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)); AbstractVectorClassifier classifier = new VectorModelClassifier(models); Vector pdf = classifier.classify(new DenseVector(2)); assertEquals("[0,0]", "[0.107, 0.787, 0.107]", AbstractCluster.formatVector(pdf, null)); pdf = classifier.classify(new DenseVector(2).assign(2)); assertEquals("[2,2]", "[0.867, 0.117, 0.016]", AbstractCluster.formatVector(pdf, null)); }
@Test public void testClusterClassification() { List<Model<VectorWritable>> models = new ArrayList<Model<VectorWritable>>(); DistanceMeasure measure = new ManhattanDistanceMeasure(); models.add(new Cluster(new DenseVector(2).assign(1), 0, measure)); models.add(new Cluster(new DenseVector(2), 1, measure)); models.add(new Cluster(new DenseVector(2).assign(-1), 2, measure)); AbstractVectorClassifier classifier = new VectorModelClassifier(models); Vector pdf = classifier.classify(new DenseVector(2)); assertEquals("[0,0]", "[0.107, 0.787, 0.107]", AbstractCluster.formatVector(pdf, null)); pdf = classifier.classify(new DenseVector(2).assign(2)); assertEquals("[2,2]", "[0.867, 0.117, 0.016]", AbstractCluster.formatVector(pdf, null)); }
@Test public void testMSCanopyClassification() { List<Model<VectorWritable>> models = new ArrayList<Model<VectorWritable>>(); 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)); AbstractVectorClassifier classifier = new VectorModelClassifier(models); try { classifier.classify(new DenseVector(2)); fail("Expected NotImplementedException"); } catch (NotImplementedException e) {} }
@Test public void testSoftClusterClassification() { List<Model<VectorWritable>> models = new ArrayList<Model<VectorWritable>>(); DistanceMeasure measure = new ManhattanDistanceMeasure(); models.add(new SoftCluster(new DenseVector(2).assign(1), 0, measure)); models.add(new SoftCluster(new DenseVector(2), 1, measure)); models.add(new SoftCluster(new DenseVector(2).assign(-1), 2, measure)); AbstractVectorClassifier classifier = new VectorModelClassifier(models); 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() { List<Model<VectorWritable>> models = new ArrayList<Model<VectorWritable>>(); models.add(new GaussianCluster(new DenseVector(2).assign(1), new DenseVector(2).assign(1), 0)); models.add(new GaussianCluster(new DenseVector(2), new DenseVector(2) .assign(1), 1)); models.add(new GaussianCluster(new DenseVector(2).assign(-1), new DenseVector(2).assign(1), 2)); AbstractVectorClassifier classifier = new VectorModelClassifier(models); 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)); }