Commit acab277e by Michal Balazia

### distance map

parent 07d6abac
File deleted
 package algorithms; package algorithms; import Jama.EigenvalueDecomposition; public class Classifier1NNNareshKumarMS extends Classifier { import Jama.Matrix; import objects.Template; public Classifier1NNNareshKumarMS() { DecisionKNN decision = new DecisionKNN(1); public class Classifier1NNNareshKumarMS extends Classifier { decision.setDistanceTemplates(new DistanceTemplatesNareshKumarMS()); setDecision(decision); public Classifier1NNNareshKumarMS() { } DecisionKNN decision = new DecisionKNN(1); } decision.setDistanceTemplates(new DistanceTemplatesNareshKumarMS()); setDecision(decision); } public final class DistanceTemplatesNareshKumarMS extends DistanceTemplates { public DistanceTemplatesNareshKumarMS() { } @Override public String getDescription() { return "NareshKumarMS"; } @Override public double getDistance(Template template1, Template template2) { double sum = 0.0; double[] generalizedEigenvalues = new EigenvalueDecomposition(getCovarianceMatrix(template2).inverse().times(getCovarianceMatrix(template1))).getRealEigenvalues(); for (int l = 0; l < generalizedEigenvalues.length; l++) { sum += Math.pow(Math.log(Math.abs(generalizedEigenvalues[l])), 2); } return Math.sqrt(sum); } private Matrix getCovarianceMatrix(Template template) { Matrix matrix = template.getMatrix(); int rows = matrix.getRowDimension(); int columns = matrix.getColumnDimension(); Matrix covarianceMatrix = new Matrix(columns, columns); for (int r = 0; r < rows; r++) { Matrix d = matrix.getMatrix(r, r, 0, columns - 1); Matrix u = getDistanceTemplatesMatrix().getMatrix(r, r, 0, columns - 1); Matrix diff = d.minus(u); covarianceMatrix.plusEquals((diff.transpose().times(diff)).times((double) 1 / (rows - 1))); } return covarianceMatrix; } } }
 ... @@ -39,8 +39,15 @@ public class ClassifierTransform1NNMMCMahalanobis extends ClassifierTransform { ... @@ -39,8 +39,15 @@ public class ClassifierTransform1NNMMCMahalanobis extends ClassifierTransform { double[] column = (getMeanMatrix(samplesOfClass).minus(meanSampleMatrix)).times(Math.sqrt((double) samplesOfClass.size() / numberOfSamples)).getColumnPackedCopy(); double[] column = (getMeanMatrix(samplesOfClass).minus(meanSampleMatrix)).times(Math.sqrt((double) samplesOfClass.size() / numberOfSamples)).getColumnPackedCopy(); Upsilon.setMatrix(0, dimension - 1, c, c, new Matrix(column, column.length)); Upsilon.setMatrix(0, dimension - 1, c, c, new Matrix(column, column.length)); } } SingularValueDecomposition svdChi = new SingularValueDecomposition(Chi); SingularValueDecomposition svdChi; Matrix Omega = svdChi.getU(); Matrix Omega; if (dimension < numberOfSamples) { svdChi = new SingularValueDecomposition(Chi.transpose()); Omega = svdChi.getV().transpose(); } else { svdChi = new SingularValueDecomposition(Chi); Omega = svdChi.getU(); } Matrix ThetaInverseSquareRoot = svdChi.getS(); Matrix ThetaInverseSquareRoot = svdChi.getS(); int dim = Math.min(ThetaInverseSquareRoot.getRowDimension(), ThetaInverseSquareRoot.getColumnDimension()); int dim = Math.min(ThetaInverseSquareRoot.getRowDimension(), ThetaInverseSquareRoot.getColumnDimension()); for (int d = 0; d < dim; d++) { for (int d = 0; d < dim; d++) { ... ...
 package algorithms; package algorithms; import java.util.List; import java.util.List; import objects.Template; import objects.Template; public abstract class Clustering extends Retriever { public abstract class Clusterer extends Retriever { public Clustering() { public Clusterer() { } } public abstract List> cluster(List
 package algorithms; package algorithms; import java.util.ArrayList; import java.util.ArrayList; import java.util.List; import java.util.List; import objects.Template; import objects.Template; public class ClusteringAgglomerativeHierarchical extends Clustering { public class ClustererAgglomerativeHierarchical extends Clusterer { private final int k; private final int k; private final double d; private final double d; public ClusteringAgglomerativeHierarchical(int k) { public ClustererAgglomerativeHierarchical(int k) { this.k = k; this.k = k; this.d = Double.MAX_VALUE; this.d = Double.MAX_VALUE; } } public ClusteringAgglomerativeHierarchical(double d) { public ClustererAgglomerativeHierarchical(double d) { this.k = Integer.MAX_VALUE; this.k = Integer.MAX_VALUE; this.d = d; this.d = d; } } @Override @Override public List> cluster(List
 package algorithms; package algorithms; import java.util.ArrayList; import java.util.ArrayList; import java.util.List; import java.util.List; import java.util.Random; import java.util.Random; import objects.Template; import objects.Template; public class ClusteringKmeans extends Clustering { public class ClustererKmeans extends Clusterer { private final int k; private final int k; public ClusteringKmeans(int k) { public ClustererKmeans(int k) { this.k = k; this.k = k; } } @Override @Override public List> cluster(List
 package algorithms; package algorithms; import java.util.List; import java.util.List; import objects.Template; import objects.Template; public abstract class Decision extends Retriever { public abstract class Decision extends Retriever { private List gallery; private List gallery; public Decision() { public List getGallery() { } return gallery; } public List getGallery() { return gallery; public void setGallery(List gallery) { } this.gallery = gallery; } public void setGallery(List gallery) { this.gallery = gallery; public abstract String getDescription(); } public abstract void importGallery(List
 package algorithms; package algorithms; import java.util.ArrayList; import java.util.List; import java.util.List; import java.util.Random; import java.util.Random; import objects.Template; import objects.Template; public class DecisionRandom extends Decision { public class DecisionRandom extends Decision { @Override @Override public String getDescription() { public String getDescription() { return "random"; return "random"; } } @Override @Override public void importGallery(List
 package algorithms; import java.util.List; import libsvm.svm; import libsvm.svm_model; import libsvm.svm_node; import libsvm.svm_parameter; import libsvm.svm_problem; import objects.Template; public class DecisionSVM extends Decision { @Override public String getDescription() { return "SVM"; } @Override public void importGallery(List
 package algorithms; package algorithms; import Jama.Matrix; import java.io.Serializable; import java.io.Serializable; import objects.Template; import objects.Template; public abstract class DistanceTemplates implements Serializable { public abstract class DistanceTemplates implements Serializable { public abstract String getDescription(); private Matrix distanceTemplatesMatrix; public abstract double getDistance(Template template1, Template template2); public DistanceTemplates() { } } public Matrix getDistanceTemplatesMatrix() { return distanceTemplatesMatrix; } public void setDistanceTemplatesMatrix(Matrix distanceTemplatesMatrix) { this.distanceTemplatesMatrix = distanceTemplatesMatrix; } public abstract String getDescription();