I’m using Weka Machine learning library’s Java API…
I’m trying to calculate the distance between two instances using the EuclidianDistance class:
http://weka.sourceforge.net/doc.dev/weka/core/EuclideanDistance.html
I have this code:
EuclideanDistance ed = new EuclideanDistance(finalInst);
double dist;
dist = ed.distance(finalInst.firstInstance(),finalInst.lastInstance());
finalInst is a valid Instances object that contains valid Instance objects…
Here’s what the first and last instance are when you use System.out.println:
finalInst.firstInstance():
?,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
finalInst.lastInstance():
?,?,?,?,1,1,?,1,?,1,?,?,1,?,?,?,?,?,1
But then when I run the code, it returns a null pointer exception…
Where did I go wrong?
I’ve got two practical suggestions:
1) Dig in WEKA’s code. It’s open-source, so you can actually add the source to your Eclipse project and browse the relevant functions with F3 (I do hope you’re using Eclipse or some other reasonable IDE).
2) Implement your own Euclidean distance. It’s really simple. Here, I’ve even done it for you: