Commit acab277e authored by Michal Balazia's avatar Michal Balazia
Browse files

distance map

parent 07d6abac
package algorithms;
import Jama.EigenvalueDecomposition;
import Jama.Matrix;
import objects.Template;
public class Classifier1NNNareshKumarMS extends Classifier {
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;
}
}
}
package algorithms;
public class Classifier1NNNareshKumarMS extends Classifier {
public Classifier1NNNareshKumarMS() {
DecisionKNN decision = new DecisionKNN(1);
decision.setDistanceTemplates(new DistanceTemplatesNareshKumarMS());
setDecision(decision);
}
}
......@@ -39,8 +39,15 @@ public class ClassifierTransform1NNMMCMahalanobis extends ClassifierTransform {
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));
}
SingularValueDecomposition svdChi = new SingularValueDecomposition(Chi);
Matrix Omega = svdChi.getU();
SingularValueDecomposition svdChi;
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();
int dim = Math.min(ThetaInverseSquareRoot.getRowDimension(), ThetaInverseSquareRoot.getColumnDimension());
for (int d = 0; d < dim; d++) {
......
package algorithms;
import java.util.List;
import objects.Template;
public abstract class Clustering extends Retriever {
public Clustering() {
}
public abstract List<List<Template>> cluster(List<Template> templatesGallery);
public Template getCentroid(List<Template> templates) {
Template centroid = new Template(null, null);
if (templates.isEmpty()) {
System.out.println("empty class");
} else {
double minSum = Double.MAX_VALUE;
for (Template centroidCandidate : templates) {
double sum = 0;
for (Template template : templates) {
sum += getDistance(template, centroidCandidate);
}
if (minSum > sum) {
minSum = sum;
centroid = centroidCandidate;
}
}
}
return centroid;
}
}
package algorithms;
import java.util.List;
import objects.Template;
public abstract class Clusterer extends Retriever {
public Clusterer() {
}
public abstract List<List<Template>> cluster(List<Template> templatesGallery);
public Template getCentroid(List<Template> templates) {
Template centroid = new Template(null, null);
if (templates.isEmpty()) {
System.out.println("empty class");
} else {
double minSum = Double.MAX_VALUE;
for (Template centroidCandidate : templates) {
double sum = 0;
for (Template template : templates) {
sum += getDistance(template, centroidCandidate);
}
if (minSum > sum) {
minSum = sum;
centroid = centroidCandidate;
}
}
}
return centroid;
}
}
package algorithms;
import java.util.ArrayList;
import java.util.List;
import objects.Template;
public class ClusteringAgglomerativeHierarchical extends Clustering {
private final int k;
private final double d;
public ClusteringAgglomerativeHierarchical(int k) {
this.k = k;
this.d = Double.MAX_VALUE;
}
public ClusteringAgglomerativeHierarchical(double d) {
this.k = Integer.MAX_VALUE;
this.d = d;
}
@Override
public List<List<Template>> cluster(List<Template> templatesGallery) {
int numberOfTemplates = templatesGallery.size();
if (numberOfTemplates < k) {
System.out.println(numberOfTemplates + " < " + k);
}
List<List<Template>> clusters = new ArrayList();
List<Template> centroids = new ArrayList();
for (Template template : templatesGallery) {
List<Template> cluster = new ArrayList();
cluster.add(template);
clusters.add(cluster);
centroids.add(template);
}
double minDistance = 0;
while (clusters.size() > k && minDistance < d) {
int numberOfClusters = clusters.size();
List<Template> clusterI = clusters.get(0);
List<Template> clusterJ = clusters.get(0);
Template centroidI = centroids.get(0);
Template centroidJ = centroids.get(0);
minDistance = Double.MAX_VALUE;
for (int i = 0; i < numberOfClusters - 1; i++) {
for (int j = i + 1; j < numberOfClusters; j++) {
double distance;
distance = getDistance(centroids.get(i), centroids.get(j));
if (minDistance > distance) {
minDistance = distance;
clusterI = clusters.get(i);
clusterJ = clusters.get(j);
centroidI = centroids.get(i);
centroidJ = centroids.get(j);
}
}
}
clusters.remove(clusterI);
clusters.remove(clusterJ);
centroids.remove(centroidI);
centroids.remove(centroidJ);
clusterI.addAll(clusterJ);
clusters.add(clusterI);
centroids.add(getCentroid(clusterI));
}
return clusters;
}
}
package algorithms;
import java.util.ArrayList;
import java.util.List;
import objects.Template;
public class ClustererAgglomerativeHierarchical extends Clusterer {
private final int k;
private final double d;
public ClustererAgglomerativeHierarchical(int k) {
this.k = k;
this.d = Double.MAX_VALUE;
}
public ClustererAgglomerativeHierarchical(double d) {
this.k = Integer.MAX_VALUE;
this.d = d;
}
@Override
public List<List<Template>> cluster(List<Template> templatesGallery) {
int numberOfTemplates = templatesGallery.size();
if (numberOfTemplates < k) {
System.out.println(numberOfTemplates + " < " + k);
}
List<List<Template>> clusters = new ArrayList();
List<Template> centroids = new ArrayList();
for (Template template : templatesGallery) {
List<Template> cluster = new ArrayList();
cluster.add(template);
clusters.add(cluster);
centroids.add(template);
}
double minDistance = 0;
while (clusters.size() > k && minDistance < d) {
int numberOfClusters = clusters.size();
List<Template> clusterI = clusters.get(0);
List<Template> clusterJ = clusters.get(0);
Template centroidI = centroids.get(0);
Template centroidJ = centroids.get(0);
minDistance = Double.MAX_VALUE;
for (int i = 0; i < numberOfClusters - 1; i++) {
for (int j = i + 1; j < numberOfClusters; j++) {
double distance;
distance = getDistance(centroids.get(i), centroids.get(j));
if (minDistance > distance) {
minDistance = distance;
clusterI = clusters.get(i);
clusterJ = clusters.get(j);
centroidI = centroids.get(i);
centroidJ = centroids.get(j);
}
}
}
clusters.remove(clusterI);
clusters.remove(clusterJ);
centroids.remove(centroidI);
centroids.remove(centroidJ);
clusterI.addAll(clusterJ);
clusters.add(clusterI);
centroids.add(getCentroid(clusterI));
}
return clusters;
}
}
package algorithms;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import objects.Template;
public class ClusteringKmeans extends Clustering {
private final int k;
public ClusteringKmeans(int k) {
this.k = k;
}
@Override
public List<List<Template>> cluster(List<Template> templatesGallery) {
int numberOfTemplates = templatesGallery.size();
if (numberOfTemplates < k) {
System.out.println(numberOfTemplates + " < " + k);
return null;
}
Random random = new Random();
List<Integer> seeds = new ArrayList();
for (int i = 0; i < k; i++) {
int seed = random.nextInt(numberOfTemplates);
while (seeds.contains(seed)) {
seed = random.nextInt(numberOfTemplates);
}
seeds.add(seed);
}
List<Template> centroids = new ArrayList();
for (int i = 0; i < k; i++) {
centroids.add(templatesGallery.get(seeds.get(i)));
}
List<List<Template>> clusters = new ArrayList();
boolean done = false;
while (!done) {//CHANGE
done = true;
clusters = new ArrayList();
for (int i = 0; i < k; i++) {
clusters.add(new ArrayList());
}
for (Template template : templatesGallery) {
double minDistance = Double.MAX_VALUE;
int closestCentroidIndex = 0;
for (int i = 0; i < k; i++) {
double distance = getDistance(template, centroids.get(i));
if (minDistance > distance) {
minDistance = distance;
closestCentroidIndex = i;
}
}
clusters.get(closestCentroidIndex).add(template);
}
for (int i = 0; i < k; i++) {
Template centroidOld = centroids.get(i);
Template centroidNew = getCentroid(clusters.get(i));
centroids.set(i, centroidNew);
if(!centroidOld.equals(centroidNew)){
done = false;
}
}
}
return clusters;
}
}
package algorithms;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import objects.Template;
public class ClustererKmeans extends Clusterer {
private final int k;
public ClustererKmeans(int k) {
this.k = k;
}
@Override
public List<List<Template>> cluster(List<Template> templatesGallery) {
int numberOfTemplates = templatesGallery.size();
if (numberOfTemplates < k) {
System.out.println(numberOfTemplates + " < " + k);
return null;
}
Random random = new Random();
List<Integer> seeds = new ArrayList();
for (int i = 0; i < k; i++) {
int seed = random.nextInt(numberOfTemplates);
while (seeds.contains(seed)) {
seed = random.nextInt(numberOfTemplates);
}
seeds.add(seed);
}
List<Template> centroids = new ArrayList();
for (int i = 0; i < k; i++) {
centroids.add(templatesGallery.get(seeds.get(i)));
}
List<List<Template>> clusters = new ArrayList();
boolean done = false;
while (!done) {//CHANGE
done = true;
clusters = new ArrayList();
for (int i = 0; i < k; i++) {
clusters.add(new ArrayList());
}
for (Template template : templatesGallery) {
double minDistance = Double.MAX_VALUE;
int closestCentroidIndex = 0;
for (int i = 0; i < k; i++) {
double distance = getDistance(template, centroids.get(i));
if (minDistance > distance) {
minDistance = distance;
closestCentroidIndex = i;
}
}
clusters.get(closestCentroidIndex).add(template);
}
for (int i = 0; i < k; i++) {
Template centroidOld = centroids.get(i);
Template centroidNew = getCentroid(clusters.get(i));
centroids.set(i, centroidNew);
if(!centroidOld.equals(centroidNew)){
done = false;
}
}
}
return clusters;
}
}
package algorithms;
import java.util.List;
import objects.Template;
public abstract class Decision extends Retriever {
private List gallery;
public Decision() {
}
public List getGallery() {
return gallery;
}
public void setGallery(List gallery) {
this.gallery = gallery;
}
public abstract String getDescription();
public abstract void importGallery(List<Template> templatesGallery);
public abstract String decide(Template template);
}
package algorithms;
import java.util.List;
import objects.Template;
public abstract class Decision extends Retriever {
private List gallery;
public List getGallery() {
return gallery;
}
public void setGallery(List gallery) {
this.gallery = gallery;
}
public abstract String getDescription();
public abstract void importGallery(List<Template> templatesGallery);
public abstract String decide(Template template);
}
package algorithms;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import objects.Template;
public class DecisionRandom extends Decision {
@Override
public String getDescription() {
return "random";
}
@Override
public void importGallery(List<Template> templatesGallery) {
List<String> gallery = new ArrayList();
for (Template template : templatesGallery) {
gallery.add(template.getSubject());
}
setGallery(gallery);
}
@Override
public String decide(Template templateProbe) {
List<String> gallery = getGallery();
return gallery.get(new Random().nextInt(gallery.size()));
}
}
package algorithms;
import java.util.List;
import java.util.Random;
import objects.Template;
public class DecisionRandom extends Decision {
@Override
public String getDescription() {
return "random";
}
@Override
public void importGallery(List<Template> templatesGallery) {
setGallery(templatesGallery);
}
@Override
public String decide(Template templateProbe) {
List<Template> gallery = getGallery();
return gallery.get(new Random().nextInt(gallery.size())).getSubject();
}
}
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<Template> templatesGallery) {
setGallery(templatesGallery);
}
@Override
public String decide(Template templateProbe) {
svm svm = new svm();
svm_model svm_model = new svm_model();
svm_problem svm_problem = new svm_problem();
return "";
}
double[][] train = new double[1000][];
double[][] test = new double[10][];
private svm_model svmTrain() {
svm_problem prob = new svm_problem();
int dataCount = train.length;
prob.y = new double[dataCount];
prob.l = dataCount;
prob.x = new svm_node[dataCount][];
for (int i = 0; i < dataCount; i++) {
double[] features = train[i];
prob.x[i] = new svm_node[features.length - 1];
for (int j = 1; j < features.length; j++) {
svm_node node = new svm_node();
node.index = j;
node.value = features[j];
prob.x[i][j - 1] = node;
}
prob.y[i] = features[0];
}
svm_parameter param = new svm_parameter();
param.probability = 1;
param.gamma = 0.5;
param.nu = 0.5;
param.C = 1;
param.svm_type = svm_parameter.C_SVC;
param.kernel_type = svm_parameter.LINEAR;
param.cache_size = 20000;
param.eps = 0.001;
svm_model model = svm.svm_train(prob, param);