The small sample of my problem is in the repo.
I have the below dataset in a .data
file:
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,Action
0,0,0,2,0,0,0,2,0,0,0,0,0,0,0,0,"Up"
2,0,0,0,2,0,0,0,0,0,0,2,0,0,0,0,"Left"
4,0,0,2,0,0,0,0,0,2,0,0,0,0,0,0,"Left"
4,2,0,2,0,2,0,0,0,0,0,0,0,0,0,0,"Up"
4,4,0,0,2,0,0,0,0,0,0,0,0,0,0,2,"Up"
8,0,0,0,2,0,0,0,2,0,0,0,2,0,0,0,"Left"
The dataset has 16 int
features and the last column is String
. I want to use the first 16 features to predict the last column using knn
.
I have trained my model successfully based on this link.
knn = new KNearestNeighbors(5);
knn.buildClassifier(data);
But now, i need to Test my model. So, the format of the TestData is, 16 integer numbers, and i expect that the knn
model predicts the action.
Sample Test Data is:
4,4,0,0,2,0,0,0,0,0,0,0,0,0,0,2
based on the code i need to have an object of Instance
interface from net.sf.javaml.core.Instance
, but the problem is:
i am wondering how to create such instance?
Well you can simply use SparseInstance method which asks for an array of Doubles. If, you convert your TestData to Double
, then it will be very easy:
double[] testData = {32,16,8,2,16,8,2,2,8,2,0,0,0,0,0,0};
Instance inst=new SparseInstance(testData);
Object predictedClassValue = knn.classify(inst);
System.out.println("Result is: "+predictedClassValue);
I tried the above code on your repo, and it gives me:
Result is: Left