[Java-ML-support] How to use a custom distance measure for K-medoids clustering ??
Status: Beta
Brought to you by:
thomasabeel
|
From: Khirod K. N. <khi...@gm...> - 2014-05-06 01:05:14
|
I have to use the K-medoids clustering for my project and I am having
trouble using a simple implementation. So far I have done this
import net.sf.javaml.core.*;
import net.sf.javaml.clustering.*;
import net.sf.javaml.distance.*;
public class ClusteringTest {
public static void main(String args[]) {
Dataset data = new DefaultDataset();
double[] val = {1, 2, 7};
double[] gval = {1, 2, 3};
CustomDist de = new CustomDist();
for (int i = 0; i < val.length; i++) {
Instance instance = new SparseInstance(2);
instance.put(1, val[i]);
instance.put(2, gval[i]);
data.add(instance);
}
// Test Clustering
Clusterer km = new KMedoids(3, 10, de);
Dataset[] clusters = km.cluster(data);
System.out.println(clusters[0]);
System.out.println(clusters[1]);
}
}
class CustomDist implements DistanceMeasure {
public boolean compare(double x, double y) {
return x < y;
}
public double getMaxValue() {
return Math.sqrt(40);
}
public double getMinValue() {
return Math.sqrt(2);
}
public double measure(Instance x, Instance y) {
// Use Euclidean Distance (for test only)
double x1 = Math.abs(x.value(1) - y.value(1)) * Math.abs(x.value(1)
- y.value(1));
double y1 = Math.abs(x.value(2) - y.value(2)) * Math.abs(x.value(2)
- y.value(2));
return Math.sqrt(x1 + y1);
}
}
Is there something wrong with my ClusteringTest Class??
|